Project

General

Profile

Bug #2712 » daemonize_startup_behaviour.diff

pasdVn, 2016-01-31 19:27

View differences:

src/server.c (Arbeitskopie)
129 129
	}
130 130
}
131 131
#elif defined(HAVE_SIGNAL) || defined(HAVE_SIGACTION)
132
static void signal_handler(int sig) {
132
static int signal_handler(int sig) {
133 133
	switch (sig) {
134 134
	case SIGTERM: srv_shutdown = 1; break;
135 135
	case SIGINT:
......
145 145
#endif
146 146

  
147 147
#ifdef HAVE_FORK
148
static void daemonize(void) {
148
static int daemonize() {
149
	int pipefd[2];
150
	pid_t pid;
149 151
#ifdef SIGTTOU
150 152
	signal(SIGTTOU, SIG_IGN);
151 153
#endif
......
155 157
#ifdef SIGTSTP
156 158
	signal(SIGTSTP, SIG_IGN);
157 159
#endif
158
	if (0 != fork()) exit(0);
159 160

  
161
	if (pipe(pipefd) < 0) exit(-1);
162
		
163
	if (0 > (pid = fork())) exit(-1);
164
	else if (0 < pid)
165
	{
166
		char buf;
167
		ssize_t bytes;
168

  
169
		close(pipefd[1]);
170
		/* parent waits for grandchild to be ready */
171
		bytes = read(pipefd[0], &buf, sizeof(buf));
172
		close(pipefd[0]);
173

  
174
		/* closed fd (without writing) == failure in grandchild */
175
		exit((bytes > 0) ? 0 : -1);
176

  
177
		return 0; /* never reached */
178
	}
179

  
180
	/* better to return -1 in the following error cases? */
160 181
	if (-1 == setsid()) exit(0);
161 182

  
162 183
	signal(SIGHUP, SIG_IGN);
......
164 185
	if (0 != fork()) exit(0);
165 186

  
166 187
	if (0 != chdir("/")) exit(0);
188

  
189
	return pipefd[1];
167 190
}
168 191
#endif
169 192

  
......
568 591
	interval.it_value.tv_usec = 0;
569 592
#endif
570 593

  
594
#ifdef HAVE_FORK
595
	int parent_pipe_fd = 0;
596
#endif
571 597

  
572 598
	/* for nice %b handling in strfime() */
573 599
	setlocale(LC_TIME, "C");
......
951 977

  
952 978
#ifdef HAVE_FORK
953 979
	/* network is up, let's deamonize ourself */
954
	if (srv->srvconf.dont_daemonize == 0) daemonize();
980
	if (srv->srvconf.dont_daemonize == 0) {
981
		parent_pipe_fd = daemonize();
982
	}
955 983
#endif
956 984

  
957 985

  
......
1218 1246
		}
1219 1247
	}
1220 1248

  
1249
#ifdef HAVE_FORK
1250
	if (srv->srvconf.dont_daemonize == 0) {
1251
		/* notify daemonize-grandparent of successfull startup */
1252
		if (0 > write(parent_pipe_fd, "\x0", 1)) return -1;
1253
		close(parent_pipe_fd);
1254
	}
1255
#endif
1256

  
1221 1257
	/* main-loop */
1222 1258
	while (!srv_shutdown) {
1223 1259
		int n;
(1-1/2)