Пример #1
0
static gboolean
infinoted_main_run(InfinotedStartup* startup,
                   GError** error)
{
    InfinotedRun* run;
    InfinotedSignal* sig;

    run = infinoted_run_new(startup, error);
    if(run == NULL) return FALSE;

    sig = infinoted_signal_register(run);

    /* Now start the server. It can later be stopped by signals. */
    infinoted_run_start(run);

    infinoted_signal_unregister(sig);
    infinoted_run_free(run);
    return TRUE;
}
Пример #2
0
/* Takes ownership of startup */
static gboolean
infinoted_main_run(InfinotedStartup* startup,
                   GError** error)
{
  InfinotedRun* run;
  InfinotedSignal* sig;

#ifdef LIBINFINITY_HAVE_LIBDAEMON
  mode_t prev_umask;
  pid_t pid;
  int saved_errno;
#endif

  /* infinoted_run_new() takes ownership of startup */
  run = infinoted_run_new(startup, error);
  if(run == NULL)
  {
    infinoted_startup_free(startup);
    return FALSE;
  }

#ifdef LIBINFINITY_HAVE_LIBDAEMON
  if(startup->options->daemonize)
  {
    prev_umask = umask(0777);

    if(daemon_retval_init() == -1)
    {
      infinoted_run_free(run);
      return FALSE; /* libdaemon already wrote an error message */
    }

    pid = daemon_fork();
    if(pid < 0)
    {
      /* Translators: fork as in "fork into the background" */
      infinoted_util_set_errno_error(error, errno, _("Failed to fork"));
      infinoted_run_free(run);
      daemon_retval_done();
      return FALSE;
    }
    else if(pid > 0)
    {
      infinoted_run_free(run);
      saved_errno = daemon_retval_wait(5);
      if(saved_errno == 0)
      {
        return TRUE;
      }
      if(saved_errno == -1)
      {
        infinoted_util_set_errno_error(error, errno,
          _("Failed to wait for daemonized child's return value"));
        return FALSE;
      }
      else
      {
        /* on -1, the child process would have subtracted one from
         * errno before passing it back to us. */
        if(saved_errno < 0) ++saved_errno;
        infinoted_util_set_errno_error(
          error, saved_errno, _("Failed to create PID file"));
        return FALSE;
      }
    }
    else
    {
      infinoted_util_daemon_set_global_pid_file_proc();
      if(daemon_pid_file_create() != 0)
      {
        infinoted_util_daemon_set_local_pid_file_proc();
        if(daemon_pid_file_create() != 0)
        {
          if(daemon_pid_file_create() != 0)
          {
            saved_errno = errno;
            infinoted_util_set_errno_error(
              error,
              saved_errno,
              _("Failed to create PID file")
            );
            if(saved_errno < 0) --saved_errno;
            daemon_retval_send(saved_errno);

            infinoted_run_free(run);
            return FALSE;
          }
        }
      }

      daemon_retval_send(0);
    }

    /* libdaemon sets the umask to either 0777 (< 0.14) or 0077 (>= 0.14).
     * We don't want either of that, to make sure the directory tree is
     * always readable by us and potentially by others (for example, a
     * webserver providing read access to the documents). Therefore, reset
     * the umask here to what it previously was, so the system administrator
     * can define the umask by setting it before launching infinoted.
     * See also http://gobby.0x539.de/trac/ticket/617.  */
    umask(prev_umask);
  }
#endif

  sig = infinoted_signal_register(run);

  /* Now start the server. It can later be stopped by signals. */
  infinoted_run_start(run);

  infinoted_signal_unregister(sig);

#ifdef LIBINFINITY_HAVE_LIBDAEMON
  /* startup might be invalid at this point in case a config reload happened,
   * so use run->startup instead (which is revalidated by config reload). */
  if(run->startup->options->daemonize)
    daemon_pid_file_remove();
#endif

  infinoted_run_free(run);
  return TRUE;
}