void test_daemonise (void) { pid_t pid; char result[2]; int status, fds[2]; /* Check that nih_main_daemonise does all of the right things, * our immediate child should exit with a zero status, and the * child within that should be run with a working directory of / */ TEST_FUNCTION ("nih_main_daemonise"); assert0 (pipe (fds)); TEST_CHILD (pid) { char buf[80]; int fd; program_name = "test"; fd = open ("/dev/null", O_WRONLY); assert (fd >= 0); assert (dup2 (fd, STDERR_FILENO) >= 0); assert0 (close (fd)); if (nih_main_daemonise () < 0) exit (50); assert (getcwd (buf, sizeof (buf))); if (strcmp (buf, "/")) { assert (write (fds[1], "wd", 2) == 2); exit (10); } assert (write (fds[1], "ok", 2) == 2); exit (10); } waitpid (pid, &status, 0); TEST_TRUE (WIFEXITED (status)); TEST_EQ (WEXITSTATUS (status), 0); if (read (fds[0], result, 2) != 2) TEST_FAILED ("expected return code from child"); if (! memcmp (result, "wd", 2)) TEST_FAILED ("wrong working directory for child"); if (memcmp (result, "ok", 2)) TEST_FAILED ("wrong return code from child, expected 'ok' got '%.2s'", result); }
int main (int argc, char *argv[]) { char ** args; int ret; DBusServer * server; struct stat sb; struct rlimit newrlimit; nih_main_init (argv[0]); nih_option_set_synopsis (_("Control group manager")); nih_option_set_help (_("The cgroup manager daemon")); args = nih_option_parser (NULL, argc, argv, options, FALSE); if (! args) exit (1); if (!setup_cgroup_dir()) { nih_fatal("Failed to set up cgmanager socket"); exit(1); } /* Setup the DBus server */ server = nih_dbus_server (CGMANAGER_DBUS_PATH, client_connect, client_disconnect); nih_assert (server != NULL); if (!setup_base_run_path()) { nih_fatal("Error setting up base cgroup path"); return -1; } if (collect_subsystems(extra_cgroup_mounts) < 0) { nih_fatal("failed to collect cgroup subsystems"); exit(1); } if (!create_agent_symlinks()) { nih_fatal("Error creating release agent symlinks"); exit(1); } if (setup_cgroup_mounts() < 0) { nih_fatal ("Failed to set up cgroup mounts"); exit(1); } if (!move_self_to_root()) { nih_fatal ("Failed to move self to root cgroup"); exit(1); } if (stat("/proc/self/ns/pid", &sb) == 0) { mypidns = read_pid_ns_link(getpid()); setns_pid_supported = true; } if (stat("/proc/self/ns/user", &sb) == 0) { myuserns = read_user_ns_link(getpid()); setns_user_supported = true; } newrlimit.rlim_cur = 10000; newrlimit.rlim_max = 10000; if (setrlimit(RLIMIT_NOFILE, &newrlimit) < 0) nih_warn("Failed to increase open file limit: %s", strerror(errno)); /* Become daemon */ if (daemonise) { if (nih_main_daemonise () < 0) { NihError *err; err = nih_error_get (); nih_fatal ("%s: %s", _("Unable to become daemon"), err->message); nih_free (err); exit (1); } } if (sigstop) raise(SIGSTOP); ret = nih_main_loop (); /* Destroy any PID file we may have created */ if (daemonise) { nih_main_unlink_pidfile(); } return ret; }
int main (int argc, char *argv[]) { char ** args; int ret; DBusServer * server; struct stat sb; nih_main_init (argv[0]); nih_option_set_synopsis (_("Control group proxy")); nih_option_set_help (_("The cgroup manager proxy")); args = nih_option_parser (NULL, argc, argv, options, FALSE); if (! args) exit (1); if (geteuid() != 0) { nih_error("%s: Cgmanager proxy must be run as root", __func__); exit(1); } /* * If we are called with checkmaster, then only check whether * cgmanager is running. This is used by the init script to * determine whether to run cgmanager or cgproxy */ if (checkmaster) { if (master_running()) exit(0); exit(1); } if (setup_proxy() < 0) { nih_fatal ("Failed to set up as proxy"); exit(1); } /* Setup the DBus server */ server = nih_dbus_server ( CGMANAGER_DBUS_PATH, client_connect, client_disconnect); nih_assert (server != NULL); if (stat("/proc/self/ns/pid", &sb) == 0) { mypidns = read_pid_ns_link(getpid()); setns_pid_supported = true; } if (stat("/proc/self/ns/user", &sb) == 0) { myuserns = read_user_ns_link(getpid()); setns_user_supported = true; } /* Become daemon */ if (daemonise) { if (nih_main_daemonise () < 0) { NihError *err; err = nih_error_get (); nih_fatal ("%s: %s", _("Unable to become daemon"), err->message); nih_free (err); exit (1); } } /* * We have to send a message to force fd passing over the dbus * link to be negotiated. Else when we try to attach an fd we'll * fail. */ if (!send_dummy_msg(server_conn)) { nih_fatal("Failed to send opening ping to cgmanager"); exit(1); } if (sigstop) raise(SIGSTOP); ret = nih_main_loop (); return ret; }
int main (int argc, char *argv[]) { char ** args; DBusConnection * connection; struct udev * udev; struct udev_monitor *udev_monitor; int ret; nih_main_init (argv[0]); nih_option_set_synopsis (_("Bridge udev events into upstart")); nih_option_set_help ( _("By default, upstart-udev-bridge does not detach from the " "console and remains in the foreground. Use the --daemon " "option to have it detach.")); args = nih_option_parser (NULL, argc, argv, options, FALSE); if (! args) exit (1); /* Initialise the connection to Upstart */ connection = NIH_SHOULD (nih_dbus_connect (DBUS_ADDRESS_UPSTART, upstart_disconnected)); if (! connection) { NihError *err; err = nih_error_get (); nih_fatal ("%s: %s", _("Could not connect to Upstart"), err->message); nih_free (err); exit (1); } upstart = NIH_SHOULD (nih_dbus_proxy_new (NULL, connection, NULL, DBUS_PATH_UPSTART, NULL, NULL)); if (! upstart) { NihError *err; err = nih_error_get (); nih_fatal ("%s: %s", _("Could not create Upstart proxy"), err->message); nih_free (err); exit (1); } /* Initialise the connection to udev */ nih_assert (udev = udev_new ()); nih_assert (udev_monitor = udev_monitor_new_from_netlink (udev, "udev")); nih_assert (udev_monitor_enable_receiving (udev_monitor) == 0); udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024); NIH_MUST (nih_io_add_watch (NULL, udev_monitor_get_fd (udev_monitor), NIH_IO_READ, (NihIoWatcher)udev_monitor_watcher, udev_monitor)); /* Become daemon */ if (daemonise) { if (nih_main_daemonise () < 0) { NihError *err; err = nih_error_get (); nih_fatal ("%s: %s", _("Unable to become daemon"), err->message); nih_free (err); exit (1); } /* Send all logging output to syslog */ openlog (program_name, LOG_PID, LOG_DAEMON); nih_log_set_logger (nih_logger_syslog); } /* Handle TERM and INT signals gracefully */ nih_signal_set_handler (SIGTERM, nih_signal_handler); NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, nih_main_term_signal, NULL)); if (! daemonise) { nih_signal_set_handler (SIGINT, nih_signal_handler); NIH_MUST (nih_signal_add_handler (NULL, SIGINT, nih_main_term_signal, NULL)); } ret = nih_main_loop (); return ret; }