VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_process.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*****************************************************************************
3 * vlc_process.h: vlc_process functions
4 *****************************************************************************
5 * Copyright © 2025 Videolabs, VideoLAN and VLC authors
6 *
7 * Authors: Gabriel Lafond Thenaille <gabriel@videolabs.io>
8 *****************************************************************************/
9
10#ifndef VLC_PROCESS_H
11#define VLC_PROCESS_H
12
13#include <vlc_common.h>
14#include <vlc_tick.h>
15
16/**
17 * @ingroup misc
18 * @file
19 * VLC_PROCESS API
20 * @defgroup process Process API
21 * @{
22 */
23
24/**
25 * Spawn a new process with input and output redirection.
26 *
27 * Creates and starts a new vlc_process for the specified executable path with
28 * the given arguments. Sets up pipes to allow reading from the process's
29 * standard output and writing to its standard input.
30 *
31 * @param [in] path Path to the executable to run. Must not be NULL.
32 * @param [in] argc Number of arguments passed to the process (must be
33 * greater than or equal to 0).
34 * @param [in] argv Array of argument strings. May be NULL if argc is 0;
35 * otherwise argv[0] must not be NULL.
36 *
37 * @return A pointer to the newly created vlc_process structure on
38 * success, or NULL on failure.
39 */
40VLC_API struct vlc_process *
41vlc_process_Spawn(const char *path, int argc, const char *const *argv);
42
43/**
44 * Kill a process and abort its I/O.
45 *
46 * Sends a termination signal to the process and shuts down the communication
47 * channel, so that any in-flight or subsequent I/O on @p process completes
48 * promptly instead of blocking: vlc_process_fd_Read() reports end-of-stream
49 * and vlc_process_fd_Write() fails with EPIPE.
50 *
51 * This does not free @p process: vlc_process_Terminate() must still be called
52 * for that. It can be called multiple times.
53 *
54 * @param [in] process Pointer to the vlc_process instance. Must not be
55 * NULL.
56 */
57VLC_API void
58vlc_process_Kill(struct vlc_process *process);
59
60/**
61 * Stop a vlc_process and wait for its termination.
62 *
63 * Closes its file descriptors, and waits for it to exit. Optionally sends a
64 * termination signal to the process,
65 *
66 * @warning This must not be called concurrently with vlc_process_fd_Read() or
67 * vlc_process_fd_Write() on the same @p process. It closes the
68 * underlying pipe/socket and frees @p process, so the caller must
69 * ensure that any thread performing I/O has returned first to avoid a
70 * use-after-free. Use vlc_process_Kill() to unblock such a thread,
71 * then join it before calling this.
72 *
73 * @param [in] process Pointer to the vlc_process instance. Must not
74 * be NULL.
75 * @param [in] kill_process Whether to forcibly terminate the process
76 * before waiting.
77 *
78 * @return The exit status of the process, or -1 on error.
79 */
80VLC_API int
81vlc_process_Terminate(struct vlc_process *process, bool kill_process);
82
83/**
84 * Read data from the process's standard output with a timeout.
85 *
86 * Attempts to read up to @p size bytes from the process's standard output
87 * into the provided buffer, waiting up to @p timeout_ms milliseconds for data
88 * to become available.
89 *
90 * On POSIX systems, this uses poll to wait for readability. On Windows,
91 * a platform-specific implementation is used due to limitations with poll on
92 * non-socket handles.
93 *
94 * @warning At most one thread may call this function on a given @p process at
95 * a time. Reading from one thread while another one writes is
96 * supported, but two concurrent readers are not.
97 *
98 * @param [in] process Pointer to the vlc_process instance.
99 * @param [out] buf Buffer where the read data will be stored.
100 * @param [in] size Maximum number of bytes to read.
101 * @param [in] timeout_ms Timeout in milliseconds to wait for data.
102 *
103 * @return The number of bytes read on success,
104 * -1 on error, and errno is set to indicate the error.
105 */
106VLC_API ssize_t
107vlc_process_fd_Read(struct vlc_process *process, uint8_t *buf, size_t size,
108 vlc_tick_t timeout_ms);
109
110/**
111 * Write data to the process's standard input with a timeout.
112 *
113 * Attempts to write up to @p size bytes from the provided buffer to the
114 * process's standard input, waiting up to @p timeout_ms milliseconds for the
115 * pipe to become writable.
116 *
117 * On POSIX systems, this uses poll to wait for writability. On Windows,
118 * a platform-specific implementation is used due to limitations with poll on
119 * non-socket handles.
120 *
121 * @warning At most one thread may call this function on a given @p process at
122 * a time. Writing from one thread while another one reads is
123 * supported, but two concurrent writers are not.
124 *
125 * @param [in] process Pointer to the vlc_process instance.
126 * @param [in] buf Buffer containing the data to write.
127 * @param [in] size Number of bytes to write.
128 * @param [in] timeout_ms Timeout in milliseconds to wait for the pipe to be
129 * writable.
130 *
131 * @return The number of bytes written on success,
132 * -1 on error, and errno is set to indicate the error.
133 */
134VLC_API ssize_t
135vlc_process_fd_Write(struct vlc_process *process, const uint8_t *buf, size_t size,
136 vlc_tick_t timeout_ms);
137
138/**
139 * @} process
140 */
141
142#endif /* VLC_PROCESS_H */
#define VLC_API
Definition fourcc_gen.c:31
struct vlc_process * vlc_process_Spawn(const char *path, int argc, const char *const *argv)
Spawn a new process with input and output redirection.
Definition missing.c:185
ssize_t vlc_process_fd_Write(struct vlc_process *process, const uint8_t *buf, size_t size, vlc_tick_t timeout_ms)
Write data to the process's standard input with a timeout.
Definition missing.c:222
void vlc_process_Kill(struct vlc_process *process)
Kill a process and abort its I/O.
Definition missing.c:194
int vlc_process_Terminate(struct vlc_process *process, bool kill_process)
Stop a vlc_process and wait for its termination.
Definition missing.c:201
ssize_t vlc_process_fd_Read(struct vlc_process *process, uint8_t *buf, size_t size, vlc_tick_t timeout_ms)
Read data from the process's standard output with a timeout.
Definition missing.c:210
Definition process.c:32
This file is a collection of common definitions and types.
int64_t vlc_tick_t
High precision date or time interval.
Definition vlc_tick.h:48