int main(int argc, char *argv[]) { // create a configuration dtn::daemon::Configuration &conf = dtn::daemon::Configuration::getInstance(); // load parameter into the configuration conf.params(argc, argv); #ifdef HAVE_LIBDAEMON if (conf.getDaemon().daemonize()) { int ret = 0; pid_t pid; #ifdef HAVE_DAEMON_RESET_SIGS /* Reset signal handlers */ if (daemon_reset_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno)); return 1; } /* Unblock signals */ if (daemon_unblock_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno)); return 1; } #endif /* Set identification string for the daemon for both syslog and PID file */ daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]); /* set the pid file path */ try { std::string p = conf.getDaemon().getPidFile().getPath(); __daemon_pidfile__ = new char[p.length() + 1]; ::strcpy(__daemon_pidfile__, p.c_str()); daemon_pid_file_proc = __daemon_pid_file_proc__; } catch (const dtn::daemon::Configuration::ParameterNotSetException&) { }; /* Check if we are called with -k parameter */ if (conf.getDaemon().kill_daemon()) { int ret; /* Kill daemon with SIGTERM */ /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */ if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0) daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno)); return ret < 0 ? 1 : 0; } /* Check that the daemon is not rung twice a the same time */ if ((pid = daemon_pid_file_is_running()) >= 0) { daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid); return 1; } /* Prepare for return value passing from the initialization procedure of the daemon process */ if (daemon_retval_init() < 0) { daemon_log(LOG_ERR, "Failed to create pipe."); return 1; } /* Do the fork */ if ((pid = daemon_fork()) < 0) { /* Exit on error */ daemon_retval_done(); return 1; } else if (pid) { /* The parent */ int ret; /* Wait for 20 seconds for the return value passed from the daemon process */ if ((ret = daemon_retval_wait(20)) < 0) { daemon_log(LOG_ERR, "Could not recieve return value from daemon process: %s", strerror(errno)); return 255; } //daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret); return ret; } else { /* The daemon */ /* Close FDs */ if (daemon_close_all(-1) < 0) { daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno)); /* Send the error condition to the parent process */ daemon_retval_send(1); goto finish; } /* Create the PID file */ if (daemon_pid_file_create() < 0) { daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno)); daemon_retval_send(2); goto finish; } /* Initialize signal handling */ if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGUSR1, SIGUSR2, 0) < 0) { daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno)); daemon_retval_send(3); goto finish; } ret = __daemon_run(); finish: /* Do a cleanup */ daemon_log(LOG_INFO, "Exiting..."); daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); } return ret; } else { #endif // run the daemon return __daemon_run(); #ifdef HAVE_LIBDAEMON } #endif }
int main(int argc, char *argv[]) { int r = 255; int wrote_pid_file = 0; avahi_set_log_function(log_function); init_rand_seed(); avahi_server_config_init(&config.server_config); config.command = DAEMON_RUN; config.daemonize = 0; config.config_file = NULL; #ifdef HAVE_DBUS config.enable_dbus = 1; config.fail_on_missing_dbus = 1; config.n_clients_max = 0; config.n_objects_per_client_max = 0; config.n_entries_per_entry_group_max = 0; #endif config.drop_root = 1; config.set_rlimits = 1; #ifdef ENABLE_CHROOT config.use_chroot = 1; #endif config.modify_proc_title = 1; config.disable_user_service_publishing = 0; config.publish_dns_servers = NULL; config.publish_resolv_conf = 0; config.use_syslog = 0; config.debug = 0; config.rlimit_as_set = 0; config.rlimit_core_set = 0; config.rlimit_data_set = 0; config.rlimit_fsize_set = 0; config.rlimit_nofile_set = 0; config.rlimit_stack_set = 0; #ifdef RLIMIT_NPROC config.rlimit_nproc_set = 0; #endif if ((argv0 = strrchr(argv[0], '/'))) argv0 = avahi_strdup(argv0 + 1); else argv0 = avahi_strdup(argv[0]); daemon_pid_file_ident = (const char *) argv0; daemon_log_ident = (char*) argv0; daemon_pid_file_proc = pid_file_proc; if (parse_command_line(&config, argc, argv) < 0) goto finish; if (config.modify_proc_title) avahi_init_proc_title(argc, argv); #ifdef ENABLE_CHROOT config.use_chroot = config.use_chroot && config.drop_root; #endif if (config.command == DAEMON_HELP) { help(stdout); r = 0; } else if (config.command == DAEMON_VERSION) { printf("%s "PACKAGE_VERSION"\n", argv0); r = 0; } else if (config.command == DAEMON_KILL) { if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) { avahi_log_warn("Failed to kill daemon: %s", strerror(errno)); goto finish; } r = 0; } else if (config.command == DAEMON_RELOAD) { if (daemon_pid_file_kill(SIGHUP) < 0) { avahi_log_warn("Failed to kill daemon: %s", strerror(errno)); goto finish; } r = 0; } else if (config.command == DAEMON_CHECK) r = (daemon_pid_file_is_running() >= 0) ? 0 : 1; else if (config.command == DAEMON_RUN) { pid_t pid; if (getuid() != 0 && config.drop_root) { avahi_log_error("This program is intended to be run as root."); goto finish; } if ((pid = daemon_pid_file_is_running()) >= 0) { avahi_log_error("Daemon already running on PID %u", pid); goto finish; } if (load_config_file(&config) < 0) goto finish; if (config.daemonize) { daemon_retval_init(); if ((pid = daemon_fork()) < 0) goto finish; else if (pid != 0) { int ret; /** Parent **/ if ((ret = daemon_retval_wait(20)) < 0) { avahi_log_error("Could not receive return value from daemon process."); goto finish; } r = ret; goto finish; } /* Child */ } if (config.use_syslog || config.daemonize) daemon_log_use = DAEMON_LOG_SYSLOG; if (sd_listen_fds(0) <= 0) if (daemon_close_all(-1) < 0) avahi_log_warn("Failed to close all remaining file descriptors: %s", strerror(errno)); daemon_reset_sigs(-1); daemon_unblock_sigs(-1); if (make_runtime_dir() < 0) goto finish; if (config.drop_root) { #ifdef ENABLE_CHROOT if (config.use_chroot) if (avahi_caps_reduce() < 0) goto finish; #endif if (drop_root() < 0) goto finish; #ifdef ENABLE_CHROOT if (config.use_chroot) if (avahi_caps_reduce2() < 0) goto finish; #endif } if (daemon_pid_file_create() < 0) { avahi_log_error("Failed to create PID file: %s", strerror(errno)); if (config.daemonize) daemon_retval_send(1); goto finish; } else wrote_pid_file = 1; if (config.set_rlimits) enforce_rlimits(); chdir("/"); #ifdef ENABLE_CHROOT if (config.drop_root && config.use_chroot) if (avahi_chroot_helper_start(argv0) < 0) { avahi_log_error("failed to start chroot() helper daemon."); goto finish; } #endif avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0); sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" starting up.", argv0); avahi_set_proc_title(argv0, "%s: starting up", argv0); if (run_server(&config) == 0) r = 0; avahi_log_info("%s "PACKAGE_VERSION" exiting.", argv0); sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" exiting.", argv0); } finish: if (config.daemonize) daemon_retval_done(); avahi_server_config_free(&config.server_config); avahi_free(config.config_file); avahi_strfreev(config.publish_dns_servers); avahi_strfreev(resolv_conf_name_servers); avahi_strfreev(resolv_conf_search_domains); if (wrote_pid_file) { #ifdef ENABLE_CHROOT avahi_chroot_helper_unlink(pid_file_proc()); #else daemon_pid_file_remove(); #endif } #ifdef ENABLE_CHROOT avahi_chroot_helper_shutdown(); #endif avahi_free(argv0); return r; }
/** Punto de entrada de la aplicacion de navegacion. * @return Estado de la aplicacion al terminar. */ int main(int argc, char *argv[]) { pid_t pid; /* Reset signal handlers */ if (daemon_reset_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno)); return 1; } /* Unblock signals */ if (daemon_unblock_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno)); return 1; } /* Set indetification string for the daemon for both syslog and PID file */ daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]); /* Check if we are called with -k parameter */ if (argc >= 2 && !strcmp(argv[1], "-k")) { int ret; /* Kill daemon with SIGTERM */ /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */ if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0) daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno)); return ret < 0 ? 1 : 0; } /* Check that the daemon is not rung twice a the same time */ if ((pid = daemon_pid_file_is_running()) >= 0) { daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid); return 1; } /* Prepare for return value passing from the initialization procedure of the daemon process */ if (daemon_retval_init() < 0) { daemon_log(LOG_ERR, "Failed to create pipe."); return 1; } /* Do the fork */ if ((pid = daemon_fork()) < 0) { /* Exit on error */ daemon_retval_done(); return 1; } else if (pid) { /* The parent */ int ret; /* Wait for 20 seconds for the return value passed from the daemon process */ if ((ret = daemon_retval_wait(20)) < 0) { daemon_log(LOG_ERR, "Could not recieve return value from daemon process: %s", strerror(errno)); return 255; } daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, "Daemon returned %i as return value.", ret); return ret; } else { /* The daemon */ int fd, quit = 0; fd_set fds; char * config_file; config_t cfg; config_setting_t *setting; /* Base de Datos de Tiempo Real */ struct rtdb rtdb; /* Controlador de estado de la MTi-G */ struct mti_g mti_g; struct time_tracker tracker; struct sharksoft sharksoft; /* Close FDs */ if (daemon_close_all(-1) < 0) { daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno)); /* Send the error condition to the parent process */ daemon_retval_send(1); goto finish; } /* Create the PID file */ if (daemon_pid_file_create() < 0) { daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno)); daemon_retval_send(2); goto finish; } /* Initialize signal handling */ if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, 0) < 0) { daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno)); daemon_retval_send(3); goto finish; } /*... do some further init work here */ daemon_log(LOG_INFO, "Starting"); /* cargar la configuración de la aplicación */ config_init(&cfg); config_file = malloc(strlen(CONFIG_PATH) + strlen(CONFIG_FILE) + 1); assert(config_file != NULL); strcpy(config_file, CONFIG_PATH); strcpy(config_file + strlen(CONFIG_PATH), CONFIG_FILE); /* Read the file. If there is an error, report it and exit. */ if (!config_read_file(&cfg, CONFIG_FILE)) { daemon_log(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); daemon_retval_send(4); goto finish; } daemon_log(LOG_INFO, "Config file readed"); /* Inicializar el controlador de estado de la MTi-G, enlazando * la RTDB como area de almacenamiento */ setting = config_lookup(&cfg, "mti_g"); mti_g_init(&mti_g, setting, &(rtdb.imu), &(rtdb.gps)); daemon_log(LOG_INFO, "MTi-G state controller inited"); setting = config_lookup(&cfg, "time_tracker"); time_tracker_init(&tracker, setting); daemon_log(LOG_INFO, "Time Tracker service inited"); setting = config_lookup(&cfg, "sharksoft"); sharksoft_init(&sharksoft, setting, &(rtdb.imu), &(rtdb.gps)); daemon_log(LOG_INFO, "Sharksoft data provider service inited"); /* Send OK to parent process */ daemon_retval_send(0); daemon_log(LOG_INFO, "Sucessfully started"); /* Prepare for select() on the signals */ FD_ZERO(&fds); fd = daemon_signal_fd(); FD_SET(fd, &fds); FD_SET(mti_g.fd, &fds); FD_SET(sharksoft.fd, &fds); while (!quit) { fd_set fds2 = fds; /* Wait for an incoming signal */ if (select(FD_SETSIZE, &fds2, 0, 0, 0) < 0) { /* If we've been interrupted by an incoming signal, continue */ if (errno == EINTR) continue; daemon_log(LOG_ERR, "select(): %s", strerror(errno)); break; } /* Check if a signal has been recieved */ if (FD_ISSET(fd, &fds2)) { int sig; /* Get signal */ if ((sig = daemon_signal_next()) <= 0) { daemon_log(LOG_ERR, "daemon_signal_next() failed: %s", strerror(errno)); break; } /* Dispatch signal */ switch (sig) { case SIGINT: case SIGQUIT: case SIGTERM: daemon_log(LOG_WARNING, "Got SIGINT, SIGQUIT or SIGTERM."); quit = 1; break; case SIGHUP: daemon_log(LOG_INFO, "Got a HUP"); daemon_exec("/", NULL, "/bin/ls", "ls", (char*) NULL); break; } } /* Procesar señales desde la MTi-G */ if (FD_ISSET(mti_g.fd, &fds2)) { mti_g_handle_request(&mti_g); //sharksoft_handle_getdata(&sharksoft); //time_tracker_handle(&tracker); } /* Procesar señales desde el HMI Sharksoft */ if (FD_ISSET(sharksoft.fd, &fds2)) { //time_tracker_handle(&tracker); sharksoft_handle_request(&sharksoft); } } time_tracker_dispose(&tracker); config_destroy(&cfg); /* Do a cleanup */ finish: daemon_log(LOG_INFO, "Exiting..."); daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); return 0; } }
int main(int argc, char *argv[]) { pid_t pid; /* Reset signal handlers */ if (daemon_reset_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to reset all signal handlers: %s", strerror(errno)); return 1; } /* Unblock signals */ if (daemon_unblock_sigs(-1) < 0) { daemon_log(LOG_ERR, "Failed to unblock all signals: %s", strerror(errno)); } /* Set indetification string for the daemon for both syslog and PID file */ daemon_log_ident = daemon_ident_from_argv0(argv[0]); daemon_pid_file_ident = daemon_log_ident; //(LOG_ERR, "The log ident is %s\n",daemon_log_ident); daemon_pid_file_proc = get_pid_file_proc; sprintf (image_pid_file, "/var/run/%s.pid", daemon_pid_file_ident); /* Check if we are called with -k parameter */ if (argc >= 2 && !strcmp(argv[1], "-k")) { int ret; /* Kill daemon with SIGTERM */ /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */ if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0) daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno)); return ret < 0 ? 1 : 0; } /* Check that the daemon is not rung twice a the same time */ if ((pid = daemon_pid_file_is_running()) >= 0) { daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid); return 1; } /* Prepare for return value passing from the initialization procedure of the daemon process */ if (daemon_retval_init() < 0) { daemon_log(LOG_ERR, "Failed to create pipe."); return 1; } /* Do the fork */ if ((pid = daemon_fork()) < 0) { /* Exit on error */ daemon_retval_done(); return 1; } else if (pid) { /* The parent */ int ret; /* Wait for 20 seconds for the return value passed from the daemon process */ if ((ret = daemon_retval_wait(20)) < 0) { daemon_log(LOG_ERR, "Count not receive return value from daemon" "process: %s", strerror(errno)); } //daemon_log(ret != 0 ? LOG_ERR : LOG_INFO, // "Daemon returned %i as return value.", ret); return ret; } else { /* The daemon */ int fd, quit = 0; fd_set fds; pthread_t sock_thread; pthread_t nl_thread; /* Close FDs */ if (daemon_close_all(-1) < 0) { daemon_log(LOG_ERR, "Failed to close all file descriptors: %s", strerror(errno)); /* Send the error condition to the parent process */ daemon_retval_send(1); goto finish; } /* Create the PID file */ if (daemon_pid_file_create() < 0) { daemon_log(LOG_ERR, "Could not create PID file (%s).", strerror(errno)); daemon_retval_send(2); goto finish; } /* Initialize signal handling */ if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, 0) < 0) { daemon_log(LOG_ERR, "Could not register signal handlers (%s).", strerror(errno)); daemon_retval_send(3); goto finish; } /* Init task */ init_image_config(argc, argv); init_netlink(); /* Send OK to parent process */ daemon_retval_send(0); daemon_log(LOG_INFO, "Sucessfully started"); /* Main task*/ pthread_create(&nl_thread, NULL, (void *)netlink_loop, (void *)NULL); #if 0 pthread_create(&sock_thread, NULL, (void *)socket_loop, (void *)NULL); #endif pthread_create(&sock_thread, NULL, (void *)mq_loop, (void *)NULL); /* Prepare for select() on the signal fd */ FD_ZERO(&fds); fd = daemon_signal_fd(); FD_SET(fd, &fds); while (!quit) { fd_set fds2 = fds; /* Wait for an incoming signal */ if (select(FD_SETSIZE, &fds2, 0, 0, 0) < 0) { /* If we've been interrupted by an incoming signal, continue */ if (errno == EINTR) continue; daemon_log(LOG_ERR, "select(): %s", strerror(errno)); break; } /* Check if a signal has been recieved */ if (FD_ISSET(fd, &fds2)) { int sig; /* Get signal */ if ((sig = daemon_signal_next()) <= 0) { daemon_log(LOG_ERR, "daemon_signal_next() failed: %s", strerror(errno)); break; } /* Dispatch signal */ switch (sig) { case SIGINT: case SIGQUIT: case SIGTERM: daemon_log(LOG_WARNING, "Got SIGINT, SIGQUIT or" "SIGTERM."); if (nl_image_config.nl_connected == 1) { nl_image_config.nl_connected = 0; if (nl_send_image_session_cmd( IMAGE_SESSION_CMD_DISCONNECT) < 0) { printf("Failed to remove connection with" "kernel!\n"); } } quit = 1; break; case SIGHUP: daemon_log(LOG_INFO, "Got a HUP"); daemon_exec("/", NULL, "/bin/ls", "ls", (char*) NULL); break; } } } /* Do a cleanup */ finish: delete_pid_file(IMAGE_SERVER_PROC); daemon_log(LOG_INFO, "Exiting..."); daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); return 0; } }
int main(int argc, char *argv[]) { int index; int c; std::string ptp_dev("tun0"); std::string app_name("tunnel"); std::string endpoint("dtn:none"); unsigned int lifetime = 60; bool daemonize = false; bool stop_daemon = false; std::string pidfile; bool throughput = false; #ifdef HAVE_LIBDAEMON while ((c = getopt (argc, argv, "td:s:l:hDkp:")) != -1) #else while ((c = getopt (argc, argv, "td:s:l:h")) != -1) #endif switch (c) { #ifdef HAVE_LIBDAEMON case 'D': daemonize = true; break; case 'k': daemonize = true; stop_daemon = true; break; case 'p': pidfile = optarg; break; #endif case 'd': ptp_dev = optarg; break; case 't': throughput = true; break; case 's': app_name = optarg; break; case 'l': lifetime = atoi(optarg); break; default: print_help(argv[0]); return 1; } int optindex = 0; for (index = optind; index < argc; ++index) { switch (optindex) { case 0: endpoint = std::string(argv[index]); break; } optindex++; } // print help if not enough parameters are set if (!stop_daemon && (optindex < 1)) { print_help(argv[0]); exit(0); } // catch process signals ibrcommon::SignalHandler sighandler(term); sighandler.handle(SIGINT); sighandler.handle(SIGTERM); sighandler.handle(SIGQUIT); //initialize sighandler after possible exit call sighandler.initialize(); // logging options //const unsigned char logopts = ibrcommon::Logger::LOG_DATETIME | ibrcommon::Logger::LOG_LEVEL; const unsigned char logopts = 0; // error filter const unsigned char logerr = ibrcommon::Logger::LOGGER_ERR | ibrcommon::Logger::LOGGER_CRIT; // logging filter, everything but debug, err and crit const unsigned char logstd = ibrcommon::Logger::LOGGER_ALL ^ (ibrcommon::Logger::LOGGER_DEBUG | logerr); // syslog filter, everything but DEBUG and NOTICE const unsigned char logsys = ibrcommon::Logger::LOGGER_ALL ^ (ibrcommon::Logger::LOGGER_DEBUG | ibrcommon::Logger::LOGGER_NOTICE); #ifdef HAVE_LIBDAEMON if (daemonize) { // enable syslog logging ibrcommon::Logger::enableSyslog(argv[0], LOG_PID, LOG_DAEMON, logsys); } else #endif { // add logging to the cout ibrcommon::Logger::addStream(std::cout, logstd, logopts); // add logging to the cerr ibrcommon::Logger::addStream(std::cerr, logerr, logopts); } #ifdef HAVE_LIBDAEMON if (daemonize) { #ifdef HAVE_DAEMON_RESET_SIGS /* Reset signal handlers */ if (daemon_reset_sigs(-1) < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Failed to reset all signal handlers: " << strerror(errno) << IBRCOMMON_LOGGER_ENDL; return 1; } /* Unblock signals */ if (daemon_unblock_sigs(-1) < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Failed to unblock all signals: " << strerror(errno) << IBRCOMMON_LOGGER_ENDL; return 1; } #endif pid_t pid; /* Set identification string for the daemon for both syslog and PID file */ daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]); /* set the pid file path */ if (pidfile.length() > 0) { __daemon_pidfile__ = new char[pidfile.length() + 1]; ::strcpy(__daemon_pidfile__, pidfile.c_str()); daemon_pid_file_proc = __daemon_pid_file_proc__; } /* Check if we are called with -k parameter */ if (stop_daemon) { int ret; /* Kill daemon with SIGTERM */ /* Check if the new function daemon_pid_file_kill_wait() is available, if it is, use it. */ if ((ret = daemon_pid_file_kill_wait(SIGTERM, 5)) < 0) IBRCOMMON_LOGGER_TAG("Core", warning) << "Failed to kill daemon: " << strerror(errno) << IBRCOMMON_LOGGER_ENDL; return ret < 0 ? 1 : 0; } /* Check that the daemon is not rung twice a the same time */ if ((pid = daemon_pid_file_is_running()) >= 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Daemon already running on PID file " << pid << IBRCOMMON_LOGGER_ENDL; return 1; } /* Prepare for return value passing from the initialization procedure of the daemon process */ if (daemon_retval_init() < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Failed to create pipe." << IBRCOMMON_LOGGER_ENDL; return 1; } /* Do the fork */ if ((pid = daemon_fork()) < 0) { /* Exit on error */ daemon_retval_done(); return 1; } else if (pid) { /* The parent */ int ret; /* Wait for 20 seconds for the return value passed from the daemon process */ if ((ret = daemon_retval_wait(20)) < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Could not recieve return value from daemon process: " << strerror(errno) << IBRCOMMON_LOGGER_ENDL; return 255; } return ret; } else { /* The daemon */ /* Close FDs */ if (daemon_close_all(-1) < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Failed to close all file descriptors: " << strerror(errno) << IBRCOMMON_LOGGER_ENDL; /* Send the error condition to the parent process */ daemon_retval_send(1); /* Do a cleanup */ daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); return -1; } /* Create the PID file */ if (daemon_pid_file_create() < 0) { IBRCOMMON_LOGGER_TAG("Core", error) << "Could not create PID file ( " << strerror(errno) << ")." << IBRCOMMON_LOGGER_ENDL; daemon_retval_send(2); /* Do a cleanup */ daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); return -1; } /* Send OK to parent process */ daemon_retval_send(0); } } #endif IBRCOMMON_LOGGER_TAG("Core", info) << "IBR-DTN IP <-> Bundle Tunnel" << IBRCOMMON_LOGGER_ENDL; // create a connection to the dtn daemon ibrcommon::vaddress addr("localhost", 4550); ibrcommon::socketstream conn(new ibrcommon::tcpsocket(addr)); try { // set-up tun2bundle gateway TUN2BundleGateway gateway(app_name, conn, ptp_dev); _gateway = &gateway; IBRCOMMON_LOGGER_TAG("Core", info) << "Local: " << app_name << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", info) << "Peer: " << endpoint << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", info) << "Device: " << gateway.getDeviceName() << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", notice) << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", notice) << "Now you need to set-up the ip tunnel. You can use commands like this:" << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", notice) << "# sudo ip link set " << gateway.getDeviceName() << " up mtu 65535" << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", notice) << "# sudo ip addr add 10.0.0.1/24 dev " << gateway.getDeviceName() << IBRCOMMON_LOGGER_ENDL; IBRCOMMON_LOGGER_TAG("Core", notice) << IBRCOMMON_LOGGER_ENDL; timer_t timerid; struct sigevent sev; if (!daemonize && throughput) { // enable throughput timer signal(SIGRTMIN, timer_display_throughput); sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN; sev.sigev_value.sival_ptr = &timerid; // create a timer timer_create(CLOCK_MONOTONIC, &sev, &timerid); // arm the timer struct itimerspec its; size_t freq_nanosecs = 200000000; its.it_value.tv_sec = freq_nanosecs / 1000000000;; its.it_value.tv_nsec = freq_nanosecs % 1000000000; its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; if (timer_settime(timerid, 0, &its, NULL) == -1) { IBRCOMMON_LOGGER_TAG("Core", error) << "Timer set failed." << IBRCOMMON_LOGGER_ENDL; } } // destination dtn::data::EID eid(endpoint); while (m_running) { gateway.process(eid, lifetime); } gateway.shutdown(); } catch (const ibrcommon::Exception &ex) { if (m_running) { IBRCOMMON_LOGGER_TAG("Core", error) << ex.what() << IBRCOMMON_LOGGER_ENDL; return -1; } } #ifdef HAVE_LIBDAEMON if (daemonize) { /* Do a cleanup */ IBRCOMMON_LOGGER_TAG("Core", info) << "Stopped " << app_name << IBRCOMMON_LOGGER_ENDL; daemon_retval_send(255); daemon_signal_done(); daemon_pid_file_remove(); } else #endif { std::cout << std::endl; } return 0; }