diff options
-rw-r--r-- | crash-worker/util.c | 172 | ||||
-rw-r--r-- | crash-worker/util.h | 47 |
2 files changed, 5 insertions, 214 deletions
diff --git a/crash-worker/util.c b/crash-worker/util.c index 48467db..2b00a99 100644 --- a/crash-worker/util.c +++ b/crash-worker/util.c @@ -30,98 +30,6 @@ #include <wait.h> #include "util.h" -int writepid(char *pidpath) -{ - FILE *fp; - - fp = fopen(pidpath, "w"); - if (fp != NULL) { - fprintf(fp, "%d", getpid()); - fclose(fp); - } - return 0; -} - -/** - * @brief read the pid - * - * get a pid and write it to pidpath - * - * @param[in] pidpath pid file path - * @return pid : success, -1 : failed - */ -int readpid(char *pidpath) -{ - FILE *fp; - int ret = -1; - - fp = fopen(pidpath, "r"); - if (fp != NULL) { - fscanf(fp, "%5d", &ret); - fclose(fp); - } - return ret; -} -/** - * @brief MANAGERize function - * - * fork the process, kill the parent process - * and replace all the standard fds to /dev/null. - * - * @return 0 : success, -1 : fork() error - */ -int daemonize(void) -{ - pid_t pid; - - pid = fork(); - if (pid < 0) - return -1; - else if (pid != 0) - exit(0); - setsid(); - chdir("/"); - close(0); - close(1); - close(2); - open("/dev/null", O_RDONLY); - open("/dev/null", O_RDWR); - dup(1); - return 0; -} -/** - * @brief function to run a process - * - * fork the process, and run the other process if it is child. - * - * @return new process pid on success, -1 on error - */ -int exec_process(char *name) -{ - int ret, pid; - int i; - - if (name[0] == '\0') - return 0; - pid = fork(); - switch (pid) { - case -1: - LOGERR("Fork error"); - ret = -1; - break; - case 0: - for (i = 0; i < _NSIG; i++) - signal(i, SIG_DFL); - execlp(name, name, NULL); - LOGERR("execlp() error : %s\n", strerror(errno)); - exit(-1); - break; - default: - ret = pid; - break; - } - return ret; -} int system_command(char *command) { int pid = 0, @@ -200,69 +108,6 @@ int system_command_with_timeout(int timeout_seconds, char *command) usleep(100000); } } -int run_command(int timeout_seconds, const char *command, ...) -{ - clock_t start = clock(); - pid_t pid = fork(); - - /* handle error case */ - if (pid < 0) { - LOGERR("fork: %s\n", strerror(errno)); - return pid; - } - - /* handle child case */ - if (pid == 0) { - const char *args[1024] = {command}; - size_t arg; - - va_list ap; - va_start(ap, command); - for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { - args[arg] = va_arg(ap, const char *); - if (args[arg] == NULL) - break; - } - execvp(command, (char **) args); - LOGINFO("exec(%s): %s\n", command, strerror(errno)); - _exit(-1); - } - /* handle parent case */ - for (;;) { - int status; - pid_t p = waitpid(pid, &status, WNOHANG); - float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC; - if (p == pid) { - if (WIFSIGNALED(status)) - LOGINFO("%s: Killed by signal %d\n", command, WTERMSIG(status)); - else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) - LOGINFO("%s: Exit code %d\n", command, WEXITSTATUS(status)); - return status; - } - if (timeout_seconds && elapsed > timeout_seconds) { - LOGINFO("%s: Timed out after %.1fs (killing pid %d)\n", - command, elapsed, pid); - kill(pid, SIGTERM); - return -1; - } - /* poll every 0.1 sec */ - usleep(100000); - } -} -char *fgets_fd(char *s, int n, int fd) -{ - char c; - register char *cs; - int num = 0; - - cs = s; - while (--n > 0 && (num = read(fd, &c, 1) > 0)) { - if ((*cs++ = c) == '\n') - break; - } - *cs = '\0'; - return (num == 0 && cs == s) ? NULL : s; -} /* WARNING : formatted string buffer is limited to 1024 byte */ int fprintf_fd(int fd, const char *fmt, ...) @@ -278,6 +123,7 @@ int fprintf_fd(int fd, const char *fmt, ...) va_end(args); return ret; } + int file_exist(const char *file) { FILE *fp; @@ -288,6 +134,7 @@ int file_exist(const char *file) fclose(fp); return 1; } + int write_fd(int fd, const void *buf, int len) { int count; @@ -306,6 +153,7 @@ int write_fd(int fd, const void *buf, int len) } return total; } + int copy_file(char *src, char *dst) { int sfd; @@ -337,6 +185,7 @@ int copy_file(char *src, char *dst) LOGINFO("copy (%s) to (%s)\n", src, dst); return 1; } + int cat_file(char *src, char *dst) { int sfd; @@ -368,19 +217,7 @@ int cat_file(char *src, char *dst) LOGINFO("copy (%s) to (%s)\n", src, dst); return 1; } -int get_file_end_pos(char *file) -{ - int pos = 0; - FILE *fp = NULL; - fp = fopen(file, "r+"); - retvm_if(fp == NULL, -1, "Failed to open %s\n", file); - fseek(fp, 0L, SEEK_END); - pos = ftell(fp); - if (fp) - fclose(fp); - return pos; -} static int _delete_dir_contents(DIR *d, const char *ignore) { int result = 0; @@ -434,6 +271,7 @@ static int _delete_dir_contents(DIR *d, const char *ignore) } return result; } + int delete_dir_contents(const char *pathname, int also_delete_dir, const char *ignore) diff --git a/crash-worker/util.h b/crash-worker/util.h index b03d425..2981a48 100644 --- a/crash-worker/util.h +++ b/crash-worker/util.h @@ -18,58 +18,11 @@ #define __DEF_UTIL_H__ #include "util_log.h" -/** - * @addtogroup CRASH_MANAGER - * @{ - */ - -/* - * @brief write the pid - * - * get a pid and write it to pidpath - * - * @param[in] pidpath pid file path - * @return 0 (always) - */ -extern int writepid(char *pidpath); - -/* - * @brief read the pid - * - * get a pid and write it to pidpath - * - * @param[in] pidpath pid file path - * @return pid : success, -1 : failed - */ -extern int readpid(char *pidpath); - -/* - * @brief MANAGERize function - * - * fork the process, kill the parent process - * and replace all the standard fds to /dev/null. - * - * @return 0 : success, -1 : fork() error - */ -extern int daemonize(void); - -/** - * @brief function to run a process - * - * fork the process, and run the other process if it is child. - * - * @return new process pid on success, -1 on error - */ -extern int exec_process(char *name); extern int system_command(char *command); extern int system_command_with_timeout(int timeout_seconds, char *command); -extern int run_command(int timeout_seconds, const char *command, ...); - -extern char *fgets_fd(char *s, int n, int fd); - extern int fprintf_fd(int fd, const char *fmt, ...); extern int file_exist(const char *file); |