static void logd_stop(void) { long running_logd_pid = cl_read_pidfile(LOGD_PIDFILE); int err; if (running_logd_pid < 0) { fprintf(stderr, "ha_logd already stopped.\n"); cl_log(LOG_INFO, "ha_logd already stopped."); exit(LSB_EXIT_OK); } cl_log(LOG_DEBUG, "Stopping ha_logd with pid %ld", running_logd_pid); if (kill((pid_t)running_logd_pid, SIGTERM) >= 0) { /* Wait for the running logd to die */ cl_log(LOG_INFO, "Waiting for pid=%ld to exit", running_logd_pid); alarm(0); do { sleep(1); }while (IsRunning(running_logd_pid)); } else if (errno != ESRCH) { err = errno; cl_perror("Pid %ld not killed", running_logd_pid); exit((err == EPERM || err == EACCES) ? LSB_EXIT_EPERM : LSB_EXIT_GENERIC); } cl_log(LOG_INFO, "Pid %ld exited", running_logd_pid); exit(LSB_EXIT_OK); }
int init_stop(const char *pid_file) { long pid; int rc = LSB_EXIT_OK; if (pid_file == NULL) { quorum_log(LOG_ERR, "No pid file specified to kill process"); return LSB_EXIT_GENERIC; } pid = cl_read_pidfile(pid_file); if (pid > 0) { if (CL_KILL((pid_t)pid, SIGTERM) < 0) { rc = (errno == EPERM ? LSB_EXIT_EPERM : LSB_EXIT_GENERIC); fprintf(stderr, "Cannot kill pid %ld\n", pid); }else{ quorum_log(LOG_INFO, "Signal sent to pid=%ld," " waiting for process to exit", pid); while (CL_PID_EXISTS(pid)) { sleep(1); } } } return rc; }
int init_status(const char *pid_file, const char *client_name) { long pid = cl_read_pidfile(pid_file); if (pid > 0) { fprintf(stderr, "%s is running [pid: %ld]\n" , client_name, pid); return LSB_STATUS_OK; } fprintf(stderr, "%s is stopped.\n", client_name); return LSB_STATUS_STOPPED; }
/* * Notes: Since the work dir is changed to "/", the client name should include * pathname or it's located in the system PATH */ static void become_daemon(void) { int j; if (pidfile) { int runningpid; if ((runningpid=cl_read_pidfile(pidfile)) > 0) { cl_log(LOG_WARNING, "pidfile [%s] says we're already running as pid [%d]" , pidfile, runningpid); exit(LSB_EXIT_OK); } if (cl_lock_pidfile(pidfile) != 0) { cl_log(LOG_ERR, "Cannot create pidfile [%s]" , pidfile); exit(LSB_EXIT_GENERIC); } } #if 0 pid_t pid; pid = fork(); if (pid < 0) { cl_log(LOG_ERR, "cannot start daemon."); exit(LSB_EXIT_GENERIC); } else if (pid > 0) { exit(LSB_EXIT_OK); } #endif if (chdir("/") < 0) { cl_log(LOG_ERR, "cannot chroot to /."); exit(LSB_EXIT_GENERIC); } umask(022); setsid(); for (j=0; j < 3; ++j) { close(j); (void)open("/dev/null", j == 0 ? O_RDONLY : O_RDWR); } CL_IGNORE_SIG(SIGINT); CL_IGNORE_SIG(SIGHUP); G_main_add_SignalHandler(G_PRIORITY_DEFAULT, SIGTERM, cl_respawn_quit, NULL, NULL); }
static void logd_make_daemon(gboolean daemonize) { long pid; if (daemonize) { if (daemon(0,0)) { fprintf(stderr, "%s: could not start daemon\n" , cmdname); perror("fork"); exit(LSB_EXIT_GENERIC); } } if (cl_lock_pidfile(LOGD_PIDFILE) < 0 ){ pid = cl_read_pidfile(LOGD_PIDFILE); if (pid > 0) fprintf(stderr, "%s: already running [pid %ld].\n", cmdname, pid); else fprintf(stderr, "%s: problem creating pid file %s\n", cmdname, LOGD_PIDFILE); exit(LSB_EXIT_OK); } if (daemonize || !verbose){ cl_log_enable_stderr(FALSE); } if (daemonize){ mode_t mask; /* * Some sample umask calculations: * * logmode = 0644 * * (~0644)&0777 = 0133 * (0133 & ~0111) = 0022 * => umask will be 022 (the expected result) * * logmode = 0600 * (~0600)&0777 = 0177 * (0177 & ~0111) = 0066 */ mask = (mode_t)(((~logd_config.logmode) & 0777) & (~0111)); umask(mask); } }
/* main loop of the daemon*/ int init_start () { int ssock; struct sockaddr_in saddr; GIOChannel* sch; /* register pid */ if (cl_lock_pidfile(PID_FILE) < 0) { quorum_log(LOG_ERR, "already running: [pid %d]." , cl_read_pidfile(PID_FILE)); quorum_log(LOG_ERR, "Startup aborted (already running)." "Shutting down."); exit(100); } register_pid(FALSE, sigterm_action); /* enable coredumps */ quorum_log(LOG_DEBUG, "Enabling coredumps"); cl_cdtocoredir(); cl_enable_coredumps(TRUE); cl_set_all_coredump_signal_handlers(); /* initialize gnutls */ initialize_tls_global(); /* enable dynamic up/down debug level */ G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR1, sig_handler, NULL, NULL); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR2, sig_handler, NULL, NULL); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGHUP, sig_handler, NULL, NULL); /* create the mainloop */ mainloop = g_main_new(FALSE); /* create the protocal table */ protocols = g_hash_table_new(g_str_hash, g_str_equal); /* create server socket */ ssock = socket(AF_INET, SOCK_STREAM, 0); if (ssock == -1) { quorum_log(LOG_ERR, "Can not create server socket." "Shutting down."); exit(100); } /* bind server socket*/ memset(&saddr, '\0', sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(PORT); if (bind(ssock, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) { quorum_log(LOG_ERR, "Can not bind server socket." "Shutting down."); exit(100); } if (listen(ssock, 10) == -1) { quorum_log(LOG_ERR, "Can not start listen." "Shutting down."); exit(100); } /* create source for server socket and add to the mainloop */ sch = g_io_channel_unix_new(ssock); g_io_add_watch(sch, G_IO_IN|G_IO_ERR|G_IO_HUP, on_listen, NULL); /* run the mainloop */ quorum_log(LOG_DEBUG, "main: run the loop..."); quorum_log(LOG_INFO, "Started."); g_main_run(mainloop); /* exit, clean the pid file */ if (cl_unlock_pidfile(PID_FILE) == 0) { quorum_log(LOG_DEBUG, "[%s] stopped", QUORUMD); } return 0; }
int main(int argc, char** argv, char** envp) { int c; gboolean daemonize = FALSE; gboolean stop_logd = FALSE; gboolean ask_status= FALSE; const char* cfgfile = NULL; pid_t pid; cmdname = argv[0]; while ((c = getopt(argc, argv, "c:dksvh")) != -1){ switch(c){ case 'd': /* daemonize */ daemonize = TRUE; break; case 'k': /* stop */ stop_logd = TRUE; break; case 's': /* status */ ask_status = TRUE; break; case 'c': /* config file*/ cfgfile = optarg; break; case 'v': verbose = TRUE; break; case 'h': /*help message */ default: usage(); exit(1); } } set_ipc_time_debug_flag(FALSE); cl_log_set_uselogd(FALSE); if (!cfgfile && access(DEFAULT_CFG_FILE, F_OK) == 0) { cfgfile = DEFAULT_CFG_FILE; } /* default one set to "logd" * by setting facility, we enable syslog */ cl_log_enable_stderr(TRUE); cl_log_set_entity(logd_config.entity); cl_log_set_facility(logd_config.log_facility); if (ask_status){ long pid; if( (pid = cl_read_pidfile(LOGD_PIDFILE)) > 0 ){ printf("logging daemon is running [pid = %ld].\n", pid); exit(LSB_EXIT_OK); }else{ if (pid == - LSB_STATUS_VAR_PID) { printf("logging daemon is stopped: %s exists.\n" , LOGD_PIDFILE); }else{ printf("logging daemon is stopped.\n"); } } exit(-pid); } if (stop_logd){ logd_stop(); exit(LSB_EXIT_OK); } logd_make_daemon(daemonize); if (ipc_channel_pair(chanspair) != IPC_OK){ cl_perror("cannot create channel pair IPC"); return -1; } if (cfgfile && !parse_config(cfgfile)) { FILE* f; if ((f = fopen(cfgfile, "r")) != NULL){ fclose(f); cl_log(LOG_ERR, "Config file [%s] is incorrect." , cfgfile); exit(LSB_EXIT_NOTCONFIGED); } } if (strlen(logd_config.debugfile) > 0) { cl_log_set_debugfile(logd_config.debugfile); } if (strlen(logd_config.logfile) > 0) { cl_log_set_logfile(logd_config.logfile); } cl_log_set_syslogprefix(logd_config.syslogprefix); cl_log_set_entity(logd_config.entity); cl_log_set_facility(logd_config.log_facility); cl_log(LOG_INFO, "logd started with %s.", cfgfile ? cfgfile : "default configuration"); if (cl_enable_coredumps(TRUE) < 0){ cl_log(LOG_ERR, "enabling core dump failed"); } cl_cdtocoredir(); chanspair[WRITE_PROC_CHAN]->ops->set_recv_qlen(chanspair[WRITE_PROC_CHAN], LOGD_QUEUE_LEN); chanspair[READ_PROC_CHAN]->ops->set_send_qlen(chanspair[READ_PROC_CHAN], LOGD_QUEUE_LEN); if (init_set_proc_title(argc, argv, envp) < 0) { cl_log(LOG_ERR, "Allocation of proc title failed."); return -1; } switch(pid = fork()){ case -1: cl_perror("Can't fork child process!"); return -1; case 0: /*child*/ cl_log_use_buffered_io(1); set_proc_title("ha_logd: write process"); write_msg_process(chanspair[WRITE_PROC_CHAN]); break; default: /*parent*/ set_proc_title("ha_logd: read process"); write_process_pid = pid; /* we don't expect to log anything in the parent. */ cl_log_close_log_files(); read_msg_process(chanspair[READ_PROC_CHAN]); break; } return 0; }
int main(int argc, char *argv[]) { int rc; int retval = 0; const char* conf_file = CONFIG_FILE; int pid; cl_cdtocoredir(); if(argc == 2){ conf_file = argv[1]; }else if(argc > 2){ printf("Usage: %s [config_file]\n", cmdname); exit(LSB_EXIT_NOTCONFIGED); } cl_log_enable_stderr(TRUE); pid = cl_read_pidfile(PIDFILE); if (pid > 0 && pid != getpid()){ cl_log(LOG_INFO, "recovermgrd is already running[%d]", pid); return 0; } cl_log_set_entity(argv[0]); cl_log_set_facility(LOG_USER); cl_log(LOG_INFO, "Starting %s", argv[0]); signal(SIGCHLD, sigchld_handler); if(parseConfigFile(conf_file) == FALSE){ exit(LSB_EXIT_NOTCONFIGED); }; /* make self a daemon */ #ifndef DEBUG daemon(0,0); #else printf("Debug mode -- non daemon\n"); #endif if (cl_lock_pidfile(PIDFILE) < 0){ cl_log(LOG_INFO, "recoverymgrd is already running[%d]", cl_read_pidfile(PIDFILE)); return 0; } /* register with apphbd as a client and send pings */ retval = register_hb(); if (0 != retval) { cl_perror("Error registering -- is apphbd running??"); exit(retval); } create_connection(); /* unregister and shutdown */ rc = apphb_unregister(); if (rc < 0) { cl_perror("apphb_unregister failure"); exit(3); } cl_unlock_pidfile(PIDFILE); return 0; }