static void read_msg_process(IPC_Channel* chan) { GHashTable* conn_cmd_attrs; IPC_WaitConnection* conn_cmd = NULL; char path[] = "path"; char socketpath[] = HA_LOGDAEMON_IPC; GMainLoop* mainloop; mainloop = g_main_new(FALSE); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM, logd_term_action,mainloop, NULL); conn_cmd_attrs = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(conn_cmd_attrs, path, socketpath); conn_cmd = ipc_wait_conn_constructor(IPC_ANYTYPE, conn_cmd_attrs); g_hash_table_destroy(conn_cmd_attrs); if (conn_cmd == NULL){ fprintf(stderr, "ERROR: create waiting connection failed"); exit(1); } /*Create a source to handle new connect rquests for command*/ G_main_add_IPC_WaitConnection( G_PRIORITY_HIGH, conn_cmd, NULL, FALSE , on_connect_cmd, chan, NULL); chan->ops->set_high_flow_callback(chan, logd_suspend_clients, NULL); chan->ops->set_low_flow_callback(chan, logd_resume_clients, NULL); chan->high_flow_mark = chan->send_queue->max_qlen; chan->low_flow_mark = (chan->send_queue->max_qlen*3)/4; G_main_add_IPC_Channel(G_PRIORITY_DEFAULT, chan, FALSE,NULL,NULL,NULL); G_main_add_SignalHandler(G_PRIORITY_DEFAULT, SIGHUP, logd_hup_action, mainloop, NULL); g_main_run(mainloop); return; }
void set_sigchld_proctrack(int priority, unsigned long maxdisptime) { GSIGSource* src = G_main_add_SignalHandler(priority, SIGCHLD , child_death_dispatch, NULL, NULL); G_main_setmaxdispatchdelay((GSource*) src, 100); G_main_setmaxdispatchtime((GSource*) src, maxdisptime); G_main_setdescription((GSource*)src, "SIGCHLD"); return; }
static void write_msg_process(IPC_Channel* readchan) { GMainLoop* mainloop; IPC_Channel* ch = readchan; mainloop = g_main_new(FALSE); G_main_add_IPC_Channel(G_PRIORITY_DEFAULT, ch, FALSE, direct_log, mainloop, NULL); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM, logd_term_write_action, mainloop, NULL); G_main_add_SignalHandler(G_PRIORITY_DEFAULT, SIGHUP, logd_hup_action, mainloop, NULL); g_main_run(mainloop); }
/* * 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 register_pid(gboolean do_fork, gboolean (*shutdown)(int nsig, gpointer userdata)) { int j; umask(022); for (j = 0; j < 3; ++j) { close(j); (void)open("/dev/null", j == 0 ? O_RDONLY : O_RDONLY); } CL_IGNORE_SIG(SIGINT); CL_IGNORE_SIG(SIGHUP); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGTERM , shutdown, NULL, NULL); cl_signal_set_interrupt(SIGTERM, 1); cl_signal_set_interrupt(SIGCHLD, 1); /* At least they are harmless, I think. ;-) */ cl_signal_set_interrupt(SIGINT, 0); cl_signal_set_interrupt(SIGHUP, 0); }
/* 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; }