~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/daemon.cc

  • Committer: Brian Aker
  • Date: 2011-07-18 17:13:07 UTC
  • mto: (2392.1.1 drizzle-autoconf)
  • mto: This revision was merged to the branch mainline in revision 2399.
  • Revision ID: brian@tangent.org-20110718171307-3ya21g8ntq16qrsq
This shifts the closing of the socket to after we know that no errors are going to occur.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
 
68
68
void daemon_is_ready()
69
69
{
 
70
  int fd;
70
71
  kill(parent_pid, SIGUSR1);
 
72
 
 
73
  if ((fd = open("/dev/null", O_RDWR, 0)) != -1) 
 
74
  {
 
75
    if(dup2(fd, STDIN_FILENO) < 0)
 
76
    {
 
77
      perror("dup2 stdin");
 
78
      return;
 
79
    }
 
80
 
 
81
    if(dup2(fd, STDOUT_FILENO) < 0)
 
82
    {
 
83
      perror("dup2 stdout");
 
84
      return;
 
85
    }
 
86
 
 
87
    if(dup2(fd, STDERR_FILENO) < 0)
 
88
    {
 
89
      perror("dup2 stderr");
 
90
      return;
 
91
    }
 
92
 
 
93
    if (fd > STDERR_FILENO)
 
94
    {
 
95
      if (close(fd) < 0)
 
96
      {
 
97
        perror("close");
 
98
        return;
 
99
      }
 
100
    }
 
101
  }
71
102
}
72
103
 
73
 
bool daemonize(bool nochdir, bool noclose, bool wait_sigusr1)
 
104
bool daemonize()
74
105
{
75
 
    int fd;
76
 
    pid_t child= -1;
77
 
 
78
 
    parent_pid= getpid();
79
 
    signal(SIGUSR1, sigusr1_handler);
80
 
 
81
 
    child= fork();
82
 
 
83
 
    switch (child)
 
106
  pid_t child= -1;
 
107
 
 
108
  parent_pid= getpid();
 
109
  signal(SIGUSR1, sigusr1_handler);
 
110
 
 
111
  child= fork();
 
112
 
 
113
  switch (child)
 
114
  {
 
115
  case -1:
 
116
    return true;
 
117
 
 
118
  case 0:
 
119
    break;
 
120
 
 
121
  default:
84
122
    {
85
 
    case -1:
86
 
        return true;
87
 
    case 0:
88
 
        break;
89
 
    default:
90
 
      if (wait_sigusr1)
91
 
      {
92
 
        /* parent */
93
 
        int exit_code= -1;
94
 
        int status;
95
 
        while (waitpid(child, &status, 0) != child)
96
 
        { }
97
 
 
98
 
        if (WIFEXITED(status))
99
 
        {
100
 
          exit_code= WEXITSTATUS(status);
101
 
        }
102
 
        if (WIFSIGNALED(status))
103
 
        {
104
 
          exit_code= -1;
105
 
        }
106
 
        _exit(exit_code);
107
 
      }
108
 
      else
109
 
      {
110
 
        _exit(EXIT_SUCCESS);
111
 
      }
112
 
    }
113
 
 
114
 
    /* child */
115
 
    if (setsid() == -1)
116
 
        return true;
117
 
 
118
 
    if (nochdir == 0) {
119
 
        if(chdir("/") != 0) {
120
 
            perror("chdir");
121
 
            return true;
122
 
        }
123
 
    }
124
 
 
125
 
    if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
126
 
        if(dup2(fd, STDIN_FILENO) < 0) {
127
 
            perror("dup2 stdin");
128
 
            return true;
129
 
        }
130
 
        if(dup2(fd, STDOUT_FILENO) < 0) {
131
 
            perror("dup2 stdout");
132
 
            return true;
133
 
        }
134
 
        if(dup2(fd, STDERR_FILENO) < 0) {
135
 
            perror("dup2 stderr");
136
 
            return true;
137
 
        }
138
 
 
139
 
        if (fd > STDERR_FILENO) {
140
 
            if(close(fd) < 0) {
141
 
                perror("close");
142
 
                return true;
143
 
            }
144
 
        }
145
 
    }
146
 
    return false; 
 
123
      /* parent */
 
124
      int exit_code= -1;
 
125
      int status;
 
126
      while (waitpid(child, &status, 0) != child)
 
127
      { }
 
128
 
 
129
      if (WIFEXITED(status))
 
130
      {
 
131
        exit_code= WEXITSTATUS(status);
 
132
      }
 
133
      if (WIFSIGNALED(status))
 
134
      {
 
135
        exit_code= -1;
 
136
      }
 
137
      _exit(exit_code);
 
138
    }
 
139
  }
 
140
 
 
141
  /* child */
 
142
  if (setsid() == -1)
 
143
  {
 
144
    return true;
 
145
  }
 
146
 
 
147
  return false; 
147
148
}
148
149
 
149
150
} /* namespace drizzled */