Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=217001ec84b5f8306dde1e1a47870e77e477b68e
commit 217001ec84b5f8306dde1e1a47870e77e477b68e Author: James Buren <[email protected]> Date: Fri Sep 7 02:36:02 2012 -0500 add function for executing shell commands diff --git a/src/local.h b/src/local.h index 01ebcf3..d853b0f 100644 --- a/src/local.h +++ b/src/local.h @@ -5,8 +5,10 @@ #include <stdio.h> #include <string.h> #include <unistd.h> +#include <fcntl.h> #include <sys/stat.h> #include <sys/time.h> +#include <sys/wait.h> #include <wchar.h> #include <errno.h> #include <limits.h> @@ -50,6 +52,7 @@ struct module extern bool mkdir_recurse(const char *path); extern bool size_to_string(char *s,size_t n,long long size,bool pad); extern int get_text_length(const char *s); +extern bool execute(const char *command,const char *root,pid_t *cpid); extern int get_text_screen_width(const char *s); extern bool get_text_screen_size(const char *text,int *width,int *height); extern bool get_button_screen_size(const char *text,int *width,int *height); diff --git a/src/utility.c b/src/utility.c index acfd65c..9bd19af 100644 --- a/src/utility.c +++ b/src/utility.c @@ -121,6 +121,67 @@ extern int get_text_length(const char *s) return l; } +extern bool execute(const char *command,const char *root,pid_t *cpid) +{ + pid_t pid = -1; + int status = 0; + + if(command == 0 || root == 0) + { + errno = EINVAL; + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + return false; + } + + fprintf(logfile,_("Attempting to execute command '%s' with root directory '%s'.\n"),command,root); + + if((pid = fork()) == -1) + { + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + return false; + } + + if(pid == 0) + { + int fd = open(LOGFILE,O_WRONLY|O_APPEND|O_CREAT,0644); + + if(fd == -1) + _exit(200); + + dup2(fd,STDOUT_FILENO); + + dup2(fd,STDERR_FILENO); + + close(fd); + + if(chroot(root) == -1) + _exit(210); + + if(chdir("/") == -1) + _exit(220); + + execl("/bin/sh","/bin/sh","-c",command,(void *) 0); + + _exit(230); + } + + if(cpid != 0) + { + *cpid = pid; + return true; + } + + if(waitpid(pid,&status,0) == -1 || !WIFEXITED(status)) + { + fprintf(logfile,"%s: %s\n",__func__,strerror(errno)); + return false; + } + + fprintf(logfile,_("Command '%s' which was executed with root directory '%s' has exitted with code '%d'.\n"),command,root,WEXITSTATUS(status)); + + return (WEXITSTATUS(status) == 0); +} + extern int get_text_screen_width(const char *s) { wchar_t wc = 0; _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
