int common_setup(const char* pidfilename, bool unsafe, const char* runas_user, const char* runas_group, bool daemonize) { if (pidfile_open(pidfilename) < 0) { crit("unable to write pidfile %s", pidfilename); return EXIT_FAILURE; } if (!unsafe && drop_privileges(runas_user, runas_group) < 0) { crit("unable to drop privileges"); return EXIT_FAILURE; } if (daemonize && daemon_detach() < 0) { crit("unable to fork"); return EXIT_FAILURE; } pidfile_refresh(); return EXIT_SUCCESS; }
int main( int argc, char* argv[ ] ) { int ret = EXIT_SUCCESS; int make_daemon = 1, daemon_flags = 0; bool force_stop = false; /////////////////////////////////////////////// // parse command line options int i; for ( i = 1; i < argc; i++ ) { if ( argv[i][0] != '-' ) break; if ( strcmp( argv[i], "--no-daemon" ) == 0 ) make_daemon = 0; else if ( strcmp( argv[i], "--force" ) == 0 ) { daemon_flags |= D_NOLOCKCHECK; force_stop = true; } else if ( strcmp( argv[i], "--no-lockfile" ) == 0 ) { daemon_flags |= D_NOLOCKFILE; } else { if ( strcmp( argv[i], "--help" ) != 0 && strcmp( argv[i], "-h" ) != 0 ) printf( "\nUnknown Option: %s\n", argv[i] ); print_usage(); exit(0); } } if ( i == argc ) { print_usage(); exit(0); } if ( i != argc - 1 ) { printf( "Error: Invalid Command\n\n" ); print_usage(); exit(0); } // parse command line commands if ( strcmp( argv[i], "start" ) != 0 ) { if ( strcmp( argv[i], "stop" ) == 0 ) { if ( !daemon_stop( force_stop ) ) exit( EXIT_FAILURE ); exit( EXIT_SUCCESS ); } else if ( strcmp( argv[i], "restart" ) == 0 ) { if ( !daemon_stop( force_stop ) ) exit( EXIT_FAILURE ); } else { printf( "Error: Unknown Command: %s\n\n", argv[i] ); print_usage(); exit(0); } } ///////////////////////////////////////////////// if ( make_daemon ) { if ( !daemon_init( daemon_flags ) ) { exit(0); } } zcnf_daemon_t* daemon_cnf = zcnf_daemon_load(); if ( !daemon_cnf ) { fprintf( stderr, "failed to load config.\n" ); exit( EXIT_FAILURE ); } // Trap SIGPIPE signal( SIGPIPE, signal_handler ); // call any needed library init functions website_init(); zs_init( daemon_cnf->ssl_cert_path, daemon_cnf->ssl_key_path ); memset( connections, 0, sizeof( connections ) ); for ( i = 0; i < daemon_cnf->n; i++ ) { int sockfd = zsocket( inet_addr( daemon_cnf->proxies[ i ].ip ), daemon_cnf->proxies[ i ].port, ZSOCK_LISTEN, insock_event_hdlr, false ); if ( sockfd == -1 ) { ret = EXIT_FAILURE; fprintf( stderr, "Proxy failed on %s:%d\n", daemon_cnf->proxies[ i ].ip, daemon_cnf->proxies[ i ].port ); goto quit; } zs_set_read( sockfd ); printf( " * started listening on %s:%d\n", daemon_cnf->proxies[ i ].ip, daemon_cnf->proxies[ i ].port ); } FL_SET( daemon_flags, D_KEEPSTDIO ); #if defined(DEBUG) FL_SET( daemon_flags, D_NOCD ); setlogmask( LOG_UPTO( LOG_DEBUG ) ); openlog( DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER ); #else setlogmask( LOG_UPTO( LOG_INFO ) ); openlog( DAEMON_NAME, LOG_CONS, LOG_USER ); #endif // daemonize if ( make_daemon ) { if ( !daemon_detach( daemon_flags ) ) { ret = EXIT_FAILURE; goto quit; } } #ifndef DEBUG daemon_redirect_stdio(); #endif syslog( LOG_INFO, "initialized." ); // starts a select() loop and calls // the associated file descriptor handlers // when they are ready to read/write do { do { msg_switch_select(); zs_select(); } while ( msg_switch_need_select() || zs_need_select() ); } while ( zfd_select(2) ); quit: // cleanup zcnf_daemon_free( daemon_cnf ); if ( ret == EXIT_FAILURE ) { syslog( LOG_INFO, "exit: failure" ); } else { syslog( LOG_INFO, "exit: success" ); } closelog(); return ret; }
int test_main(int argc, char **argv) { int i; int debug = 0; char * cmd = 0; printf("PID=%ld\n", GetCurrentProcessId()); for (i = 0; i < argc; i++) { printf("%d: \"%s\"\n", i, argv[i]); if (!strcmp(argv[i],"-d")) debug = 1; } if (argc > 1 && argv[argc-1][0] != '-') cmd = argv[argc-1]; daemon_signal(SIGINT, sig_handler); daemon_signal(SIGBREAK, sig_handler); daemon_signal(SIGTERM, sig_handler); daemon_signal(SIGHUP, sig_handler); daemon_signal(SIGUSR1, sig_handler); daemon_signal(SIGUSR2, sig_handler); atexit(test_exit); if (!debug) { printf("Preparing to detach...\n"); Sleep(2000); daemon_detach("test"); printf("Detached!\n"); } for (;;) { daemon_sleep(1); printf("."); fflush(stdout); if (caughtsig) { if (caughtsig == SIGUSR2) { debug ^= 1; if (debug) daemon_enable_console("Daemon[Debug]"); else daemon_disable_console(); } else if (caughtsig == SIGUSR1 && cmd) { char inpbuf[200], outbuf[1000]; int rc; strcpy(inpbuf, "Hello\nWorld!\n"); rc = daemon_spawn(cmd, inpbuf, strlen(inpbuf), outbuf, sizeof(outbuf)); if (!debug) daemon_enable_console("Command output"); printf("\"%s\" returns %d\n", cmd, rc); if (rc >= 0) printf("output:\n%s.\n", outbuf); fflush(stdout); if (!debug) { Sleep(10000); daemon_disable_console(); } } printf("[PID=%ld: Signal=%d]", GetCurrentProcessId(), caughtsig); fflush(stdout); if (caughtsig == SIGTERM || caughtsig == SIGBREAK) break; caughtsig = 0; } } printf("\nExiting on signal %d\n", caughtsig); return 0; }
int main (int argc, char *argv[]) { MateSettingsManager *manager; DBusGConnection *bus; gboolean res; GError *error; manager = NULL; if (!g_thread_supported ()) { g_thread_init (NULL); } mate_settings_profile_start (NULL); bindtextdomain (GETTEXT_PACKAGE, MATE_SETTINGS_LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); setlocale (LC_ALL, ""); parse_args (&argc, &argv); daemon_start (); g_type_init (); mate_settings_profile_start ("opening gtk display"); if (! gtk_init_check (NULL, NULL)) { g_warning ("Unable to initialize GTK+"); daemon_terminate_parent (); exit (EXIT_FAILURE); } mate_settings_profile_end ("opening gtk display"); daemon_detach (); g_log_set_default_handler (gsd_log_default_handler, NULL); bus = get_session_bus (); if (bus == NULL) { g_warning ("Could not get a connection to the bus"); goto out; } if (! bus_register (bus)) { goto out; } mate_settings_profile_start ("mate_settings_manager_new"); manager = mate_settings_manager_new (); mate_settings_profile_end ("mate_settings_manager_new"); if (manager == NULL) { g_warning ("Unable to register object"); goto out; } set_session_over_handler (bus, manager); /* If we aren't started by dbus then load the plugins automatically. Otherwise, wait for an Awake etc. */ if (g_getenv ("DBUS_STARTER_BUS_TYPE") == NULL) { error = NULL; if (mateconf_prefix != NULL) { res = mate_settings_manager_start_with_settings_prefix (manager, mateconf_prefix, &error); } else { res = mate_settings_manager_start (manager, &error); } if (! res) { g_warning ("Unable to start: %s", error->message); g_error_free (error); goto out; } } daemon_terminate_parent (); if (do_timed_exit) { g_timeout_add (1000 * 30, (GSourceFunc) timed_exit_cb, NULL); } gtk_main (); out: g_free (mateconf_prefix); if (bus != NULL) { dbus_g_connection_unref (bus); } if (manager != NULL) { g_object_unref (manager); } g_debug ("SettingsDaemon finished"); mate_settings_profile_end (NULL); return 0; }