2 * $Id: afp_dsi.c,v 1.27.2.3.2.4 2004-05-04 15:38:24 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 __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 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
83 afp_dsi_close(child.obj);
84 if (sig) /* if no signal, assume dieing because logins are disabled &
85 don't log it (maintenance mode)*/
86 LOG(log_info, logtype_afpd, "Connection terminated");
87 if (sig == SIGTERM || sig == SIGALRM) {
96 static void afp_dsi_sleep(void)
98 child.flags |= CHILD_SLEEPING;
99 dsi_sleep(child.obj->handle, 1);
102 /* ------------------- */
103 static void afp_dsi_timedown()
108 child.flags |= CHILD_DIE;
109 /* shutdown and don't reconnect. server going down in 5 minutes. */
110 setmessage("The server is going down for maintenance.");
111 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
112 AFPATTN_MESG | AFPATTN_TIME(5));
114 it.it_interval.tv_sec = 0;
115 it.it_interval.tv_usec = 0;
116 it.it_value.tv_sec = 300;
117 it.it_value.tv_usec = 0;
119 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
120 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
121 afp_dsi_die(EXITERR_SYS);
123 memset(&sv, 0, sizeof(sv));
124 sv.sa_handler = afp_dsi_die;
125 sigemptyset( &sv.sa_mask );
126 sigaddset(&sv.sa_mask, SIGHUP);
127 sigaddset(&sv.sa_mask, SIGTERM);
128 sv.sa_flags = SA_RESTART;
129 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
130 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
131 afp_dsi_die(EXITERR_SYS);
135 sv.sa_handler = SIG_IGN;
136 sigemptyset( &sv.sa_mask );
137 sv.sa_flags = SA_RESTART;
138 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
139 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
140 afp_dsi_die(EXITERR_SYS);
145 /* ---------------------------------
146 * SIGHUP reload configuration file
147 * FIXME here or we wait ?
149 volatile int reload_request = 0;
151 static void afp_dsi_reload()
156 /* ---------------------- */
158 static void afp_dsi_getmesg (int sig)
160 readmessage(child.obj);
161 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
163 #endif /* SERVERTEXT */
165 static void alarm_handler()
169 /* if we're in the midst of processing something,
171 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
173 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
174 if (!(err = pollvoltime(child.obj)))
175 err = dsi_tickle(child.obj->handle);
177 afp_dsi_die(EXITERR_CLNT);
179 } else { /* didn't receive a tickle. close connection */
180 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
181 afp_dsi_die(EXITERR_CLNT);
187 /* ---------------------------------
188 * old signal handler for SIGUSR1 - set the debug flag and
189 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
191 void afp_set_debug (int sig)
193 char fname[MAXPATHLEN];
195 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
196 freopen(fname, "w", stdout);
197 child.obj->options.flags |= OPTION_DEBUG;
203 /* -------------------------------------------
204 afp over dsi. this never returns.
206 void afp_over_dsi(AFPObj *obj)
208 DSI *dsi = (DSI *) obj->handle;
211 struct sigaction action;
213 obj->exit = afp_dsi_die;
214 obj->reply = (int (*)()) dsi_cmdreply;
215 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
217 obj->sleep = afp_dsi_sleep;
219 child.tickle = child.flags = 0;
221 memset(&action, 0, sizeof(action));
224 action.sa_handler = afp_dsi_reload;
225 sigemptyset( &action.sa_mask );
226 sigaddset(&action.sa_mask, SIGALRM);
227 sigaddset(&action.sa_mask, SIGTERM);
228 sigaddset(&action.sa_mask, SIGUSR1);
230 sigaddset(&action.sa_mask, SIGUSR2);
232 action.sa_flags = SA_RESTART;
233 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
234 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
235 afp_dsi_die(EXITERR_SYS);
238 /* install SIGTERM */
239 action.sa_handler = afp_dsi_die;
240 sigemptyset( &action.sa_mask );
241 sigaddset(&action.sa_mask, SIGALRM);
242 sigaddset(&action.sa_mask, SIGHUP);
243 sigaddset(&action.sa_mask, SIGUSR1);
245 sigaddset(&action.sa_mask, SIGUSR2);
247 action.sa_flags = SA_RESTART;
248 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
249 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
250 afp_dsi_die(EXITERR_SYS);
254 /* Added for server message support */
255 action.sa_handler = afp_dsi_getmesg;
256 sigemptyset( &action.sa_mask );
257 sigaddset(&action.sa_mask, SIGALRM);
258 sigaddset(&action.sa_mask, SIGTERM);
259 sigaddset(&action.sa_mask, SIGUSR1);
260 sigaddset(&action.sa_mask, SIGHUP);
261 action.sa_flags = SA_RESTART;
262 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
263 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
264 afp_dsi_die(EXITERR_SYS);
266 #endif /* SERVERTEXT */
268 /* SIGUSR1 - set down in 5 minutes */
269 action.sa_handler = afp_dsi_timedown;
270 sigemptyset( &action.sa_mask );
271 sigaddset(&action.sa_mask, SIGALRM);
272 sigaddset(&action.sa_mask, SIGHUP);
273 sigaddset(&action.sa_mask, SIGTERM);
275 sigaddset(&action.sa_mask, SIGUSR2);
277 action.sa_flags = SA_RESTART;
278 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
279 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
280 afp_dsi_die(EXITERR_SYS);
284 action.sa_handler = alarm_handler;
285 sigemptyset(&action.sa_mask);
286 sigaddset(&action.sa_mask, SIGHUP);
287 sigaddset(&action.sa_mask, SIGTERM);
288 sigaddset(&action.sa_mask, SIGUSR1);
290 sigaddset(&action.sa_mask, SIGUSR2);
292 action.sa_flags = SA_RESTART;
293 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
294 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
295 afp_dsi_die(EXITERR_SYS);
299 fault_setup((void (*)(void *))afp_dsi_die);
302 /* get stuck here until the end */
303 while ((cmd = dsi_receive(dsi))) {
305 child.flags &= ~CHILD_SLEEPING;
306 dsi_sleep(dsi, 0); /* wake up */
307 if (reload_request) {
309 load_volumes(child.obj);
312 if (cmd == DSIFUNC_TICKLE) {
313 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
314 if ((child.flags & CHILD_DIE))
317 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
318 setitimer(ITIMER_REAL, &dsi->timer, NULL);
323 LOG(log_info, logtype_afpd, "done");
325 if (obj->options.flags & OPTION_DEBUG )
334 if ( flushfork( writtenfork ) < 0 ) {
335 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
341 function = (u_char) dsi->commands[0];
343 if (obj->options.flags & OPTION_DEBUG ) {
344 printf("command: %d (%s)\n", function, AfpNum2name(function));
345 bprint((char *) dsi->commands, dsi->cmdlen);
349 /* send off an afp command. in a couple cases, we take advantage
350 * of the fact that we're a stream-based protocol. */
351 if (afp_switch[function]) {
352 dsi->datalen = DSI_DATASIZ;
353 child.flags |= CHILD_RUNNING;
355 err = (*afp_switch[function])(obj,
356 dsi->commands, dsi->cmdlen,
357 dsi->data, &dsi->datalen);
359 /* bring everything back to old euid, egid */
361 restore_uidgid ( &obj->uidgid );
362 #endif /* FORCE_UIDGID */
363 child.flags &= ~CHILD_RUNNING;
365 LOG(log_error, logtype_afpd, "bad function %X", function);
370 /* single shot toggle that gets set by dsi_readinit. */
377 if (obj->options.flags & OPTION_DEBUG ) {
378 printf( "reply: %d, %d\n", err, dsi->clientID);
379 bprint((char *) dsi->data, dsi->datalen);
382 if (!dsi_cmdreply(dsi, err)) {
383 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
384 afp_dsi_die(EXITERR_CLNT);
388 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
389 function = (u_char) dsi->commands[0];
391 if ( obj->options.flags & OPTION_DEBUG ) {
392 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
393 bprint((char *) dsi->commands, dsi->cmdlen);
396 if ( afp_switch[ function ] != NULL ) {
397 dsi->datalen = DSI_DATASIZ;
398 child.flags |= CHILD_RUNNING;
399 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
400 dsi->data, &dsi->datalen);
401 child.flags &= ~CHILD_RUNNING;
403 /* bring everything back to old euid, egid */
405 restore_uidgid ( &obj->uidgid );
406 #endif /* FORCE_UIDGID */
408 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
414 if (obj->options.flags & OPTION_DEBUG ) {
415 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
416 bprint((char *) dsi->data, dsi->datalen);
419 if (!dsi_wrtreply(dsi, err)) {
420 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
421 afp_dsi_die(EXITERR_CLNT);
425 case DSIFUNC_ATTN: /* attention replies */
429 /* error. this usually implies a mismatch of some kind
430 * between server and client. if things are correct,
431 * we need to flush the rest of the packet if necessary. */
433 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
434 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
439 if ( obj->options.flags & OPTION_DEBUG ) {
443 of_pforkdesc( stdout );
450 afp_dsi_die(EXITERR_CLNT);