00001 #ifndef LIBNAGIOS_runcmd_h__ 00002 #define LIBNAGIOS_runcmd_h__ 00003 #include <signal.h> 00004 00005 /** 00006 * @file runcmd.h 00007 * @brief runcmd library function declarations 00008 * 00009 * @note This is inherited from the nagiosplugins project, although 00010 * I (AE) wrote the original code, and it might need refactoring 00011 * for performance later. 00012 * @{ 00013 */ 00014 00015 /** Return code bitflags for runcmd_cmd2strv() */ 00016 #define RUNCMD_HAS_REDIR (1 << 0) /**< I/O redirection */ 00017 #define RUNCMD_HAS_SUBCOMMAND (1 << 1) /**< subcommands present */ 00018 #define RUNCMD_HAS_PAREN (1 << 2) /**< parentheses present in command */ 00019 #define RUNCMD_HAS_JOBCONTROL (1 << 3) /**< job control stuff present */ 00020 #define RUNCMD_HAS_UBSQ (1 << 4) /**< unbalanced single quotes */ 00021 #define RUNCMD_HAS_UBDQ (1 << 5) /**< unbalanced double quotes */ 00022 #define RUNCMD_HAS_WILDCARD (1 << 6) /**< wildcards present */ 00023 #define RUNCMD_HAS_SHVAR (1 << 7) /**< shell variables present */ 00024 00025 00026 #define RUNCMD_EFD (-1) /**< Failed to pipe() or open() */ 00027 #define RUNCMD_EALLOC (-2) /**< Failed to alloc */ 00028 #define RUNCMD_ECMD (-3) /**< Bad command */ 00029 #define RUNCMD_EFORK (-4) /**< Failed to fork() */ 00030 #define RUNCMD_EINVAL (-5) /**< Invalid parameters */ 00031 #define RUNCMD_EWAIT (-6) /**< Failed to wait() */ 00032 00033 /** 00034 * Initialize the runcmd library. 00035 * 00036 * Only multi-threaded programs that might launch the first external 00037 * program from multiple threads simultaneously need to bother with 00038 * this. 00039 */ 00040 extern void runcmd_init(void); 00041 00042 /** 00043 * Return pid of a command with a specific file descriptor 00044 * @param[in] fd stdout filedescriptor of the child to get pid from 00045 * @return pid of the child, or 0 on errors 00046 */ 00047 extern pid_t runcmd_pid(int fd); 00048 00049 /** 00050 * Return explanation of which system call or operation failed 00051 * @param code Error code returned by a library function 00052 * @return A non-free()'able string explaining where the error occurred 00053 */ 00054 extern const char *runcmd_strerror(int code); 00055 00056 /** 00057 * Start a command from a command string 00058 * @param[in] cmdstring The command to launch 00059 * @param[out] pfd Child's stdout filedescriptor 00060 * @param[out] pfderr Child's stderr filedescriptor 00061 * @param[in] env Currently ignored for portability 00062 * @param[in] iobreg The callback function to register the iobrokers for the read ends of the pipe 00063 * @param[in] iobregarg The "arg" value to pass to iobroker_register() 00064 */ 00065 extern int runcmd_open(const char *cmd, int *pfd, int *pfderr, char **env, 00066 void (*iobreg)(int, int, void *), void *iobregarg) 00067 __attribute__((__nonnull__(1, 2, 3, 5, 6))); 00068 00069 /** 00070 * Close a command and return its exit status 00071 * @note Don't use this. It's a retarded way to reap children suitable 00072 * only for launching a one-shot program. 00073 * 00074 * @param[in] fd The child's stdout filedescriptor 00075 * @return exit-status of the child, or -1 in case of errors 00076 */ 00077 extern int runcmd_close(int fd); 00078 00079 /** 00080 * Convert a string to a vector of arguments like a shell would 00081 * @note This might have bugs and is only tested to behave similar 00082 * to how /bin/sh does things. For csh or other non bash-ish shells 00083 * there are no guarantees. 00084 * @note The out_argv array has to be large enough to hold all strings 00085 * found in the command. 00086 * @param[in] str The string to convert to an argument vector 00087 * @param[out] out_argc The number of arguments found 00088 * @param[out] out_argv The argument vector 00089 * @return 0 on (great) success, or a bitmask of failure-codes 00090 * representing f.e. unclosed quotes, job control or output redirection. 00091 * See the RUNCMD_HAS_* and their ilk to find out about the flag. 00092 */ 00093 extern int runcmd_cmd2strv(const char *str, int *out_argc, char **out_argv); 00094 00095 #endif /* INCLUDE_runcmd_h__ */ 00096 /** @} */