Example #1
0
int main(int argc, char **argv)
{
    int code = main_process(argc, argv);
    if (code != 0) {
        char errmsg[256];
        switch (code) {
            case 10: strcpy(errmsg, "FILE_ERROR"); break;
            default: strcpy(errmsg, "UNKNOWN_ERROR");
        }
        printf("\nERROR: Program exited with code: %d [%s]", code, errmsg);
    }
    return code;
}
Example #2
0
/**
 * The main routine for the smfd daemon.
 * @param argc
 * @param argv
 * 
 * @return int
 */
int main(int argc, char *argv[])
{
	/* Determine how this process was started, by NID or AMF */
	if (getenv("SA_AMF_COMPONENT_NAME") != NULL) {
		/* AMF start */
		smfd_cb->nid_started = 0;
	} else {
		/* NID start */
		smfd_cb->nid_started = 1;
	}

	daemonize(argc, argv);

	if (ncs_agents_startup() != NCSCC_RC_SUCCESS) {
		LOG_ER("ncs_agents_startup failed");
		goto done;
	}

	if (initialize_smfd() != NCSCC_RC_SUCCESS) {
		LOG_ER("initialize_smfd failed");
		goto done;
	}

	if (signal(SIGUSR1, usr1_sig_handler) == SIG_ERR) {
		LOG_ER("signal SIGUSR1 failed");
		goto done;
	}

	if (smfd_cb->nid_started == 1) {
		if (nid_notify("SMFD", NCSCC_RC_SUCCESS, NULL) !=
		    NCSCC_RC_SUCCESS)
			LOG_ER("nid_notify failed");
	}

	main_process();

	exit(EXIT_SUCCESS);

 done:
	if (smfd_cb->nid_started == 1) {
		if (nid_notify("SMFD", NCSCC_RC_FAILURE, NULL) !=
		    NCSCC_RC_SUCCESS)
			LOG_ER("nid_notify failed");
	}

	LOG_ER("SMFD failed, exiting...");
	exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
	pid_t pid;

	// initialize shared memory for IPCs
	shared_memory();

	pid = fork();
	switch(pid){
		case -1:
			// process creation failed
			die("process creation failed");
		case 0:
			// input process
			pid = fork();
			switch(pid){
				case -1:
					// process creation failed
					die("process creation failed");
				case 0:
					// event key process (input)
					return eventkey_process();
				default:
					// input process
					return input_process();
			}
		default:
			pid = fork();
			switch(pid){
				case -1:
					// process creation failed
					die("process creation failed");
				case 0:
					// output process
					return output_process();
				default:
					// main process
					main_process();
			}
	}

	free_shared();

	return 0;
}
Example #4
0
/*
 * 必要な関数二つめ。フレームごとに呼ばれるよ!
 *
 */
__declspec(dllexport) void ExtProcess(void *ctx,void* dummy, vhext_frame *pict){
		ContextInfo *ci = (ContextInfo *) ctx;
		FILE* log = ci->log;
		if(!ci->data.typeNicovideoE){
			pict = (vhext_frame*)dummy;
		}

	/* Note:
	 * Saccubus 1.22以降の拡張vhookフィルタでは、RGB24フォーマットでのみ
	 * 画像が提供されます。
	 */

		//SDLのサーフェイスに変換
		SDL_Surface* surf = SDL_CreateRGBSurfaceFrom(pict->data,
												pict->w,pict->h,24,pict->linesize,
											#if SDL_BYTEORDER == SDL_BIG_ENDIAN
												0xff000000,
												0x00ff0000,
												0x0000ff00,
											#else
												0x000000ff,
												0x0000ff00,
												0x00ff0000,
											#endif
												0x00000000
												);
	//フィルタ
	int now_vpos = (pict->pts * VPOS_FACTOR);
	if(!main_process(&ci->data,surf,now_vpos)){
		fputs("[framehook/process]failed to process.\n",log);
		fflush(log);
		exit(1);
	}
	//サーフェイス開放
	SDL_FreeSurface(surf);
	fflush(log);
}
Example #5
0
int main(int argc, char **argv)
{
    argv0 = argv[0];
    const char *config_path   = DEFAULT_CONFIG_PATH;
    const char *python_pkgdir = DEFAULT_DISPATCH_PYTHON_DIR;
    const char *pidfile = 0;
    const char *user    = 0;
    bool        daemon_mode = false;

    static struct option long_options[] = {
    {"config",  required_argument, 0, 'c'},
    {"include", required_argument, 0, 'I'},
    {"daemon",  no_argument,       0, 'd'},
    {"pidfile", required_argument, 0, 'P'},
    {"user",    required_argument, 0, 'U'},
    {"help",    no_argument,       0, 'h'},
    {"version", no_argument,       0, 'v'},
    {0,         0,                 0,  0}
    };

    while (1) {
        int c = getopt_long(argc, argv, "c:I:dP:U:h:v", long_options, 0);
        if (c == -1)
            break;

        switch (c) {
        case 'c' :
            config_path = optarg;
            break;

        case 'I' :
            python_pkgdir = optarg;
            break;

        case 'd' :
            daemon_mode = true;
            break;

        case 'P' :
            pidfile = optarg;
            break;

        case 'U' :
            user = optarg;
            break;

        case 'h' :
            usage(argv);
            exit(0);

        case 'v' :
            fprintf(stdout, "Qpid Dispatch Router %s\n", QPID_DISPATCH_VERSION);
            exit(0);

        case '?' :
            usage(argv);
            exit(1);
        }
    }
    if (optind < argc) {
        fprintf(stderr, "Unexpected arguments:");
        for (; optind < argc; ++optind) fprintf(stderr, " %s", argv[optind]);
        fprintf(stderr, "\n\n");
        usage(argv);
        exit(1);
    }

    if (daemon_mode)
        daemon_process(config_path, python_pkgdir, pidfile, user);
    else
        main_process(config_path, python_pkgdir, 2);

    return 0;
}
Example #6
0
static void daemon_process(const char *config_path, const char *python_pkgdir,
                           const char *pidfile, const char *user)
{
    int pipefd[2];

    //
    // This daemonization process is based on that outlined in the
    // "daemon" manpage from Linux.
    //

    //
    // Create an unnamed pipe for communication from the daemon to the main process
    //
    if (pipe(pipefd) < 0) {
        perror("Error creating inter-process pipe");
        exit(1);
    }

    //
    // First fork
    //
    pid_t pid = fork();
    if (pid == 0) {
        //
        // Child Process
        //

        //
        // Detach any terminals and create an independent session
        //
        if (setsid() < 0) fail(pipefd[1], "Cannot start a new session");
        //
        // Second fork
        //
        pid_t pid2 = fork();
        if (pid2 == 0) {
            close(pipefd[0]); // Close read end.

            //
            // Assign stdin, stdout, and stderr to /dev/null
            //
            close(2);
            close(1);
            close(0);
            int fd = open("/dev/null", O_RDWR);
            if (fd != 0) fail(pipefd[1], "Can't redirect stdin to /dev/null");
            if (dup(fd) < 0) fail(pipefd[1], "Can't redirect stdout to /dev/null");
            if (dup(fd) < 0) fail(pipefd[1], "Can't redirect stderr /dev/null");

            //
            // Set the umask to 0
            //
            umask(0);

            //
            // Set the current directory to "/" to avoid blocking
            // mount points
            //
            if (chdir("/") < 0) fail(pipefd[1], "Can't chdir /");

            //
            // If a pidfile was provided, write the daemon pid there.
            //
            if (pidfile) {
                FILE *pf = fopen(pidfile, "w");
                if (pf == 0) fail(pipefd[1], "Can't write pidfile %s", pidfile);
                fprintf(pf, "%d\n", getpid());
                fclose(pf);
            }

            //
            // If a user was provided, drop privileges to the user's
            // privilege level.
            //
            if (user) {
                struct passwd *pwd = getpwnam(user);
                if (pwd == 0) fail(pipefd[1], "Can't look up user %s", user);
                if (setuid(pwd->pw_uid) < 0) fail(pipefd[1], "Can't set user ID for user %s, errno=%d", user, errno);
                //if (setgid(pwd->pw_gid) < 0) fail(pipefd[1], "Can't set group ID for user %s, errno=%d", user, errno);
            }

            main_process(config_path, python_pkgdir, pipefd[1]);
        } else
            //
            // Exit first child
            //
            exit(0);
    } else {
        //
        // Parent Process
        // Wait for a success signal ('0') from the daemon process.
        // If we get success, exit with 0.  Otherwise, exit with 1.
        //
        close(pipefd[1]); // Close write end.
        char result[256];
        memset(result, 0, sizeof(result));
        if (read(pipefd[0], &result, sizeof(result)-1) < 0) {
            perror("Error reading inter-process pipe");
            exit(1);
        }

        if (strcmp(result, "ok") == 0)
            exit(0);
        fprintf(stderr, "%s", result);
        exit(1);
    }
}
Example #7
0
/**
 * The main function.
 * @param argc
 * Number of arguments.
 * @param argv
 * Arguments.
 * @return
 * 0.
 */
int main(int argc, char **argv)
{
#ifdef WIN32
    /* Open all files in binary mode by default. */
    _set_fmode(_O_BINARY);
#endif

    init(argc, argv);
    memset(&marker, 0, sizeof(struct obj));

    if (settings.plugin_unit_tests) {
        LOG(INFO, "Running plugin unit tests...");
        object *activator = player_get_dummy(PLAYER_TESTING_NAME1, NULL);
        object *me = player_get_dummy(PLAYER_TESTING_NAME2, NULL);
        trigger_unit_event(activator, me);

        if (!settings.unit_tests) {
            cleanup();
            exit(0);
        }
    }

    if (settings.unit_tests) {
#ifdef HAVE_CHECK
        LOG(INFO, "Running unit tests...");
        cleanup();
        check_main(argc, argv);
        exit(0);
#else
        LOG(ERROR, "Unit tests have not been compiled, aborting.");
        exit(1);
#endif
    }

    atexit(cleanup);

    if (settings.world_maker) {
#ifdef HAVE_WORLD_MAKER
        LOG(INFO, "Running the world maker...");
        world_maker();
        exit(0);
#else
        LOG(ERROR, "Cannot run world maker; server was not compiled with libgd, exiting...");
        exit(1);
#endif
    }

    if (!settings.no_console) {
        console_start_thread();
    }

    process_delay = 0;

    LOG(INFO, "Server ready. Waiting for connections...");

    for (; ; ) {
        if (unlikely(shutdown_timer_check())) {
            break;
        }

        console_command_handle();
        socket_server_process();

        if (++process_delay >= max_time_multiplier) {
            process_delay = 0;
            main_process();
        }

        socket_server_post_process();

        /* Sleep proper amount of time before next tick */
        sleep_delta();
    }

    server_shutdown();

    return 0;
}
Example #8
0
static void daemon_process(const char *ns_pid, const char *pidfile, const char *address, const char *user)
{
    int pipefd[2];

    //
    // This daemonization process is based on that outlined in the
    // "daemon" manpage from Linux.
    //

    //
    // Create an unnamed pipe for communication from the daemon to the main process
    //
    if (pipe(pipefd) < 0) {
        perror("Error creating inter-process pipe");
        exit(1);
    }

    //
    // First fork
    //
    pid_t pid = fork();
    if (pid == 0) {
        //
        // Child Process
        //

        //
        // Detach any terminals and create an independent session
        //
        if (setsid() < 0) {
            write(pipefd[1], "1", 1);
            exit(0);
        }

        //
        // Second fork
        //
        pid_t pid2 = fork();
        if (pid2 == 0) {
            close(pipefd[0]); // Close read end.

            //
            // Assign stdin, stdout, and stderr to /dev/null
            //
            close(2);
            close(1);
            close(0);
            int fd = open("/dev/null", O_RDWR);
            if (fd != 0) {
                write(pipefd[1], "2", 1);
                exit(0);
            }
            if (dup(fd) < 0) {
                write(pipefd[1], "3", 1);
                exit(0);
            }
            if (dup(fd) < 0) {
                write(pipefd[1], "4", 1);
                exit(0);
            }

            //
            // Set the umask to 0
            //
            if (umask(0) < 0) {
                write(pipefd[1], "5", 1);
                exit(0);
            }

            //
            // Set the current directory to "/" to avoid blocking
            // mount points
            //
            if (chdir("/") < 0) {
                write(pipefd[1], "6", 1);
                exit(0);
            }

            //
            // If a pidfile was provided, write the daemon pid there.
            //
            if (pidfile) {
                FILE *pf = fopen(pidfile, "w");
                if (pf == 0) {
                    write(pipefd[1], "7", 1);
                    exit(0);
                }
                fprintf(pf, "%d\n", getpid());
                fclose(pf);
            }

            //
            // If a user was provided, drop privileges to the user's
            // privilege level.
            //
            if (user) {
                struct passwd *pwd = getpwnam(user);
                if (pwd == 0) {
                    write(pipefd[1], "8", 1);
                    exit(0);
                }
                if (setuid(pwd->pw_uid) < 0) {
                    write(pipefd[1], "9", 1);
                    exit(0);
                }
                if (setgid(pwd->pw_gid) < 0) {
                    write(pipefd[1], "A", 1);
                    exit(0);
                }
            }

            main_process(ns_pid, address, pipefd[1]);
        } else
            //
            // Exit first child
            //
            exit(0);
    } else {
        //
        // Parent Process
        // Wait for a success signal ('0') from the daemon process.
        // If we get success, exit with 0.  Otherwise, exit with 1.
        //
        char code;
        close(pipefd[1]); // Close write end.
        if (read(pipefd[0], &code, 1) < 0) {
            perror("Error reading inter-process pipe");
            exit(1);
        }

        if (code == '0')
            exit(0);
        fprintf(stderr, "Error occurred during daemon initialization, please see logs.  [code=%c]\n", code);
        exit(1);
    }
}
Example #9
0
int main(int argc, char **argv)
{
    const char *address = "127.0.0.1:amqp";
    const char *ns_pid  = 0;
    const char *pidfile = 0;
    const char *user    = 0;
    bool        daemon_mode = false;
    
    static struct option long_options[] = {
    {"daemon",  no_argument,       0, 'd'},
    {"pidfile", required_argument, 0, 'P'},
    {"user",    required_argument, 0, 'U'},
    {"help",    no_argument,       0, 'h'},
    {0,         0,                 0,  0}
    };

   
    while (1) {
        int c = getopt_long(argc, argv, "a:dP:U:h", long_options, 0);
        if (c == -1)
            break;

        switch (c) {
        case 'a' :
            address = optarg;
            break;
            
        case 'd' :
            daemon_mode = true;
            break;

        case 'P' :
            pidfile = optarg;
            break;

        case 'U' :
            user = optarg;
            break;

        case 'h' :
            printf("Usage: %s [OPTIONS]\n\n", argv[0]);
            printf("  -a, --address              Message bus connection address\n");
            printf("  -d, --daemon               Run process as a SysV-style daemon\n");
            printf("  -P, --pidfile              If daemon, the file for the stored daemon pid\n");
            printf("  -U, --user                 If daemon, the username to run as\n");
            printf("  -h, --help                 Print this help\n");
            exit(0);

        case '?' :
            exit(1);
        }
    }
    
    if (daemon_mode)
        daemon_process(ns_pid, pidfile, address, user);
    else
        main_process(ns_pid, address, -1);

    return 0;
    
}
Example #10
0
static void daemon_process(const char *config_path, const char *python_pkgdir, bool test_hooks,
                           const char *pidfile, const char *user)
{
    int pipefd[2];

    //
    // This daemonization process is based on that outlined in the
    // "daemon" manpage from Linux.
    //

    //
    // Create an unnamed pipe for communication from the daemon to the main process
    //
    if (pipe(pipefd) < 0) {
        perror("Error creating inter-process pipe");
        exit(1);
    }

    //
    // First fork
    //
    pid_t pid = fork();
    if (pid == 0) {
        //
        // Child Process
        //

        //
        // Detach any terminals and create an independent session
        //
        if (setsid() < 0) fail(pipefd[1], "Cannot start a new session");
        //
        // Second fork
        //
        pid_t pid2 = fork();
        if (pid2 == 0) {
            close(pipefd[0]); // Close read end.

            //
            // Assign stdin, stdout, and stderr to /dev/null
            //
            close(2);
            close(1);
            close(0);
            int fd = open("/dev/null", O_RDWR);
            if (fd != 0) fail(pipefd[1], "Can't redirect stdin to /dev/null");
            if (dup(fd) < 0) fail(pipefd[1], "Can't redirect stdout to /dev/null");
            if (dup(fd) < 0) fail(pipefd[1], "Can't redirect stderr /dev/null");

            //
            // Set the umask to 0
            //
            umask(0);


            //
            // If config path is not represented by its full path, then
            // save current path before changing to /
            //
            char *config_path_full = NULL;
            if (strncmp("/", config_path, 1)) {
                char *cur_path = NULL;
                size_t path_size = 256;
                int getcwd_error = 0;
                cur_path = (char *) calloc(path_size, sizeof(char));
                errno = 0;

                while (getcwd(cur_path, path_size) == NULL) {
                    free(cur_path);
                    if ( errno != ERANGE ) {
                        // If unable to get current directory
                        getcwd_error = 1;
                        break;
                    }
                    // If current path does not fit, allocate more memory
                    path_size += 256;
                    cur_path = (char *) calloc(path_size, sizeof(char));
                    errno = 0;
                }

                // Populating fully qualified config file name
                if (!getcwd_error) {
                    size_t cpf_len = path_size + strlen(config_path) + 1;
                    config_path_full = calloc(cpf_len, sizeof(char));
                    snprintf(config_path_full, cpf_len, "%s%s%s",
                             cur_path,
                             !strcmp("/", cur_path)? "":"/",
                             config_path);

                    // Releasing temporary path variable
                    memset(cur_path, 0, path_size * sizeof(char));
                    free(cur_path);
                }
            }

            //
            // Set the current directory to "/" to avoid blocking
            // mount points
            //
            if (chdir("/") < 0) fail(pipefd[1], "Can't chdir /");

            //
            // If a pidfile was provided, write the daemon pid there.
            //
            if (pidfile) {
                FILE *pf = fopen(pidfile, "w");
                if (pf == 0) fail(pipefd[1], "Can't write pidfile %s", pidfile);
                fprintf(pf, "%d\n", getpid());
                fclose(pf);
            }

            //
            // If a user was provided, drop privileges to the user's
            // privilege level.
            //
            if (user) {
                struct passwd *pwd = getpwnam(user);
                if (pwd == 0) fail(pipefd[1], "Can't look up user %s", user);
                if (setuid(pwd->pw_uid) < 0) fail(pipefd[1], "Can't set user ID for user %s, errno=%d", user, errno);
                //if (setgid(pwd->pw_gid) < 0) fail(pipefd[1], "Can't set group ID for user %s, errno=%d", user, errno);
            }

            main_process((config_path_full ? config_path_full : config_path), python_pkgdir, test_hooks, pipefd[1]);

            free(config_path_full);
        } else
            //
            // Exit first child
            //
            exit(0);
    } else {
        //
        // Parent Process
        // Wait for a success signal ('0') from the daemon process.
        // If we get success, exit with 0.  Otherwise, exit with 1.
        //
        close(pipefd[1]); // Close write end.
        char result[256];
        memset(result, 0, sizeof(result));
        if (read(pipefd[0], &result, sizeof(result)-1) < 0) {
            perror("Error reading inter-process pipe");
            exit(1);
        }

        if (strcmp(result, "ok") == 0)
            exit(0);
        fprintf(stderr, "%s", result);
        exit(1);
    }
}