~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/daemon.c

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:24:18 UTC
  • mfrom: (2159.1.1 remove-lint)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172418-vd210j88hiwk8jih
Removed the lint stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 * SUCH DAMAGE.
32
32
 */
33
33
 
34
 
#include <config.h>
35
 
 
36
34
#if defined __SUNPRO_C || defined __DECC || defined __HP_cc
37
35
# pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
38
36
# pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
47
45
#include <unistd.h>
48
46
#include <sys/select.h>
49
47
 
50
 
#include <drizzled/daemon.h>
51
 
 
52
 
namespace drizzled
53
 
{
 
48
int daemonize(int nochdir, int noclose, int wait_sigusr1);
 
49
int daemon_is_ready(void);
 
50
void sigusr1_handler(int sig);
54
51
 
55
52
pid_t parent_pid;
56
53
 
57
 
extern "C"
58
 
{
59
 
 
60
 
static void sigusr1_handler(int sig)
 
54
void sigusr1_handler(int sig)
61
55
{
62
56
  if (sig == SIGUSR1)
63
57
    _exit(EXIT_SUCCESS);
64
58
}
65
59
 
66
 
}
67
 
 
68
 
void daemon_is_ready()
 
60
int daemon_is_ready()
69
61
{
70
62
  kill(parent_pid, SIGUSR1);
 
63
  return 0;
71
64
}
72
65
 
73
 
bool daemonize(bool nochdir, bool noclose, bool wait_sigusr1)
 
66
int daemonize(int nochdir, int noclose, int wait_sigusr1)
74
67
{
75
68
    int fd;
76
69
    pid_t child= -1;
83
76
    switch (child)
84
77
    {
85
78
    case -1:
86
 
        return true;
 
79
        return (-1);
87
80
    case 0:
88
81
        break;
89
82
    default:
92
85
        /* parent */
93
86
        int exit_code= -1;
94
87
        int status;
95
 
        while (waitpid(child, &status, 0) != child)
96
 
        { }
97
 
 
 
88
        while (waitpid(child, &status, 0) != child);
98
89
        if (WIFEXITED(status))
99
90
        {
100
91
          exit_code= WEXITSTATUS(status);
113
104
 
114
105
    /* child */
115
106
    if (setsid() == -1)
116
 
        return true;
 
107
        return (-1);
117
108
 
118
109
    if (nochdir == 0) {
119
110
        if(chdir("/") != 0) {
120
111
            perror("chdir");
121
 
            return true;
 
112
            return (-1);
122
113
        }
123
114
    }
124
115
 
125
116
    if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
126
117
        if(dup2(fd, STDIN_FILENO) < 0) {
127
118
            perror("dup2 stdin");
128
 
            return true;
 
119
            return (-1);
129
120
        }
130
121
        if(dup2(fd, STDOUT_FILENO) < 0) {
131
122
            perror("dup2 stdout");
132
 
            return true;
 
123
            return (-1);
133
124
        }
134
125
        if(dup2(fd, STDERR_FILENO) < 0) {
135
126
            perror("dup2 stderr");
136
 
            return true;
 
127
            return (-1);
137
128
        }
138
129
 
139
130
        if (fd > STDERR_FILENO) {
140
131
            if(close(fd) < 0) {
141
132
                perror("close");
142
 
                return true;
 
133
                return (-1);
143
134
            }
144
135
        }
145
136
    }
146
 
    return false; 
 
137
    return (0);
147
138
}
148
 
 
149
 
} /* namespace drizzled */