2 * $Id: afp_dsi.c,v 1.40 2009-10-14 02:24:05 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)
57 static void afp_dsi_close(AFPObj *obj)
59 DSI *dsi = obj->handle;
65 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
66 dsi->read_count/1024.0, dsi->write_count/1024.0);
71 /* -------------------------------
73 * a little bit of code duplication.
75 static void afp_dsi_die(int sig)
77 static volatile int in_handler;
82 /* it's not atomic but we don't care because it's an exit function
83 * ie if a signal is received here, between the test and the affectation,
88 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
89 afp_dsi_close(child.obj);
90 if (sig) /* if no signal, assume dieing because logins are disabled &
91 don't log it (maintenance mode)*/
92 LOG(log_info, logtype_afpd, "Connection terminated");
93 if (sig == SIGTERM || sig == SIGALRM) {
102 static void afp_dsi_sleep(void)
104 child.flags |= CHILD_SLEEPING;
105 dsi_sleep(child.obj->handle, 1);
108 /* ------------------- */
109 static void afp_dsi_timedown(int sig _U_)
114 child.flags |= CHILD_DIE;
115 /* shutdown and don't reconnect. server going down in 5 minutes. */
116 setmessage("The server is going down for maintenance.");
117 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
118 AFPATTN_MESG | AFPATTN_TIME(5));
120 it.it_interval.tv_sec = 0;
121 it.it_interval.tv_usec = 0;
122 it.it_value.tv_sec = 300;
123 it.it_value.tv_usec = 0;
125 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
126 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
127 afp_dsi_die(EXITERR_SYS);
129 memset(&sv, 0, sizeof(sv));
130 sv.sa_handler = afp_dsi_die;
131 sigemptyset( &sv.sa_mask );
132 sigaddset(&sv.sa_mask, SIGHUP);
133 sigaddset(&sv.sa_mask, SIGTERM);
134 sv.sa_flags = SA_RESTART;
135 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
136 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
137 afp_dsi_die(EXITERR_SYS);
141 sv.sa_handler = SIG_IGN;
142 sigemptyset( &sv.sa_mask );
143 sv.sa_flags = SA_RESTART;
144 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
145 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
146 afp_dsi_die(EXITERR_SYS);
151 /* ---------------------------------
152 * SIGHUP reload configuration file
153 * FIXME here or we wait ?
155 volatile int reload_request = 0;
157 static void afp_dsi_reload(int sig _U_)
162 /* ---------------------- */
164 static void afp_dsi_getmesg (int sig _U_)
166 readmessage(child.obj);
167 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
169 #endif /* SERVERTEXT */
171 static void alarm_handler(int sig _U_)
175 /* if we're in the midst of processing something,
177 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
179 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
180 if (!(err = pollvoltime(child.obj)))
181 err = dsi_tickle(child.obj->handle);
183 afp_dsi_die(EXITERR_CLNT);
185 } else { /* didn't receive a tickle. close connection */
186 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
187 afp_dsi_die(EXITERR_CLNT);
193 /* ---------------------------------
194 * old signal handler for SIGUSR1 - set the debug flag and
195 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
197 void afp_set_debug (int sig)
199 char fname[MAXPATHLEN];
201 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
202 freopen(fname, "w", stdout);
203 child.obj->options.flags |= OPTION_DEBUG;
209 /* -------------------------------------------
210 afp over dsi. this never returns.
212 void afp_over_dsi(AFPObj *obj)
214 DSI *dsi = (DSI *) obj->handle;
217 struct sigaction action;
218 const char *afpcmpstr;
220 obj->exit = afp_dsi_die;
221 obj->reply = (int (*)()) dsi_cmdreply;
222 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
224 obj->sleep = afp_dsi_sleep;
226 child.tickle = child.flags = 0;
228 memset(&action, 0, sizeof(action));
231 action.sa_handler = afp_dsi_reload;
232 sigemptyset( &action.sa_mask );
233 sigaddset(&action.sa_mask, SIGALRM);
234 sigaddset(&action.sa_mask, SIGTERM);
235 sigaddset(&action.sa_mask, SIGUSR1);
237 sigaddset(&action.sa_mask, SIGUSR2);
239 action.sa_flags = SA_RESTART;
240 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
241 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
242 afp_dsi_die(EXITERR_SYS);
245 /* install SIGTERM */
246 action.sa_handler = afp_dsi_die;
247 sigemptyset( &action.sa_mask );
248 sigaddset(&action.sa_mask, SIGALRM);
249 sigaddset(&action.sa_mask, SIGHUP);
250 sigaddset(&action.sa_mask, SIGUSR1);
252 sigaddset(&action.sa_mask, SIGUSR2);
254 action.sa_flags = SA_RESTART;
255 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
256 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
257 afp_dsi_die(EXITERR_SYS);
261 /* Added for server message support */
262 action.sa_handler = afp_dsi_getmesg;
263 sigemptyset( &action.sa_mask );
264 sigaddset(&action.sa_mask, SIGALRM);
265 sigaddset(&action.sa_mask, SIGTERM);
266 sigaddset(&action.sa_mask, SIGUSR1);
267 sigaddset(&action.sa_mask, SIGHUP);
268 action.sa_flags = SA_RESTART;
269 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
270 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
271 afp_dsi_die(EXITERR_SYS);
273 #endif /* SERVERTEXT */
275 /* SIGUSR1 - set down in 5 minutes */
276 action.sa_handler = afp_dsi_timedown;
277 sigemptyset( &action.sa_mask );
278 sigaddset(&action.sa_mask, SIGALRM);
279 sigaddset(&action.sa_mask, SIGHUP);
280 sigaddset(&action.sa_mask, SIGTERM);
282 sigaddset(&action.sa_mask, SIGUSR2);
284 action.sa_flags = SA_RESTART;
285 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
286 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
287 afp_dsi_die(EXITERR_SYS);
292 action.sa_handler = alarm_handler;
293 sigemptyset(&action.sa_mask);
294 sigaddset(&action.sa_mask, SIGHUP);
295 sigaddset(&action.sa_mask, SIGTERM);
296 sigaddset(&action.sa_mask, SIGUSR1);
298 sigaddset(&action.sa_mask, SIGUSR2);
300 action.sa_flags = SA_RESTART;
301 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
302 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
303 afp_dsi_die(EXITERR_SYS);
305 #endif /* DEBUGGING */
308 fault_setup((void (*)(void *))afp_dsi_die);
311 /* get stuck here until the end */
312 while ((cmd = dsi_receive(dsi))) {
314 child.flags &= ~CHILD_SLEEPING;
315 dsi_sleep(dsi, 0); /* wake up */
316 if (reload_request) {
318 load_volumes(child.obj);
321 if (cmd == DSIFUNC_TICKLE) {
322 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
323 if ((child.flags & CHILD_DIE))
326 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
328 setitimer(ITIMER_REAL, &dsi->timer, NULL);
334 LOG(log_info, logtype_afpd, "done");
336 if (obj->options.flags & OPTION_DEBUG )
345 if ( flushfork( writtenfork ) < 0 ) {
346 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
352 function = (u_char) dsi->commands[0];
354 if (obj->options.flags & OPTION_DEBUG ) {
355 printf("command: %d (%s)\n", function, AfpNum2name(function));
356 bprint((char *) dsi->commands, dsi->cmdlen);
360 /* send off an afp command. in a couple cases, we take advantage
361 * of the fact that we're a stream-based protocol. */
362 if (afp_switch[function]) {
363 dsi->datalen = DSI_DATASIZ;
364 child.flags |= CHILD_RUNNING;
366 afpcmpstr = AfpNum2name(function);
367 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
369 err = (*afp_switch[function])(obj,
370 dsi->commands, dsi->cmdlen,
371 dsi->data, &dsi->datalen);
373 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
375 /* bring everything back to old euid, egid */
377 restore_uidgid ( &obj->uidgid );
378 #endif /* FORCE_UIDGID */
379 child.flags &= ~CHILD_RUNNING;
381 LOG(log_error, logtype_afpd, "bad function %X", function);
386 /* single shot toggle that gets set by dsi_readinit. */
393 if (obj->options.flags & OPTION_DEBUG ) {
394 printf( "reply: %d, %d\n", err, dsi->clientID);
395 bprint((char *) dsi->data, dsi->datalen);
398 if (!dsi_cmdreply(dsi, err)) {
399 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
400 afp_dsi_die(EXITERR_CLNT);
404 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
405 function = (u_char) dsi->commands[0];
407 if ( obj->options.flags & OPTION_DEBUG ) {
408 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
409 bprint((char *) dsi->commands, dsi->cmdlen);
412 if ( afp_switch[ function ] != NULL ) {
413 dsi->datalen = DSI_DATASIZ;
414 child.flags |= CHILD_RUNNING;
415 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
416 dsi->data, &dsi->datalen);
417 child.flags &= ~CHILD_RUNNING;
419 /* bring everything back to old euid, egid */
421 restore_uidgid ( &obj->uidgid );
422 #endif /* FORCE_UIDGID */
424 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
430 if (obj->options.flags & OPTION_DEBUG ) {
431 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
432 bprint((char *) dsi->data, dsi->datalen);
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 */
445 /* error. this usually implies a mismatch of some kind
446 * between server and client. if things are correct,
447 * we need to flush the rest of the packet if necessary. */
449 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
450 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
455 if ( obj->options.flags & OPTION_DEBUG ) {
459 of_pforkdesc( stdout );
466 afp_dsi_die(EXITERR_CLNT);