2 * $Id: afp_dsi.c,v 1.37 2009-07-21 13:41:16 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 extern struct oforks *writtenfork;
48 #define CHILD_DIE (1 << 0)
49 #define CHILD_RUNNING (1 << 1)
50 #define CHILD_SLEEPING (1 << 2)
59 static void afp_dsi_close(AFPObj *obj)
61 DSI *dsi = obj->handle;
67 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
68 dsi->read_count/1024.0, dsi->write_count/1024.0);
73 /* -------------------------------
75 * a little bit of code duplication.
77 static void afp_dsi_die(int sig)
79 static volatile int in_handler;
84 /* it's not atomic but we don't care because it's an exit function
85 * ie if a signal is received here, between the test and the affectation,
90 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
91 afp_dsi_close(child.obj);
92 if (sig) /* if no signal, assume dieing because logins are disabled &
93 don't log it (maintenance mode)*/
94 LOG(log_info, logtype_afpd, "Connection terminated");
95 if (sig == SIGTERM || sig == SIGALRM) {
104 static void afp_dsi_sleep(void)
106 child.flags |= CHILD_SLEEPING;
107 dsi_sleep(child.obj->handle, 1);
110 /* ------------------- */
111 static void afp_dsi_timedown()
116 child.flags |= CHILD_DIE;
117 /* shutdown and don't reconnect. server going down in 5 minutes. */
118 setmessage("The server is going down for maintenance.");
119 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
120 AFPATTN_MESG | AFPATTN_TIME(5));
122 it.it_interval.tv_sec = 0;
123 it.it_interval.tv_usec = 0;
124 it.it_value.tv_sec = 300;
125 it.it_value.tv_usec = 0;
127 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
128 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
129 afp_dsi_die(EXITERR_SYS);
131 memset(&sv, 0, sizeof(sv));
132 sv.sa_handler = afp_dsi_die;
133 sigemptyset( &sv.sa_mask );
134 sigaddset(&sv.sa_mask, SIGHUP);
135 sigaddset(&sv.sa_mask, SIGTERM);
136 sv.sa_flags = SA_RESTART;
137 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
138 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
139 afp_dsi_die(EXITERR_SYS);
143 sv.sa_handler = SIG_IGN;
144 sigemptyset( &sv.sa_mask );
145 sv.sa_flags = SA_RESTART;
146 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
147 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
148 afp_dsi_die(EXITERR_SYS);
153 /* ---------------------------------
154 * SIGHUP reload configuration file
155 * FIXME here or we wait ?
157 volatile int reload_request = 0;
159 static void afp_dsi_reload()
164 /* ---------------------- */
166 static void afp_dsi_getmesg (int sig _U_)
168 readmessage(child.obj);
169 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
171 #endif /* SERVERTEXT */
173 static void alarm_handler()
177 /* if we're in the midst of processing something,
179 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
181 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
182 if (!(err = pollvoltime(child.obj)))
183 err = dsi_tickle(child.obj->handle);
185 afp_dsi_die(EXITERR_CLNT);
187 } else { /* didn't receive a tickle. close connection */
188 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
189 afp_dsi_die(EXITERR_CLNT);
195 /* ---------------------------------
196 * old signal handler for SIGUSR1 - set the debug flag and
197 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
199 void afp_set_debug (int sig)
201 char fname[MAXPATHLEN];
203 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
204 freopen(fname, "w", stdout);
205 child.obj->options.flags |= OPTION_DEBUG;
211 /* -------------------------------------------
212 afp over dsi. this never returns.
214 void afp_over_dsi(AFPObj *obj)
216 DSI *dsi = (DSI *) obj->handle;
219 struct sigaction action;
220 const char *afpcmpstr;
222 obj->exit = afp_dsi_die;
223 obj->reply = (int (*)()) dsi_cmdreply;
224 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
226 obj->sleep = afp_dsi_sleep;
228 child.tickle = child.flags = 0;
230 memset(&action, 0, sizeof(action));
233 action.sa_handler = afp_dsi_reload;
234 sigemptyset( &action.sa_mask );
235 sigaddset(&action.sa_mask, SIGALRM);
236 sigaddset(&action.sa_mask, SIGTERM);
237 sigaddset(&action.sa_mask, SIGUSR1);
239 sigaddset(&action.sa_mask, SIGUSR2);
241 action.sa_flags = SA_RESTART;
242 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
243 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
244 afp_dsi_die(EXITERR_SYS);
247 /* install SIGTERM */
248 action.sa_handler = afp_dsi_die;
249 sigemptyset( &action.sa_mask );
250 sigaddset(&action.sa_mask, SIGALRM);
251 sigaddset(&action.sa_mask, SIGHUP);
252 sigaddset(&action.sa_mask, SIGUSR1);
254 sigaddset(&action.sa_mask, SIGUSR2);
256 action.sa_flags = SA_RESTART;
257 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
258 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
259 afp_dsi_die(EXITERR_SYS);
263 /* Added for server message support */
264 action.sa_handler = afp_dsi_getmesg;
265 sigemptyset( &action.sa_mask );
266 sigaddset(&action.sa_mask, SIGALRM);
267 sigaddset(&action.sa_mask, SIGTERM);
268 sigaddset(&action.sa_mask, SIGUSR1);
269 sigaddset(&action.sa_mask, SIGHUP);
270 action.sa_flags = SA_RESTART;
271 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
272 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
273 afp_dsi_die(EXITERR_SYS);
275 #endif /* SERVERTEXT */
277 /* SIGUSR1 - set down in 5 minutes */
278 action.sa_handler = afp_dsi_timedown;
279 sigemptyset( &action.sa_mask );
280 sigaddset(&action.sa_mask, SIGALRM);
281 sigaddset(&action.sa_mask, SIGHUP);
282 sigaddset(&action.sa_mask, SIGTERM);
284 sigaddset(&action.sa_mask, SIGUSR2);
286 action.sa_flags = SA_RESTART;
287 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
288 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
289 afp_dsi_die(EXITERR_SYS);
294 action.sa_handler = alarm_handler;
295 sigemptyset(&action.sa_mask);
296 sigaddset(&action.sa_mask, SIGHUP);
297 sigaddset(&action.sa_mask, SIGTERM);
298 sigaddset(&action.sa_mask, SIGUSR1);
300 sigaddset(&action.sa_mask, SIGUSR2);
302 action.sa_flags = SA_RESTART;
303 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
304 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
305 afp_dsi_die(EXITERR_SYS);
307 #endif /* DEBUGGING */
310 fault_setup((void (*)(void *))afp_dsi_die);
313 /* get stuck here until the end */
314 while ((cmd = dsi_receive(dsi))) {
316 child.flags &= ~CHILD_SLEEPING;
317 dsi_sleep(dsi, 0); /* wake up */
318 if (reload_request) {
320 load_volumes(child.obj);
323 if (cmd == DSIFUNC_TICKLE) {
324 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
325 if ((child.flags & CHILD_DIE))
328 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
330 setitimer(ITIMER_REAL, &dsi->timer, NULL);
336 LOG(log_info, logtype_afpd, "done");
338 if (obj->options.flags & OPTION_DEBUG )
347 if ( flushfork( writtenfork ) < 0 ) {
348 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
354 function = (u_char) dsi->commands[0];
356 if (obj->options.flags & OPTION_DEBUG ) {
357 printf("command: %d (%s)\n", function, AfpNum2name(function));
358 bprint((char *) dsi->commands, dsi->cmdlen);
362 /* send off an afp command. in a couple cases, we take advantage
363 * of the fact that we're a stream-based protocol. */
364 if (afp_switch[function]) {
365 dsi->datalen = DSI_DATASIZ;
366 child.flags |= CHILD_RUNNING;
368 afpcmpstr = AfpNum2name(function);
369 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
371 err = (*afp_switch[function])(obj,
372 dsi->commands, dsi->cmdlen,
373 dsi->data, &dsi->datalen);
375 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
377 /* bring everything back to old euid, egid */
379 restore_uidgid ( &obj->uidgid );
380 #endif /* FORCE_UIDGID */
381 child.flags &= ~CHILD_RUNNING;
383 LOG(log_error, logtype_afpd, "bad function %X", function);
388 /* single shot toggle that gets set by dsi_readinit. */
395 if (obj->options.flags & OPTION_DEBUG ) {
396 printf( "reply: %d, %d\n", err, dsi->clientID);
397 bprint((char *) dsi->data, dsi->datalen);
400 if (!dsi_cmdreply(dsi, err)) {
401 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
402 afp_dsi_die(EXITERR_CLNT);
406 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
407 function = (u_char) dsi->commands[0];
409 if ( obj->options.flags & OPTION_DEBUG ) {
410 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
411 bprint((char *) dsi->commands, dsi->cmdlen);
414 if ( afp_switch[ function ] != NULL ) {
415 dsi->datalen = DSI_DATASIZ;
416 child.flags |= CHILD_RUNNING;
417 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
418 dsi->data, &dsi->datalen);
419 child.flags &= ~CHILD_RUNNING;
421 /* bring everything back to old euid, egid */
423 restore_uidgid ( &obj->uidgid );
424 #endif /* FORCE_UIDGID */
426 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
432 if (obj->options.flags & OPTION_DEBUG ) {
433 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
434 bprint((char *) dsi->data, dsi->datalen);
437 if (!dsi_wrtreply(dsi, err)) {
438 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
439 afp_dsi_die(EXITERR_CLNT);
443 case DSIFUNC_ATTN: /* attention replies */
447 /* error. this usually implies a mismatch of some kind
448 * between server and client. if things are correct,
449 * we need to flush the rest of the packet if necessary. */
451 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
452 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
457 if ( obj->options.flags & OPTION_DEBUG ) {
461 of_pforkdesc( stdout );
468 afp_dsi_die(EXITERR_CLNT);