2 * $Id: afp_dsi.c,v 1.41 2009-10-21 07:03:08 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;
66 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
67 dsi->read_count/1024.0, dsi->write_count/1024.0);
72 /* -------------------------------
74 * a little bit of code duplication.
76 static void afp_dsi_die(int sig)
78 static volatile int in_handler;
83 /* it's not atomic but we don't care because it's an exit function
84 * ie if a signal is received here, between the test and the affectation,
89 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
90 afp_dsi_close(child.obj);
91 if (sig) /* if no signal, assume dieing because logins are disabled &
92 don't log it (maintenance mode)*/
93 LOG(log_info, logtype_afpd, "Connection terminated");
94 if (sig == SIGTERM || sig == SIGALRM) {
103 static void afp_dsi_sleep(void)
105 child.flags |= CHILD_SLEEPING;
106 dsi_sleep(child.obj->handle, 1);
109 /* ------------------- */
110 static void afp_dsi_timedown(int sig _U_)
115 child.flags |= CHILD_DIE;
116 /* shutdown and don't reconnect. server going down in 5 minutes. */
117 setmessage("The server is going down for maintenance.");
118 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
119 AFPATTN_MESG | AFPATTN_TIME(5));
121 it.it_interval.tv_sec = 0;
122 it.it_interval.tv_usec = 0;
123 it.it_value.tv_sec = 300;
124 it.it_value.tv_usec = 0;
126 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
127 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
128 afp_dsi_die(EXITERR_SYS);
130 memset(&sv, 0, sizeof(sv));
131 sv.sa_handler = afp_dsi_die;
132 sigemptyset( &sv.sa_mask );
133 sigaddset(&sv.sa_mask, SIGHUP);
134 sigaddset(&sv.sa_mask, SIGTERM);
135 sv.sa_flags = SA_RESTART;
136 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
137 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
138 afp_dsi_die(EXITERR_SYS);
142 sv.sa_handler = SIG_IGN;
143 sigemptyset( &sv.sa_mask );
144 sv.sa_flags = SA_RESTART;
145 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
146 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
147 afp_dsi_die(EXITERR_SYS);
152 /* ---------------------------------
153 * SIGHUP reload configuration file
154 * FIXME here or we wait ?
156 volatile int reload_request = 0;
158 static void afp_dsi_reload(int sig _U_)
163 /* ---------------------- */
165 static void afp_dsi_getmesg (int sig _U_)
167 readmessage(child.obj);
168 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
170 #endif /* SERVERTEXT */
172 static void alarm_handler(int sig _U_)
175 DSI *dsi = (DSI *) child.obj->handle;
177 /* we have to restart the timer because some libraries
179 setitimer(ITIMER_REAL, &dsi->timer, NULL);
181 /* we got some traffic from the client since the previous timer
183 if ((child.flags & CHILD_DATA)) {
184 child.flags &= ~CHILD_DATA;
188 /* if we're in the midst of processing something,
190 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
194 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
195 if (!(err = pollvoltime(child.obj)))
196 err = dsi_tickle(child.obj->handle);
198 afp_dsi_die(EXITERR_CLNT);
200 } else { /* didn't receive a tickle. close connection */
201 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
202 afp_dsi_die(EXITERR_CLNT);
208 /* ---------------------------------
209 * old signal handler for SIGUSR1 - set the debug flag and
210 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
212 void afp_set_debug (int sig)
214 char fname[MAXPATHLEN];
216 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
217 freopen(fname, "w", stdout);
218 child.obj->options.flags |= OPTION_DEBUG;
224 /* -------------------------------------------
225 afp over dsi. this never returns.
227 void afp_over_dsi(AFPObj *obj)
229 DSI *dsi = (DSI *) obj->handle;
232 struct sigaction action;
233 const char *afpcmpstr;
235 obj->exit = afp_dsi_die;
236 obj->reply = (int (*)()) dsi_cmdreply;
237 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
239 obj->sleep = afp_dsi_sleep;
241 child.tickle = child.flags = 0;
243 memset(&action, 0, sizeof(action));
246 action.sa_handler = afp_dsi_reload;
247 sigemptyset( &action.sa_mask );
248 sigaddset(&action.sa_mask, SIGALRM);
249 sigaddset(&action.sa_mask, SIGTERM);
250 sigaddset(&action.sa_mask, SIGUSR1);
252 sigaddset(&action.sa_mask, SIGUSR2);
254 action.sa_flags = SA_RESTART;
255 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
256 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
257 afp_dsi_die(EXITERR_SYS);
260 /* install SIGTERM */
261 action.sa_handler = afp_dsi_die;
262 sigemptyset( &action.sa_mask );
263 sigaddset(&action.sa_mask, SIGALRM);
264 sigaddset(&action.sa_mask, SIGHUP);
265 sigaddset(&action.sa_mask, SIGUSR1);
267 sigaddset(&action.sa_mask, SIGUSR2);
269 action.sa_flags = SA_RESTART;
270 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
271 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
272 afp_dsi_die(EXITERR_SYS);
276 /* Added for server message support */
277 action.sa_handler = afp_dsi_getmesg;
278 sigemptyset( &action.sa_mask );
279 sigaddset(&action.sa_mask, SIGALRM);
280 sigaddset(&action.sa_mask, SIGTERM);
281 sigaddset(&action.sa_mask, SIGUSR1);
282 sigaddset(&action.sa_mask, SIGHUP);
283 action.sa_flags = SA_RESTART;
284 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
285 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
286 afp_dsi_die(EXITERR_SYS);
288 #endif /* SERVERTEXT */
290 /* SIGUSR1 - set down in 5 minutes */
291 action.sa_handler = afp_dsi_timedown;
292 sigemptyset( &action.sa_mask );
293 sigaddset(&action.sa_mask, SIGALRM);
294 sigaddset(&action.sa_mask, SIGHUP);
295 sigaddset(&action.sa_mask, SIGTERM);
297 sigaddset(&action.sa_mask, SIGUSR2);
299 action.sa_flags = SA_RESTART;
300 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
301 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
302 afp_dsi_die(EXITERR_SYS);
307 action.sa_handler = alarm_handler;
308 sigemptyset(&action.sa_mask);
309 sigaddset(&action.sa_mask, SIGHUP);
310 sigaddset(&action.sa_mask, SIGTERM);
311 sigaddset(&action.sa_mask, SIGUSR1);
313 sigaddset(&action.sa_mask, SIGUSR2);
315 action.sa_flags = SA_RESTART;
316 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
317 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
318 afp_dsi_die(EXITERR_SYS);
320 #endif /* DEBUGGING */
323 fault_setup((void (*)(void *))afp_dsi_die);
326 /* get stuck here until the end */
327 while ((cmd = dsi_receive(dsi))) {
329 child.flags &= ~CHILD_SLEEPING;
330 dsi_sleep(dsi, 0); /* wake up */
331 if (reload_request) {
333 load_volumes(child.obj);
336 if (cmd == DSIFUNC_TICKLE) {
337 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
338 if ((child.flags & CHILD_DIE))
342 child.flags |= CHILD_DATA;
346 LOG(log_info, logtype_afpd, "done");
348 if (obj->options.flags & OPTION_DEBUG )
357 if ( flushfork( writtenfork ) < 0 ) {
358 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
364 function = (u_char) dsi->commands[0];
366 if (obj->options.flags & OPTION_DEBUG ) {
367 printf("command: %d (%s)\n", function, AfpNum2name(function));
368 bprint((char *) dsi->commands, dsi->cmdlen);
372 /* send off an afp command. in a couple cases, we take advantage
373 * of the fact that we're a stream-based protocol. */
374 if (afp_switch[function]) {
375 dsi->datalen = DSI_DATASIZ;
376 child.flags |= CHILD_RUNNING;
378 afpcmpstr = AfpNum2name(function);
379 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
381 err = (*afp_switch[function])(obj,
382 dsi->commands, dsi->cmdlen,
383 dsi->data, &dsi->datalen);
385 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
387 /* bring everything back to old euid, egid */
389 restore_uidgid ( &obj->uidgid );
390 #endif /* FORCE_UIDGID */
391 child.flags &= ~CHILD_RUNNING;
393 LOG(log_error, logtype_afpd, "bad function %X", function);
398 /* single shot toggle that gets set by dsi_readinit. */
405 if (obj->options.flags & OPTION_DEBUG ) {
406 printf( "reply: %d, %d\n", err, dsi->clientID);
407 bprint((char *) dsi->data, dsi->datalen);
410 if (!dsi_cmdreply(dsi, err)) {
411 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
412 afp_dsi_die(EXITERR_CLNT);
416 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
417 function = (u_char) dsi->commands[0];
419 if ( obj->options.flags & OPTION_DEBUG ) {
420 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
421 bprint((char *) dsi->commands, dsi->cmdlen);
424 if ( afp_switch[ function ] != NULL ) {
425 dsi->datalen = DSI_DATASIZ;
426 child.flags |= CHILD_RUNNING;
427 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
428 dsi->data, &dsi->datalen);
429 child.flags &= ~CHILD_RUNNING;
431 /* bring everything back to old euid, egid */
433 restore_uidgid ( &obj->uidgid );
434 #endif /* FORCE_UIDGID */
436 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
442 if (obj->options.flags & OPTION_DEBUG ) {
443 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
444 bprint((char *) dsi->data, dsi->datalen);
447 if (!dsi_wrtreply(dsi, err)) {
448 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
449 afp_dsi_die(EXITERR_CLNT);
453 case DSIFUNC_ATTN: /* attention replies */
457 /* error. this usually implies a mismatch of some kind
458 * between server and client. if things are correct,
459 * we need to flush the rest of the packet if necessary. */
461 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
462 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
467 if ( obj->options.flags & OPTION_DEBUG ) {
471 of_pforkdesc( stdout );
478 afp_dsi_die(EXITERR_CLNT);