Verified Commit a803ad41 authored by Karel Koci's avatar Karel Koci 🤘

subprocess: mark some arguments as not possible to be NULL

This also removes possibly hack to run C/Lua code in subprocess without
providing command. It is overhead but it is more systematic to use
'true' as a command instead of possibly in code unexpected value NULL.
parent 5a09ba46
......@@ -47,20 +47,17 @@ static void run_child(const char *cmd, const char *args[], subproc_callback call
if (callback)
callback(data);
// Exec
if (cmd) {
size_t arg_c = 2; // cmd and NULL terminator
for (const char **p = args; *p; p++)
arg_c++;
char *argv[arg_c];
size_t i = 1;
for (const char **p = args; *p; p++)
argv[i++] = strdup(*p);
argv[i] = NULL;
argv[0] = strdup(cmd);
execvp(cmd, argv);
DIE("Failed to exec %s: %s", cmd, strerror(errno));
} else
exit(0); // We just exit child
size_t arg_c = 2; // cmd and NULL terminator
for (const char **p = args; *p; p++)
arg_c++;
char *argv[arg_c];
size_t i = 1;
for (const char **p = args; *p; p++)
argv[i++] = strdup(*p);
argv[i] = NULL;
argv[0] = strdup(cmd);
execvp(cmd, argv);
DIE("Failed to exec %s: %s", cmd, strerror(errno));
}
int subprocv(int timeout, const char *cmd, ...) {
......
......@@ -46,22 +46,22 @@ Value can also be NULL and in that case we unset given environment variable.
Note that these calls are blocking ones.
Returned status field from wait call. See manual for wait on how to decode it.
*/
int subprocv(int timeout, const char *command, ...); // (char *) NULL
int subprocvo(int timeout, FILE *fd[2], const char *command, ...); // (char *) NULL
int subprocvoc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, ...); // (char *) NULL
int subprocl(int timeout, const char *command, const char *args[]);
int subproclo(int timeout, FILE *fd[2], const char *command, const char *args[]);
int subprocloc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, const char *args[]);
int vsubprocv(int timeout, const char *command, va_list args);
int vsubprocvo(int timeout, FILE *fd[2], const char *command, va_list args);
int vsubprocvoc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, va_list args);
int subprocv(int timeout, const char *command, ...) __attribute__((nonnull(2))); // (char *) NULL
int subprocvo(int timeout, FILE *fd[2], const char *command, ...) __attribute__((nonnull(2,3))); // (char *) NULL
int subprocvoc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, ...) __attribute__((nonnull(2,5))); // (char *) NULL
int subprocl(int timeout, const char *command, const char *args[]) __attribute__((nonnull(2,3)));
int subproclo(int timeout, FILE *fd[2], const char *command, const char *args[]) __attribute__((nonnull(2,3,4)));
int subprocloc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, const char *args[]) __attribute__((nonnull(2,5,6)));
int vsubprocv(int timeout, const char *command, va_list args) __attribute__((nonnull(2)));
int vsubprocvo(int timeout, FILE *fd[2], const char *command, va_list args) __attribute__((nonnull(2,3)));
int vsubprocvoc(int timeout, FILE *fd[2], subproc_callback callback, void *data, const char *command, va_list args) __attribute__((nonnull(2,5)));
// Following functions integrate log_subproc with subproc to enable logging of subprocess output.
int lsubprocv(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, ...);
int lsubprocvc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, ...);
int lsubprocl(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, const char *args[]);
int lsubproclc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, const char *args[]);
int lvsubprocv(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, va_list args);
int lvsubprocvc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, va_list args);
int lsubprocv(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, ...) __attribute__((nonnull(2,5)));
int lsubprocvc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, ...) __attribute__((nonnull(2,7)));
int lsubprocl(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, const char *args[]) __attribute__((nonnull(2,5,6)));
int lsubproclc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, const char *args[]) __attribute__((nonnull(2,7,8)));
int lvsubprocv(enum log_subproc_type type, const char *message, char **output, int timeout, const char *command, va_list args) __attribute__((nonnull(2,5)));
int lvsubprocvc(enum log_subproc_type type, const char *message, char **output, int timeout, subproc_callback callback, void *data, const char *command, va_list args) __attribute__((nonnull(2,7)));
#endif
......@@ -112,10 +112,10 @@ START_TEST(callback) {
struct buffs *bfs = buffs_init();
// Without data
ck_assert_int_eq(0, subprocloc(1000, bfs->fds, callback_test, NULL, NULL, NULL));
ck_assert_int_eq(0, subprocvoc(1000, bfs->fds, callback_test, NULL, "true", NULL));
buffs_assert(bfs, "hello", "");
// With data
ck_assert_int_eq(0, subprocvoc(1000, bfs->fds, callback_test, "Hello again", NULL, NULL));
ck_assert_int_eq(0, subprocvoc(1000, bfs->fds, callback_test, "Hello again", "true", NULL));
buffs_assert(bfs, "Hello again", "");
buffs_free(bfs);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment