Beispiel #1
0
Datei: run.c Projekt: 1587/ltp
/**
 * Main program.  Returns 1 if all programs run successfully, 0 if
 * something failed and -1 if there was an error running programs.
 */
int main(int argc, char *argv[])
{
	int retcode;
	struct sigaction zig;
	pid_t pid;
	char *c;

	/* Check parameters */
	if (argc < 2) {
		fprintf(stderr, "Usage: %s test_prog\n", argv[0]);
		return 1;
	}

	if (argc > 2 && strcmp(argv[2], "--leader") == 0) {
		pounder_fprintf(stdout,
				"Logging this test output to %s/POUNDERLOG.\n",
				getenv("POUNDER_LOGDIR"));
		is_leader = 1;
		record_pid();
	}

	progname = argv[0];

	/* Set up signals */
	memset(&zig, 0x00, sizeof(zig));
	zig.sa_handler = jump_out;
	sigaction(SIGHUP, &zig, NULL);
	sigaction(SIGINT, &zig, NULL);
	sigaction(SIGTERM, &zig, NULL);

	if (is_directory(argv[1])) {
		retcode = process_dir(argv[1]);
	} else {
		if (is_executable(argv[1])) {
			c = rindex(argv[1], '/');
			c++;

			// Start the test
			pid = spawn_test(argv[1]);
			if (pid < 0) {
				perror("fork");
				retcode = -1;
				goto out;
			}
			// Track the test
			note_process(pid, argv[1]);
			if (wait_for_pids() == 0) {
				retcode = 1;
			} else {
				retcode = 0;
			}
		} else {
			pounder_fprintf(stderr,
					"%s: Not a directory or a test.\n",
					argv[1]);
			retcode = -1;
		}
	}

out:
	kill_daemons();
	wait_for_daemons();
	if (is_leader) {
		if (retcode == 0) {
			pounder_fprintf(stdout, "%s: %s.\n", argv[1], pass_msg);
		} else if (retcode < 0 || retcode == 255) {
			pounder_fprintf(stdout, "%s: %s with code %d.\n",
					argv[1], abort_msg, retcode);
		} else {
			pounder_fprintf(stdout, "%s: %s with code %d.\n",
					argv[1], fail_msg, retcode);
		}
		unlink(pidfile);
	}
	exit(retcode);
}
int main(int argc, char *argv[])
{
    char* cmd;

    if ((cmd = strrchr(argv[0],'/')) == NULL)
    {
        cmd = argv[0];
    }
    else
        cmd++;

    const char* server_name = "synctool";
    char path[BUFSIZE];
    char server_path[BUFSIZE];
    memset(path,0,BUFSIZE);
    memset(server_path,0,BUFSIZE);
    getcwd(path,BUFSIZE);
    strcat(path,"/");
    memcpy(server_path,path,BUFSIZE);
    strcat(server_path,server_name);

    memcpy(log_name,path,BUFSIZE);
    strcat(log_name,"daemon_log.txt");

    char now_time[40];
    get_time(now_time,40);
    write_log("synctool daemon start time : %s\n",now_time);

    /// 先判断守护进程是否已经运行
    /// 
    if(just_running(LOCKDAEM,NULL) == 0)
    {
        printf("the sync_daemon is already running !\n");
        return 0;
    }
    daemonize(cmd);

    signal(SIGTERM,sigterm_handle);

    /// 记录守护进程pid

    if(record_pid(LOCKDAEM) != 0)
    {
        printf("sync_daemon write pid failed !\n");
        return 0;
    }

    while(_running)
    {
        pid_t pid;
        if((pid = fork()) < 0)
        {
            write_log("fork error !\n");
        }
        else if(pid == 0)
        {
            if(record_pid(LOCKFILE) == 0)
            {
                if(execl(server_path,server_name,path,(char*)0) < 0)
                {
                    write_log(strerror(errno));
                    remove(LOCKFILE);
                }
            }
        }
        else
        {
            pid_t sub_pid = wait(NULL);
            if (sub_pid != pid)
            {
                write_log("wait synctool pid (%d) not equal fork return pid(%d)! \n",sub_pid,pid);
            }
            remove(LOCKFILE);
            write_log("restart synctool server !\n");
        }
    }
    return 0;
}