2 * $Id: afp_dsi.c,v 1.35 2009-03-20 09:10:25 franklahm 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;
221 obj->exit = afp_dsi_die;
222 obj->reply = (int (*)()) dsi_cmdreply;
223 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
225 obj->sleep = afp_dsi_sleep;
227 child.tickle = child.flags = 0;
229 memset(&action, 0, sizeof(action));
232 action.sa_handler = afp_dsi_reload;
233 sigemptyset( &action.sa_mask );
234 sigaddset(&action.sa_mask, SIGALRM);
235 sigaddset(&action.sa_mask, SIGTERM);
236 sigaddset(&action.sa_mask, SIGUSR1);
238 sigaddset(&action.sa_mask, SIGUSR2);
240 action.sa_flags = SA_RESTART;
241 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
242 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
243 afp_dsi_die(EXITERR_SYS);
246 /* install SIGTERM */
247 action.sa_handler = afp_dsi_die;
248 sigemptyset( &action.sa_mask );
249 sigaddset(&action.sa_mask, SIGALRM);
250 sigaddset(&action.sa_mask, SIGHUP);
251 sigaddset(&action.sa_mask, SIGUSR1);
253 sigaddset(&action.sa_mask, SIGUSR2);
255 action.sa_flags = SA_RESTART;
256 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
257 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
258 afp_dsi_die(EXITERR_SYS);
262 /* Added for server message support */
263 action.sa_handler = afp_dsi_getmesg;
264 sigemptyset( &action.sa_mask );
265 sigaddset(&action.sa_mask, SIGALRM);
266 sigaddset(&action.sa_mask, SIGTERM);
267 sigaddset(&action.sa_mask, SIGUSR1);
268 sigaddset(&action.sa_mask, SIGHUP);
269 action.sa_flags = SA_RESTART;
270 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
271 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
272 afp_dsi_die(EXITERR_SYS);
274 #endif /* SERVERTEXT */
276 /* SIGUSR1 - set down in 5 minutes */
277 action.sa_handler = afp_dsi_timedown;
278 sigemptyset( &action.sa_mask );
279 sigaddset(&action.sa_mask, SIGALRM);
280 sigaddset(&action.sa_mask, SIGHUP);
281 sigaddset(&action.sa_mask, SIGTERM);
283 sigaddset(&action.sa_mask, SIGUSR2);
285 action.sa_flags = SA_RESTART;
286 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
287 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
288 afp_dsi_die(EXITERR_SYS);
293 action.sa_handler = alarm_handler;
294 sigemptyset(&action.sa_mask);
295 sigaddset(&action.sa_mask, SIGHUP);
296 sigaddset(&action.sa_mask, SIGTERM);
297 sigaddset(&action.sa_mask, SIGUSR1);
299 sigaddset(&action.sa_mask, SIGUSR2);
301 action.sa_flags = SA_RESTART;
302 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
303 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
304 afp_dsi_die(EXITERR_SYS);
306 #endif /* DEBUGGING */
309 fault_setup((void (*)(void *))afp_dsi_die);
312 /* get stuck here until the end */
313 while ((cmd = dsi_receive(dsi))) {
315 child.flags &= ~CHILD_SLEEPING;
316 dsi_sleep(dsi, 0); /* wake up */
317 if (reload_request) {
319 load_volumes(child.obj);
322 if (cmd == DSIFUNC_TICKLE) {
323 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
324 if ((child.flags & CHILD_DIE))
327 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
329 setitimer(ITIMER_REAL, &dsi->timer, NULL);
335 LOG(log_info, logtype_afpd, "done");
337 if (obj->options.flags & OPTION_DEBUG )
346 if ( flushfork( writtenfork ) < 0 ) {
347 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
353 function = (u_char) dsi->commands[0];
355 if (obj->options.flags & OPTION_DEBUG ) {
356 printf("command: %d (%s)\n", function, AfpNum2name(function));
357 bprint((char *) dsi->commands, dsi->cmdlen);
361 /* send off an afp command. in a couple cases, we take advantage
362 * of the fact that we're a stream-based protocol. */
363 if (afp_switch[function]) {
364 dsi->datalen = DSI_DATASIZ;
365 child.flags |= CHILD_RUNNING;
367 err = (*afp_switch[function])(obj,
368 dsi->commands, dsi->cmdlen,
369 dsi->data, &dsi->datalen);
371 /* bring everything back to old euid, egid */
373 restore_uidgid ( &obj->uidgid );
374 #endif /* FORCE_UIDGID */
375 child.flags &= ~CHILD_RUNNING;
377 LOG(log_error, logtype_afpd, "bad function %X", function);
382 /* single shot toggle that gets set by dsi_readinit. */
389 if (obj->options.flags & OPTION_DEBUG ) {
390 printf( "reply: %d, %d\n", err, dsi->clientID);
391 bprint((char *) dsi->data, dsi->datalen);
394 if (!dsi_cmdreply(dsi, err)) {
395 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
396 afp_dsi_die(EXITERR_CLNT);
400 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
401 function = (u_char) dsi->commands[0];
403 if ( obj->options.flags & OPTION_DEBUG ) {
404 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
405 bprint((char *) dsi->commands, dsi->cmdlen);
408 if ( afp_switch[ function ] != NULL ) {
409 dsi->datalen = DSI_DATASIZ;
410 child.flags |= CHILD_RUNNING;
411 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
412 dsi->data, &dsi->datalen);
413 child.flags &= ~CHILD_RUNNING;
415 /* bring everything back to old euid, egid */
417 restore_uidgid ( &obj->uidgid );
418 #endif /* FORCE_UIDGID */
420 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
426 if (obj->options.flags & OPTION_DEBUG ) {
427 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
428 bprint((char *) dsi->data, dsi->datalen);
431 if (!dsi_wrtreply(dsi, err)) {
432 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
433 afp_dsi_die(EXITERR_CLNT);
437 case DSIFUNC_ATTN: /* attention replies */
441 /* error. this usually implies a mismatch of some kind
442 * between server and client. if things are correct,
443 * we need to flush the rest of the packet if necessary. */
445 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
446 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
451 if ( obj->options.flags & OPTION_DEBUG ) {
455 of_pforkdesc( stdout );
462 afp_dsi_die(EXITERR_CLNT);