+ pid_file_name = avahi_strdup_printf(AVAHI_RUNTIME_DIR"/avahi-autoipd.%s.pid", interface_name);
+
+ if (command == DAEMON_RUN) {
+ pid_t pid;
+ int ifindex;
+
+ init_rand_seed();
+
+ if ((ifindex = if_nametoindex(interface_name)) <= 0) {
+ daemon_log(LOG_ERR, "Failed to get index for interface name '%s': %s", interface_name, strerror(errno));
+ goto finish;
+ }
+
+ if (getuid() != 0) {
+ daemon_log(LOG_ERR, "This program is intended to be run as root.");
+ goto finish;
+ }
+
+ if ((pid = daemon_pid_file_is_running()) >= 0) {
+ daemon_log(LOG_ERR, "Daemon already running on PID %u", pid);
+ goto finish;
+ }
+
+ if (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) {
+ daemon_log(LOG_ERR, "Could not receive return value from daemon process.");
+ goto finish;
+ }
+
+ r = ret;
+ goto finish;
+ }
+
+ /* Child */
+ }
+
+ if (use_syslog || daemonize)
+ daemon_log_use = DAEMON_LOG_SYSLOG;
+
+ chdir("/");
+
+ if (daemon_pid_file_create() < 0) {
+ daemon_log(LOG_ERR, "Failed to create PID file: %s", strerror(errno));
+
+ if (daemonize)
+ daemon_retval_send(1);
+ goto finish;
+ } else
+ wrote_pid_file = 1;
+
+ if (loop(ifindex, start_address) < 0)
+ goto finish;
+
+ r = 0;
+ } else if (command == DAEMON_HELP) {
+ help(stdout, argv0);
+
+ r = 0;
+ } else if (command == DAEMON_VERSION) {
+ printf("%s "PACKAGE_VERSION"\n", argv0);
+
+ r = 0;
+ } else if (command == DAEMON_KILL) {
+ if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
+ daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
+ goto finish;
+ }
+
+ r = 0;
+ } else if (command == DAEMON_REFRESH) {
+ if (daemon_pid_file_kill(SIGHUP) < 0) {
+ daemon_log(LOG_WARNING, "Failed to kill daemon: %s", strerror(errno));
+ goto finish;
+ }
+
+ r = 0;
+ } else if (command == DAEMON_CHECK)
+ r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
+
+
+finish:
+
+ if (daemonize)
+ daemon_retval_done();