Blame envy24control/new_process.c

Packit 427e91
#include <sys wait.h="">
Packit 427e91
#include <signal.h>
Packit 427e91
#include <errno.h>
Packit 427e91
#include <unistd.h>
Packit 427e91
#include <sys stat.h="">
Packit 427e91
#include <sys types.h="">
Packit 427e91
Packit 427e91
#ifndef MAX_PARAM
Packit 427e91
#define MAX_PARAM 10
Packit 427e91
#endif
Packit 427e91
Packit 427e91
/*
Packit 427e91
 * start child process
Packit 427e91
 */
Packit 427e91
int new_process(char * const cmd_line[MAX_PARAM])
Packit 427e91
{
Packit 427e91
	int proc_status;
Packit 427e91
	pid_t pid;
Packit 427e91
	pid_t w;
Packit 427e91
	struct stat file_status;
Packit 427e91
Packit 427e91
	/* memory for storage of function pointers from the signal handling routines */
Packit 427e91
	void (*int_stat)();
Packit 427e91
	void (*quit_stat)();
Packit 427e91
	void (*usr2_stat)();
Packit 427e91
Packit 427e91
	/*
Packit 427e91
	 * check command file
Packit 427e91
	 */
Packit 427e91
Packit 427e91
	/* search file */
Packit 427e91
	if (stat(cmd_line[0], &file_status) < 0) {
Packit 427e91
		fprintf(stderr, "Cannot find program '%s'.\n", cmd_line[0]);
Packit 427e91
		fprintf(stderr, "You must specify path for '%s'.\n", cmd_line[0]);
Packit 427e91
		return -errno;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	proc_status = 0;
Packit 427e91
	/* check file status and permissions */
Packit 427e91
	if (file_status.st_mode & S_IFREG) {
Packit 427e91
		if (!(file_status.st_mode & S_IXOTH)) {
Packit 427e91
			if (!(file_status.st_mode & S_IXGRP)) {
Packit 427e91
				if (!(file_status.st_mode & S_IXUSR)) {
Packit 427e91
					proc_status = -EACCES;
Packit 427e91
				} else if (file_status.st_uid != getuid()) {
Packit 427e91
					proc_status = -EACCES;
Packit 427e91
				}
Packit 427e91
			} else if ((file_status.st_gid != getgid()) && (file_status.st_uid != getuid())) {
Packit 427e91
				proc_status = -EACCES;
Packit 427e91
			}
Packit 427e91
		}
Packit 427e91
	} else {
Packit 427e91
		proc_status = -EACCES;
Packit 427e91
	}
Packit 427e91
		
Packit 427e91
	if (proc_status != 0) {
Packit 427e91
		fprintf(stderr, "No permissions to execute program '%s'.\n", cmd_line[0]);
Packit 427e91
		return proc_status;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	if ( (pid = fork() ) == 0) {
Packit 427e91
		execv(cmd_line[0], cmd_line);
Packit 427e91
	}
Packit 427e91
Packit 427e91
	/* for waiting ingnoring special interrupts */
Packit 427e91
Packit 427e91
	int_stat = signal(SIGINT, SIG_IGN);
Packit 427e91
	quit_stat = signal(SIGQUIT, SIG_IGN);
Packit 427e91
	usr2_stat = signal(SIGUSR2, SIG_IGN);
Packit 427e91
Packit 427e91
	/* waiting for the end of the child process */
Packit 427e91
Packit 427e91
	while ( ( (w = wait(&proc_status)) != pid ) && (w != -1) )
Packit 427e91
		;
Packit 427e91
	if (w == -1) {
Packit 427e91
		proc_status = -errno;
Packit 427e91
	}
Packit 427e91
Packit 427e91
	/* restore pointers from signal handling routines */
Packit 427e91
Packit 427e91
	signal(SIGINT, int_stat);
Packit 427e91
	signal(SIGQUIT, quit_stat);
Packit 427e91
	signal(SIGUSR2, usr2_stat);
Packit 427e91
Packit 427e91
	return proc_status;
Packit 427e91
}