2 * $Id: afp_dsi.c,v 1.32 2005-04-28 20:49:39 bfernhomberg 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 __inline__ void afp_dsi_close(AFPObj *obj)
61 DSI *dsi = obj->handle;
67 /* UAM had syslog control; afpd needs to reassert itself */
68 set_processname("afpd");
69 syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
70 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
71 dsi->read_count/1024.0, dsi->write_count/1024.0);
76 /* -------------------------------
78 * a little bit of code duplication.
80 static void afp_dsi_die(int sig)
82 static volatile int in_handler;
87 /* it's not atomic but we don't care because it's an exit function
88 * ie if a signal is received here, between the test and the affectation,
93 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
94 afp_dsi_close(child.obj);
95 if (sig) /* if no signal, assume dieing because logins are disabled &
96 don't log it (maintenance mode)*/
97 LOG(log_info, logtype_afpd, "Connection terminated");
98 if (sig == SIGTERM || sig == SIGALRM) {
107 static void afp_dsi_sleep(void)
109 child.flags |= CHILD_SLEEPING;
110 dsi_sleep(child.obj->handle, 1);
113 /* ------------------- */
114 static void afp_dsi_timedown()
119 child.flags |= CHILD_DIE;
120 /* shutdown and don't reconnect. server going down in 5 minutes. */
121 setmessage("The server is going down for maintenance.");
122 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
123 AFPATTN_MESG | AFPATTN_TIME(5));
125 it.it_interval.tv_sec = 0;
126 it.it_interval.tv_usec = 0;
127 it.it_value.tv_sec = 300;
128 it.it_value.tv_usec = 0;
130 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
131 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
132 afp_dsi_die(EXITERR_SYS);
134 memset(&sv, 0, sizeof(sv));
135 sv.sa_handler = afp_dsi_die;
136 sigemptyset( &sv.sa_mask );
137 sigaddset(&sv.sa_mask, SIGHUP);
138 sigaddset(&sv.sa_mask, SIGTERM);
139 sv.sa_flags = SA_RESTART;
140 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
141 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
142 afp_dsi_die(EXITERR_SYS);
146 sv.sa_handler = SIG_IGN;
147 sigemptyset( &sv.sa_mask );
148 sv.sa_flags = SA_RESTART;
149 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
150 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
151 afp_dsi_die(EXITERR_SYS);
156 /* ---------------------------------
157 * SIGHUP reload configuration file
158 * FIXME here or we wait ?
160 volatile int reload_request = 0;
162 static void afp_dsi_reload()
167 /* ---------------------- */
169 static void afp_dsi_getmesg (int sig _U_)
171 readmessage(child.obj);
172 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
174 #endif /* SERVERTEXT */
176 static void alarm_handler()
180 /* if we're in the midst of processing something,
182 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
184 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
185 if (!(err = pollvoltime(child.obj)))
186 err = dsi_tickle(child.obj->handle);
188 afp_dsi_die(EXITERR_CLNT);
190 } else { /* didn't receive a tickle. close connection */
191 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
192 afp_dsi_die(EXITERR_CLNT);
198 /* ---------------------------------
199 * old signal handler for SIGUSR1 - set the debug flag and
200 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
202 void afp_set_debug (int sig)
204 char fname[MAXPATHLEN];
206 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
207 freopen(fname, "w", stdout);
208 child.obj->options.flags |= OPTION_DEBUG;
214 /* -------------------------------------------
215 afp over dsi. this never returns.
217 void afp_over_dsi(AFPObj *obj)
219 DSI *dsi = (DSI *) obj->handle;
222 struct sigaction action;
224 obj->exit = afp_dsi_die;
225 obj->reply = (int (*)()) dsi_cmdreply;
226 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
228 obj->sleep = afp_dsi_sleep;
230 child.tickle = child.flags = 0;
232 memset(&action, 0, sizeof(action));
235 action.sa_handler = afp_dsi_reload;
236 sigemptyset( &action.sa_mask );
237 sigaddset(&action.sa_mask, SIGALRM);
238 sigaddset(&action.sa_mask, SIGTERM);
239 sigaddset(&action.sa_mask, SIGUSR1);
241 sigaddset(&action.sa_mask, SIGUSR2);
243 action.sa_flags = SA_RESTART;
244 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
245 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
246 afp_dsi_die(EXITERR_SYS);
249 /* install SIGTERM */
250 action.sa_handler = afp_dsi_die;
251 sigemptyset( &action.sa_mask );
252 sigaddset(&action.sa_mask, SIGALRM);
253 sigaddset(&action.sa_mask, SIGHUP);
254 sigaddset(&action.sa_mask, SIGUSR1);
256 sigaddset(&action.sa_mask, SIGUSR2);
258 action.sa_flags = SA_RESTART;
259 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
260 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
261 afp_dsi_die(EXITERR_SYS);
265 /* Added for server message support */
266 action.sa_handler = afp_dsi_getmesg;
267 sigemptyset( &action.sa_mask );
268 sigaddset(&action.sa_mask, SIGALRM);
269 sigaddset(&action.sa_mask, SIGTERM);
270 sigaddset(&action.sa_mask, SIGUSR1);
271 sigaddset(&action.sa_mask, SIGHUP);
272 action.sa_flags = SA_RESTART;
273 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
274 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
275 afp_dsi_die(EXITERR_SYS);
277 #endif /* SERVERTEXT */
279 /* SIGUSR1 - set down in 5 minutes */
280 action.sa_handler = afp_dsi_timedown;
281 sigemptyset( &action.sa_mask );
282 sigaddset(&action.sa_mask, SIGALRM);
283 sigaddset(&action.sa_mask, SIGHUP);
284 sigaddset(&action.sa_mask, SIGTERM);
286 sigaddset(&action.sa_mask, SIGUSR2);
288 action.sa_flags = SA_RESTART;
289 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
290 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
291 afp_dsi_die(EXITERR_SYS);
295 action.sa_handler = alarm_handler;
296 sigemptyset(&action.sa_mask);
297 sigaddset(&action.sa_mask, SIGHUP);
298 sigaddset(&action.sa_mask, SIGTERM);
299 sigaddset(&action.sa_mask, SIGUSR1);
301 sigaddset(&action.sa_mask, SIGUSR2);
303 action.sa_flags = SA_RESTART;
304 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
305 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
306 afp_dsi_die(EXITERR_SYS);
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 */
329 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 err = (*afp_switch[function])(obj,
367 dsi->commands, dsi->cmdlen,
368 dsi->data, &dsi->datalen);
370 /* bring everything back to old euid, egid */
372 restore_uidgid ( &obj->uidgid );
373 #endif /* FORCE_UIDGID */
374 child.flags &= ~CHILD_RUNNING;
376 LOG(log_error, logtype_afpd, "bad function %X", function);
381 /* single shot toggle that gets set by dsi_readinit. */
388 if (obj->options.flags & OPTION_DEBUG ) {
389 printf( "reply: %d, %d\n", err, dsi->clientID);
390 bprint((char *) dsi->data, dsi->datalen);
393 if (!dsi_cmdreply(dsi, err)) {
394 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
395 afp_dsi_die(EXITERR_CLNT);
399 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
400 function = (u_char) dsi->commands[0];
402 if ( obj->options.flags & OPTION_DEBUG ) {
403 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
404 bprint((char *) dsi->commands, dsi->cmdlen);
407 if ( afp_switch[ function ] != NULL ) {
408 dsi->datalen = DSI_DATASIZ;
409 child.flags |= CHILD_RUNNING;
410 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
411 dsi->data, &dsi->datalen);
412 child.flags &= ~CHILD_RUNNING;
414 /* bring everything back to old euid, egid */
416 restore_uidgid ( &obj->uidgid );
417 #endif /* FORCE_UIDGID */
419 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
425 if (obj->options.flags & OPTION_DEBUG ) {
426 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
427 bprint((char *) dsi->data, dsi->datalen);
430 if (!dsi_wrtreply(dsi, err)) {
431 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
432 afp_dsi_die(EXITERR_CLNT);
436 case DSIFUNC_ATTN: /* attention replies */
440 /* error. this usually implies a mismatch of some kind
441 * between server and client. if things are correct,
442 * we need to flush the rest of the packet if necessary. */
444 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
445 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
450 if ( obj->options.flags & OPTION_DEBUG ) {
454 of_pforkdesc( stdout );
461 afp_dsi_die(EXITERR_CLNT);