diff --git a/Makefile b/Makefile index c98f01d..3212155 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,8 @@ LIBIFUPDOWN_SRC = \ libifupdown/fgetline.c \ libifupdown/version.c \ libifupdown/state.c \ - libifupdown/environment.c + libifupdown/environment.c \ + libifupdown/execute.c LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_LIB = libifupdown.a diff --git a/libifupdown/execute.c b/libifupdown/execute.c new file mode 100644 index 0000000..563fa4c --- /dev/null +++ b/libifupdown/execute.c @@ -0,0 +1,54 @@ +/* + * libifupdown/execute.c + * Purpose: execution of individual commands + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libifupdown/execute.h" + +#define SHELL "/bin/sh" + +bool +lif_execute_fmt(char *const envp[], const char *fmt, ...) +{ + char cmdbuf[4096]; + va_list va; + + va_start(va, fmt); + vsnprintf(cmdbuf, sizeof cmdbuf, fmt, va); + va_end(va); + + pid_t child; + char *argv[] = { SHELL, "-c", cmdbuf, NULL }; + + if (posix_spawn(&child, SHELL, NULL, NULL, argv, envp) != 0) + { + fprintf(stderr, "execute '%s': %s\n", cmdbuf, strerror(errno)); + return false; + } + + int status; + waitpid(child, &status, 0); + + return WIFEXITED(status) && WEXITSTATUS(status) == 0; +} diff --git a/libifupdown/execute.h b/libifupdown/execute.h new file mode 100644 index 0000000..1a01248 --- /dev/null +++ b/libifupdown/execute.h @@ -0,0 +1,24 @@ +/* + * libifupdown/execute.h + * Purpose: execution of individual commands + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef LIBIFUPDOWN_EXECUTE_H__GUARD +#define LIBIFUPDOWN_EXECUTE_H__GUARD + +#include +#include + +extern bool lif_execute_fmt(char *const envp[], const char *fmt, ...); + +#endif diff --git a/libifupdown/libifupdown.h b/libifupdown/libifupdown.h index b9c48d8..774d729 100644 --- a/libifupdown/libifupdown.h +++ b/libifupdown/libifupdown.h @@ -24,5 +24,6 @@ #include "libifupdown/version.h" #include "libifupdown/state.h" #include "libifupdown/environment.h" +#include "libifupdown/execute.h" #endif