#include "cupsd.h"
#include <grp.h>
#if defined(__APPLE__)
# include <libgen.h>
#endif
typedef struct
{
int pid;
char name[1];
} cupsd_proc_t;
static cups_array_t *process_array = NULL;
static int compare_procs(cupsd_proc_t *a, cupsd_proc_t *b);
int
cupsdEndProcess(int pid,
int force)
{
if (force)
return (kill(pid, SIGKILL));
else
return (kill(pid, SIGTERM));
}
const char *
cupsdFinishProcess(int pid,
char *name,
int namelen)
{
cupsd_proc_t key,
*proc;
key.pid = pid;
if ((proc = (cupsd_proc_t *)cupsArrayFind(process_array, &key)) != NULL)
{
strlcpy(name, proc->name, namelen);
cupsArrayRemove(process_array, proc);
free(proc);
return (name);
}
else
return ("unknown");
}
int
cupsdStartProcess(
const char *command,
char *argv[],
char *envp[],
int infd,
int outfd,
int errfd,
int backfd,
int sidefd,
int root,
int *pid)
{
cupsd_proc_t *proc;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
#if defined(__APPLE__)
char processPath[1024],
linkpath[1024];
int linkbytes;
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdStartProcess(\"%s\", %p, %p, %d, %d, %d)",
command, argv, envp, infd, outfd, errfd);
if (access(command, X_OK))
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to execute %s: %s", command,
strerror(errno));
*pid = 0;
return (0);
}
#if defined(__APPLE__)
if (envp)
{
if ((linkbytes = readlink(command, linkpath, sizeof(linkpath) - 1)) > 0)
{
linkpath[linkbytes] = '\0';
if (linkpath[0] == '/')
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
linkpath);
else
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
dirname((char *)command), linkpath);
}
else
snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", command);
envp[0] = processPath;
}
#endif
cupsdHoldSignals();
if ((*pid = fork()) == 0)
{
if (infd != 0)
{
close(0);
if (infd > 0)
dup(infd);
else
open("/dev/null", O_RDONLY);
}
if (outfd != 1)
{
close(1);
if (outfd > 0)
dup(outfd);
else
open("/dev/null", O_WRONLY);
}
if (errfd != 2)
{
close(2);
if (errfd > 0)
dup(errfd);
else
open("/dev/null", O_WRONLY);
}
if (backfd != 3)
{
close(3);
if (backfd > 0)
dup(backfd);
else
open("/dev/null", O_RDWR);
fcntl(3, F_SETFL, O_NDELAY);
}
if (sidefd != 4 && sidefd > 0)
{
close(4);
dup(sidefd);
fcntl(4, F_SETFL, O_NDELAY);
}
if (!root)
nice(FilterNice);
if (!root && !RunUser)
{
if (setgid(Group))
exit(errno);
if (setgroups(1, &Group))
exit(errno);
if (setuid(User))
exit(errno);
}
else
{
setgid(Group);
setgroups(1, &Group);
}
umask(077);
#ifdef HAVE_SIGSET
sigset(SIGTERM, SIG_DFL);
sigset(SIGCHLD, SIG_DFL);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_DFL;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
#else
signal(SIGTERM, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
#endif
cupsdReleaseSignals();
if (envp)
execve(command, argv, envp);
else
execv(command, argv);
perror(command);
exit(errno);
}
else if (*pid < 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork %s - %s.", command,
strerror(errno));
*pid = 0;
}
else
{
if (!process_array)
process_array = cupsArrayNew((cups_array_func_t)compare_procs, NULL);
if (process_array)
{
if ((proc = calloc(1, sizeof(cupsd_proc_t) + strlen(command))) != NULL)
{
proc->pid = *pid;
strcpy(proc->name, command);
cupsArrayAdd(process_array, proc);
}
}
}
cupsdReleaseSignals();
return (*pid);
}
static int
compare_procs(cupsd_proc_t *a,
cupsd_proc_t *b)
{
return (a->pid - b->pid);
}