2 * $Id: afp_dsi.c,v 1.48 2009-11-14 05:18:50 didg Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * Copyright (c) 1990,1993 Regents of The University of Michigan.
6 * All Rights Reserved. See COPYRIGHT.
8 * modified from main.c. this handles afp over tcp.
13 #endif /* HAVE_CONFIG_H */
22 #endif /* HAVE_UNISTD_H */
23 #include <sys/socket.h>
25 #ifdef HAVE_SYS_STAT_H
27 #endif /* HAVE_SYS_STAT_H */
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <atalk/logger.h>
32 #include <atalk/dsi.h>
33 #include <atalk/compat.h>
34 #include <atalk/util.h>
44 #endif /* FORCE_UIDGID */
46 #define CHILD_DIE (1 << 0)
47 #define CHILD_RUNNING (1 << 1)
48 #define CHILD_SLEEPING (1 << 2)
49 #define CHILD_DATA (1 << 3)
58 static void afp_dsi_close(AFPObj *obj)
60 DSI *dsi = obj->handle;
62 /* we may have been called from a signal handler caught when afpd was running
63 * as uid 0, that's the wrong user for volume's prexec_close scripts if any,
64 * restore our login user
66 if (seteuid( obj->uid ) < 0) {
67 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
74 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
75 dsi->read_count/1024.0, dsi->write_count/1024.0);
80 /* -------------------------------
82 * a little bit of code duplication.
84 static void afp_dsi_die(int sig)
86 static volatile int in_handler;
91 /* it's not atomic but we don't care because it's an exit function
92 * ie if a signal is received here, between the test and the affectation,
97 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
98 afp_dsi_close(child.obj);
99 if (sig) /* if no signal, assume dieing because logins are disabled &
100 don't log it (maintenance mode)*/
101 LOG(log_info, logtype_afpd, "Connection terminated");
102 if (sig == SIGTERM || sig == SIGALRM) {
111 static void afp_dsi_sleep(void)
113 child.flags |= CHILD_SLEEPING;
114 dsi_sleep(child.obj->handle, 1);
117 /* ------------------- */
118 static void afp_dsi_timedown(int sig _U_)
123 child.flags |= CHILD_DIE;
124 /* shutdown and don't reconnect. server going down in 5 minutes. */
125 setmessage("The server is going down for maintenance.");
126 if (dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
127 AFPATTN_MESG | AFPATTN_TIME(5)) < 0) {
128 DSI *dsi = (DSI *) child.obj->handle;
129 dsi->down_request = 1;
132 it.it_interval.tv_sec = 0;
133 it.it_interval.tv_usec = 0;
134 it.it_value.tv_sec = 300;
135 it.it_value.tv_usec = 0;
137 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
138 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
139 afp_dsi_die(EXITERR_SYS);
141 memset(&sv, 0, sizeof(sv));
142 sv.sa_handler = afp_dsi_die;
143 sigemptyset( &sv.sa_mask );
144 sigaddset(&sv.sa_mask, SIGHUP);
145 sigaddset(&sv.sa_mask, SIGTERM);
146 sv.sa_flags = SA_RESTART;
147 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
148 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
149 afp_dsi_die(EXITERR_SYS);
153 sv.sa_handler = SIG_IGN;
154 sigemptyset( &sv.sa_mask );
155 sv.sa_flags = SA_RESTART;
156 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
157 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
158 afp_dsi_die(EXITERR_SYS);
162 /* ---------------------------------
163 * SIGHUP reload configuration file
164 * FIXME here or we wait ?
166 volatile int reload_request = 0;
168 static void afp_dsi_reload(int sig _U_)
173 /* ---------------------- */
175 static void afp_dsi_getmesg (int sig _U_)
177 DSI *dsi = (DSI *) child.obj->handle;
179 dsi->msg_request = 1;
180 if (dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5)) < 0)
181 dsi->msg_request = 2;
183 #endif /* SERVERTEXT */
185 static void alarm_handler(int sig _U_)
188 DSI *dsi = (DSI *) child.obj->handle;
190 /* we have to restart the timer because some libraries
192 setitimer(ITIMER_REAL, &dsi->timer, NULL);
194 /* we got some traffic from the client since the previous timer
196 if ((child.flags & CHILD_DATA)) {
197 child.flags &= ~CHILD_DATA;
201 /* if we're in the midst of processing something,
203 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
207 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
208 if (!(err = pollvoltime(child.obj)))
209 err = dsi_tickle(child.obj->handle);
211 afp_dsi_die(EXITERR_CLNT);
213 } else { /* didn't receive a tickle. close connection */
214 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
215 afp_dsi_die(EXITERR_CLNT);
220 if dsi->in_write is set attention, tickle (and close?) msg
221 aren't sent. We don't care about tickle
223 static void pending_request(DSI *dsi)
225 /* send pending attention */
227 /* read msg if any, it could be done in afp_getsrvrmesg */
228 if (dsi->msg_request) {
229 if (dsi->msg_request == 2) {
230 /* didn't send it in signal handler */
231 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
233 dsi->msg_request = 0;
234 readmessage(child.obj);
236 if (dsi->down_request) {
237 dsi->down_request = 0;
238 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
239 AFPATTN_MESG | AFPATTN_TIME(5));
243 /* -------------------------------------------
244 afp over dsi. this never returns.
246 void afp_over_dsi(AFPObj *obj)
248 DSI *dsi = (DSI *) obj->handle;
251 struct sigaction action;
253 obj->exit = afp_dsi_die;
254 obj->reply = (int (*)()) dsi_cmdreply;
255 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
257 obj->sleep = afp_dsi_sleep;
259 child.tickle = child.flags = 0;
261 memset(&action, 0, sizeof(action));
264 action.sa_handler = afp_dsi_reload;
265 sigemptyset( &action.sa_mask );
266 sigaddset(&action.sa_mask, SIGALRM);
267 sigaddset(&action.sa_mask, SIGTERM);
268 sigaddset(&action.sa_mask, SIGUSR1);
270 sigaddset(&action.sa_mask, SIGUSR2);
272 action.sa_flags = SA_RESTART;
273 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
274 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
275 afp_dsi_die(EXITERR_SYS);
278 /* install SIGTERM */
279 action.sa_handler = afp_dsi_die;
280 sigemptyset( &action.sa_mask );
281 sigaddset(&action.sa_mask, SIGALRM);
282 sigaddset(&action.sa_mask, SIGHUP);
283 sigaddset(&action.sa_mask, SIGUSR1);
285 sigaddset(&action.sa_mask, SIGUSR2);
287 action.sa_flags = SA_RESTART;
288 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
289 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
290 afp_dsi_die(EXITERR_SYS);
294 /* Added for server message support */
295 action.sa_handler = afp_dsi_getmesg;
296 sigemptyset( &action.sa_mask );
297 sigaddset(&action.sa_mask, SIGALRM);
298 sigaddset(&action.sa_mask, SIGTERM);
299 sigaddset(&action.sa_mask, SIGUSR1);
300 sigaddset(&action.sa_mask, SIGHUP);
301 action.sa_flags = SA_RESTART;
302 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
303 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
304 afp_dsi_die(EXITERR_SYS);
306 #endif /* SERVERTEXT */
308 /* SIGUSR1 - set down in 5 minutes */
309 action.sa_handler = afp_dsi_timedown;
310 sigemptyset( &action.sa_mask );
311 sigaddset(&action.sa_mask, SIGALRM);
312 sigaddset(&action.sa_mask, SIGHUP);
313 sigaddset(&action.sa_mask, SIGTERM);
315 sigaddset(&action.sa_mask, SIGUSR2);
317 action.sa_flags = SA_RESTART;
318 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
319 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
320 afp_dsi_die(EXITERR_SYS);
325 action.sa_handler = alarm_handler;
326 sigemptyset(&action.sa_mask);
327 sigaddset(&action.sa_mask, SIGHUP);
328 sigaddset(&action.sa_mask, SIGTERM);
329 sigaddset(&action.sa_mask, SIGUSR1);
331 sigaddset(&action.sa_mask, SIGUSR2);
333 action.sa_flags = SA_RESTART;
334 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
335 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
336 afp_dsi_die(EXITERR_SYS);
338 #endif /* DEBUGGING */
340 /* get stuck here until the end */
341 while ((cmd = dsi_receive(dsi))) {
343 child.flags &= ~CHILD_SLEEPING;
344 dsi_sleep(dsi, 0); /* wake up */
345 if (reload_request) {
347 load_volumes(child.obj);
350 if (cmd == DSIFUNC_TICKLE) {
351 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
352 if ((child.flags & CHILD_DIE))
354 pending_request(dsi);
358 child.flags |= CHILD_DATA;
362 LOG(log_info, logtype_afpd, "done");
369 if ( flushfork( writtenfork ) < 0 ) {
370 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
376 function = (u_char) dsi->commands[0];
378 /* send off an afp command. in a couple cases, we take advantage
379 * of the fact that we're a stream-based protocol. */
380 if (afp_switch[function]) {
381 dsi->datalen = DSI_DATASIZ;
382 child.flags |= CHILD_RUNNING;
384 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", AfpNum2name(function));
386 err = (*afp_switch[function])(obj,
387 (char *)&dsi->commands, dsi->cmdlen,
388 (char *)&dsi->data, &dsi->datalen);
390 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", AfpNum2name(function));
392 /* bring everything back to old euid, egid */
394 restore_uidgid ( &obj->uidgid );
395 #endif /* FORCE_UIDGID */
396 child.flags &= ~CHILD_RUNNING;
398 LOG(log_error, logtype_afpd, "bad function %X", function);
403 /* single shot toggle that gets set by dsi_readinit. */
409 if (!dsi_cmdreply(dsi, err)) {
410 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
411 afp_dsi_die(EXITERR_CLNT);
415 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
416 function = (u_char) dsi->commands[0];
417 if ( afp_switch[ function ] != NULL ) {
418 dsi->datalen = DSI_DATASIZ;
419 child.flags |= CHILD_RUNNING;
420 err = (*afp_switch[function])(obj,
421 (char *)&dsi->commands, dsi->cmdlen,
422 (char *)&dsi->data, &dsi->datalen);
423 child.flags &= ~CHILD_RUNNING;
425 /* bring everything back to old euid, egid */
427 restore_uidgid ( &obj->uidgid );
428 #endif /* FORCE_UIDGID */
430 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
435 if (!dsi_wrtreply(dsi, err)) {
436 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
437 afp_dsi_die(EXITERR_CLNT);
441 case DSIFUNC_ATTN: /* attention replies */
444 /* error. this usually implies a mismatch of some kind
445 * between server and client. if things are correct,
446 * we need to flush the rest of the packet if necessary. */
448 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
449 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
453 pending_request(dsi);
457 afp_dsi_die(EXITERR_CLNT);