int test(int argc,char **argv) { os_argv = argv; if (0 != init_setproctitle()) { printf("Fail to do init_setproctitle\n"); } /*here use test: as mark for this program */ setproctitle("test: master process"); int i=0; pid_t pid; for (i=0; i<4; i++) { if ((pid = fork()) == -1) { perror("Failed to fork process.\n"); exit(1); } else if (pid == 0) { /* child process */ char buf[100] = {0}; printf("test: fork process %d\n", i+1); snprintf(buf, sizeof(buf), "test: worker procee %d", i+1); setproctitle(buf); sleep(30); return(0); } } sleep(30); return 0; }
int main(int argc, char **argv) { int fd; int i; pid_t pid; sigset_t set; mainParseOptions(argc, argv); printf("yang test ... opt_send_signal:%d\r\n", opt_send_signal); if (-1 == opt_send_signal) if (checkRunningPid()) exit(1); enableCoredumps(); writePidFile(); save_argv(argc, argv); init_setproctitle(); init_signals(); sigemptyset(&set); printf("father pid1=%d\n",getpid()); int worker_processes = 3; start_worker_processes(worker_processes, PROCESS_RESPAWN); start_dispatcher_process(PROCESS_RESPAWN); printf("father pid2=%d\n", getpid()); setproctitle("nginx:master"); int live = 1; for (;;) { printf("father before suspend\n"); sigsuspend(&set); printf("father after suspend\n"); if (reap) { reap = 0; fprintf(stderr, "reap children\n"); live = reap_children(); } } return 0; }
int main (int argc, char *argv[]) { int i, pidfd; int blocked_signals[] = {SIGPIPE, 0}; int cc; char *oom_value; uint32_t slurmd_uid = 0; uint32_t curr_uid = 0; char time_stamp[256]; log_options_t lopts = LOG_OPTS_INITIALIZER; /* NOTE: logfile is NULL at this point */ log_init(argv[0], lopts, LOG_DAEMON, NULL); /* * Make sure we have no extra open files which * would be propagated to spawned tasks. */ cc = sysconf(_SC_OPEN_MAX); for (i = 3; i < cc; i++) close(i); /* * Drop supplementary groups. */ if (geteuid() == 0) { if (setgroups(0, NULL) != 0) { fatal("Failed to drop supplementary groups, " "setgroups: %m"); } } else { debug("Not running as root. Can't drop supplementary groups"); } /* * Create and set default values for the slurmd global * config variable "conf" */ conf = xmalloc(sizeof(slurmd_conf_t)); _init_conf(); conf->argv = &argv; conf->argc = &argc; if (_slurmd_init() < 0) { error( "slurmd initialization failed" ); fflush( NULL ); exit(1); } slurmd_uid = slurm_get_slurmd_user_id(); curr_uid = getuid(); if (curr_uid != slurmd_uid) { struct passwd *pw = NULL; char *slurmd_user = NULL; char *curr_user = NULL; /* since when you do a getpwuid you get a pointer to a * structure you have to do a xstrdup on the first * call or your information will just get over * written. This is a memory leak, but a fatal is * called right after so it isn't that big of a deal. */ if ((pw=getpwuid(slurmd_uid))) slurmd_user = xstrdup(pw->pw_name); if ((pw=getpwuid(curr_uid))) curr_user = pw->pw_name; fatal("You are running slurmd as something " "other than user %s(%d). If you want to " "run as this user add SlurmdUser=%s " "to the slurm.conf file.", slurmd_user, slurmd_uid, curr_user); } init_setproctitle(argc, argv); xsignal(SIGTERM, &_term_handler); xsignal(SIGINT, &_term_handler); xsignal(SIGHUP, &_hup_handler ); xsignal_block(blocked_signals); debug3("slurmd initialization successful"); /* * Become a daemon if desired. * Do not chdir("/") or close all fd's */ if (conf->daemonize) { if (daemon(1,1) == -1) error("Couldn't daemonize slurmd: %m"); } test_core_limit(); info("slurmd version %s started", SLURM_VERSION_STRING); debug3("finished daemonize"); if ((oom_value = getenv("SLURMD_OOM_ADJ"))) { i = atoi(oom_value); debug("Setting slurmd oom_adj to %d", i); set_oom_adj(i); } _kill_old_slurmd(); if (conf->mlock_pages) { /* * Call mlockall() if available to ensure slurmd * doesn't get swapped out */ #ifdef _POSIX_MEMLOCK if (mlockall (MCL_FUTURE | MCL_CURRENT) < 0) error ("failed to mlock() slurmd pages: %m"); #else error ("mlockall() system call does not appear to be available"); #endif /* _POSIX_MEMLOCK */ } /* * Restore any saved revoked credential information */ if (!conf->cleanstart && (_restore_cred_state(conf->vctx) < 0)) return SLURM_FAILURE; if (jobacct_gather_init() != SLURM_SUCCESS) fatal("Unable to initialize jobacct_gather"); if (job_container_init() < 0) fatal("Unable to initialize job_container plugin."); if (container_g_restore(conf->spooldir, !conf->cleanstart)) error("Unable to restore job_container state."); if (switch_g_node_init() < 0) fatal("Unable to initialize interconnect."); if (conf->cleanstart && switch_g_clear_node_state()) fatal("Unable to clear interconnect state."); switch_g_slurmd_init(); _create_msg_socket(); conf->pid = getpid(); /* This has to happen after daemon(), which closes all fd's, so we keep the write lock of the pidfile. */ pidfd = create_pidfile(conf->pidfile, 0); rfc2822_timestamp(time_stamp, sizeof(time_stamp)); info("%s started on %s", slurm_prog_name, time_stamp); _install_fork_handlers(); list_install_fork_handlers(); slurm_conf_install_fork_handlers(); /* * Initialize any plugins */ if (slurmd_plugstack_init()) fatal("failed to initialize slurmd_plugstack"); _spawn_registration_engine(); _msg_engine(); /* * Close fd here, otherwise we'll deadlock since create_pidfile() * flocks the pidfile. */ if (pidfd >= 0) /* valid pidfd, non-error */ (void) close(pidfd); /* Ignore errors */ if (unlink(conf->pidfile) < 0) error("Unable to remove pidfile `%s': %m", conf->pidfile); _wait_for_all_threads(120); _slurmd_fini(); _destroy_conf(); slurm_crypto_fini(); /* must be after _destroy_conf() */ info("Slurmd shutdown completing"); log_fini(); return 0; }
int main (int argc, char *argv[]) { slurm_addr_t *cli; slurm_addr_t *self; slurm_msg_t *msg; slurmd_job_t *job; int ngids; gid_t *gids; int rc = 0; if (process_cmdline (argc, argv) < 0) fatal ("Error in slurmstepd command line"); xsignal_block(slurmstepd_blocked_signals); conf = xmalloc(sizeof(*conf)); conf->argv = &argv; conf->argc = &argc; init_setproctitle(argc, argv); if (slurm_select_init(1) != SLURM_SUCCESS ) fatal( "failed to initialize node selection plugin" ); /* Receive job parameters from the slurmd */ _init_from_slurmd(STDIN_FILENO, argv, &cli, &self, &msg, &ngids, &gids); /* Fancy way of closing stdin that keeps STDIN_FILENO from being * allocated to any random file. The slurmd already opened /dev/null * on STDERR_FILENO for us. */ dup2(STDERR_FILENO, STDIN_FILENO); /* Create the slurmd_job_t, mostly from info in a launch_tasks_request_msg_t or a batch_job_launch_msg_t */ if(!(job = _step_setup(cli, self, msg))) { _send_fail_to_slurmd(STDOUT_FILENO); rc = SLURM_FAILURE; goto ending; } job->ngids = ngids; job->gids = gids; /* fork handlers cause mutexes on some global data structures to be re-initialized after the fork. */ list_install_fork_handlers(); slurm_conf_install_fork_handlers(); /* sets job->msg_handle and job->msgid */ if (msg_thr_create(job) == SLURM_ERROR) { _send_fail_to_slurmd(STDOUT_FILENO); rc = SLURM_FAILURE; goto ending; } _send_ok_to_slurmd(STDOUT_FILENO); /* Fancy way of closing stdout that keeps STDOUT_FILENO from being * allocated to any random file. The slurmd already opened /dev/null * on STDERR_FILENO for us. */ dup2(STDERR_FILENO, STDOUT_FILENO); /* This does most of the stdio setup, then launches all the tasks, and blocks until the step is complete */ rc = job_manager(job); /* signal the message thread to shutdown, and wait for it */ eio_signal_shutdown(job->msg_handle); pthread_join(job->msgid, NULL); if (job->batch) batch_finish(job, rc); /* sends batch complete message */ ending: #ifdef MEMORY_LEAK_DEBUG _step_cleanup(job, msg, rc); xfree(cli); xfree(self); xfree(conf->hostname); xfree(conf->block_map); xfree(conf->block_map_inv); xfree(conf->spooldir); xfree(conf->node_name); xfree(conf->node_topo_addr); xfree(conf->node_topo_pattern); xfree(conf->logfile); xfree(conf); #endif info("done with job"); return rc; }
int main (int argc, char **argv) { slurm_addr_t *cli; slurm_addr_t *self; slurm_msg_t *msg; stepd_step_rec_t *job; int ngids; gid_t *gids; int rc = 0; char *launch_params; if (_process_cmdline (argc, argv) < 0) fatal ("Error in slurmstepd command line"); xsignal_block(slurmstepd_blocked_signals); conf = xmalloc(sizeof(*conf)); conf->argv = &argv; conf->argc = &argc; init_setproctitle(argc, argv); if (slurm_select_init(1) != SLURM_SUCCESS ) fatal( "failed to initialize node selection plugin" ); if (slurm_auth_init(NULL) != SLURM_SUCCESS) fatal( "failed to initialize authentication plugin" ); /* Receive job parameters from the slurmd */ _init_from_slurmd(STDIN_FILENO, argv, &cli, &self, &msg, &ngids, &gids); /* Create the stepd_step_rec_t, mostly from info in a * launch_tasks_request_msg_t or a batch_job_launch_msg_t */ if (!(job = _step_setup(cli, self, msg))) { _send_fail_to_slurmd(STDOUT_FILENO); rc = SLURM_FAILURE; goto ending; } job->ngids = ngids; job->gids = gids; /* fork handlers cause mutexes on some global data structures * to be re-initialized after the fork. */ list_install_fork_handlers(); slurm_conf_install_fork_handlers(); /* sets job->msg_handle and job->msgid */ if (msg_thr_create(job) == SLURM_ERROR) { _send_fail_to_slurmd(STDOUT_FILENO); rc = SLURM_FAILURE; goto ending; } _send_ok_to_slurmd(STDOUT_FILENO); _got_ack_from_slurmd(STDIN_FILENO); /* Fancy way of closing stdin that keeps STDIN_FILENO from being * allocated to any random file. The slurmd already opened /dev/null * on STDERR_FILENO for us. */ dup2(STDERR_FILENO, STDIN_FILENO); /* Fancy way of closing stdout that keeps STDOUT_FILENO from being * allocated to any random file. The slurmd already opened /dev/null * on STDERR_FILENO for us. */ dup2(STDERR_FILENO, STDOUT_FILENO); /* slurmstepd is the only daemon that should survive upgrade. If it * had been swapped out before upgrade happened it could easily lead * to SIGBUS at any time after upgrade. Avoid that by locking it * in-memory. */ launch_params = slurm_get_launch_params(); if (launch_params && strstr(launch_params, "slurmstepd_memlock")) { #ifdef _POSIX_MEMLOCK int flags = MCL_CURRENT; if (strstr(launch_params, "slurmstepd_memlock_all")) flags |= MCL_FUTURE; if (mlockall(flags) < 0) info("failed to mlock() slurmstepd pages: %m"); else debug("slurmstepd locked in memory"); #else info("mlockall() system call does not appear to be available"); #endif } xfree(launch_params); /* This does most of the stdio setup, then launches all the tasks, * and blocks until the step is complete */ rc = job_manager(job); return stepd_cleanup(msg, job, cli, self, rc, 0); ending: return stepd_cleanup(msg, job, cli, self, rc, 1); }
int main(int argc, char *argv[], char *envp[]) { int c; int show_version = 0; const char *cmdopts = OPTION_COMMANDS; char SoftBotRoot[MAX_PATH_LEN], *tmp; // set gSoftBotRoot if ( realpath(argv[0], SoftBotRoot) ) { tmp = strstr( SoftBotRoot, "/bin/softbotd" ); if ( tmp ) { *tmp = '\0'; strcpy( gSoftBotRoot, SoftBotRoot ); } } info("server has started in [%s].", gSoftBotRoot ); #ifdef AIX5 { char *env = getenv("EXTSHM"); if (!env || strcmp(env, "ON")) { /* we need EXTSHM=ON in our environment to make the shmat and mmaps work */ setenv("EXTSHM", "ON", 1); fprintf(stderr, "re-executing self with EXTSHM=ON exported to fix AIX shmat problem.\n"); execv(argv[0], argv); fprintf(stderr, "re-execution failed - terminating.\n"); exit(1); } } #endif init_setproctitle(argc, argv, envp); set_static_modules(server_static_modules); /* getopt stuff */ opterr = 0; while ( (c = #ifdef HAVE_GETOPT_LONG getopt_long(argc, argv, cmdopts, opts, NULL) #elif HAVE_GETOPT getopt(argc, argv, cmdopts) #else # error at least one of getopt, getopt_long must exist. #endif /* HAVE_GETOPT_LONG */ ) != -1 ) { int i = 0; switch ( c ) { case 'n': nodaemon++; set_screen_log(); break; case 'p': clc_listen_port++; if (strcmp(optarg,"show")==0) { /* show using ports info */ clc_listen_port++; nodaemon++; log_setlevelstr("error"); clc_log_level++; break; } i=atoi(optarg); if (i<1024) gSoftBotListenPort += i; else gSoftBotListenPort = i; info("Setting gSoftBotListenPort to %d",gSoftBotListenPort); break; case 'g': if ( !optarg ) { error("Fatal: -g requires error log level argument."); exit(1); } if (log_setlevelstr(optarg) == SUCCESS) { clc_log_level++; } else { error("Invalid log level."); info(" arg for -d is like following"); for (i=0; gLogLevelStr[i]; i++) { info(" %s",gLogLevelStr[i]); } exit(1); } break; case 'm': if ( !optarg ) { error("Fatal: -m requires module name."); exit(1); } strncpy(debug_module, optarg, MAX_MODULE_NAME); debug_module[MAX_MODULE_NAME-1] = '\0'; /* nodaemon is on by default when debugging a module */ nodaemon++; set_screen_log(); debug("debugging module[%s]", debug_module); break; case 'u': if (unittest==0) { info("When doing unittest,"); info("always check that modules to be tested are loaded"); } nodaemon++; set_screen_log(); unittest++; break; case 'k': /* refer to hook.c, hook.h, modules.c */ debug_module_hooks++; break; case 'l': list_static_modules(stdout); exit(0); break; case 'c': if ( !optarg ) { error("Fatal: -c requires configuration path"); exit(1); } strncpy(mConfigFile, optarg, MAX_PATH_LEN); mConfigFile[MAX_PATH_LEN-1] = '\0'; debug("configuration file path set to %s",mConfigFile); break; case 't': check_config_syntax = 1; clc_log_level++; gLogLevel = LEVEL_INFO; set_screen_log(); // syntax checking 시 error를 화면으로.. printf("Checking syntax of configuration file\n"); fflush(stdout); break; case 'r': if ( !optarg ) { error("Fatal: -r requires server root path"); exit(1); } strncpy(gSoftBotRoot, optarg, MAX_PATH_LEN); gSoftBotRoot[MAX_PATH_LEN-1] = '\0'; clc_server_root++; debug("gSoftBotRoot is set to %s by -r option.",gSoftBotRoot); break; case 'v': show_version++; break; case 'h': show_usage(0); case '?': error("Unknown option: %c", (char)optopt); show_usage(1); } /* switch ( c ) */ } /* end of getopt stuff */ if ( show_version ) show_version_and_exit(); /* initializing */ /* we're only doing a syntax check of the configuration file. */ if ( check_config_syntax ) { int ret=FAIL; // TODO: is this enough? -aragorn debug("checking syntax"); debug("configuration file:%s",mConfigFile); load_static_modules(); ret = read_config(mConfigFile, NULL); printf("Syntax check completed.\n"); printf("Configuration module returned %d\n",ret); exit(0); } /* setuid, setgid stuff */ /* signal handler stuff */ set_signal_handlers(); /* resource limits */ if ( nodaemon == 0 ) detach(); /* RESTART:*/ /* set pid */ gRootPid = getpid(); server_main(); /* if ( restart ) goto RESTART; */ return 0; }