2 * $Id: afp_dsi.c,v 1.27.2.3 2003-07-21 05:50:53 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;
66 /* UAM had syslog control; afpd needs to reassert itself */
67 set_processname("afpd");
68 syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
69 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
70 dsi->read_count/1024.0, dsi->write_count/1024.0);
75 /* -------------------------------
77 * a little bit of code duplication.
79 static void afp_dsi_die(int sig)
81 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
82 afp_dsi_close(child.obj);
83 if (sig) /* if no signal, assume dieing because logins are disabled &
84 don't log it (maintenance mode)*/
85 LOG(log_info, logtype_afpd, "Connection terminated");
86 if (sig == SIGTERM || sig == SIGALRM) {
95 static void afp_dsi_sleep(void)
97 child.flags |= CHILD_SLEEPING;
98 dsi_sleep(child.obj->handle, 1);
101 /* ------------------- */
102 static void afp_dsi_timedown()
107 child.flags |= CHILD_DIE;
108 /* shutdown and don't reconnect. server going down in 5 minutes. */
109 setmessage("The server is going down for maintenance.");
110 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
111 AFPATTN_MESG | AFPATTN_TIME(5));
113 it.it_interval.tv_sec = 0;
114 it.it_interval.tv_usec = 0;
115 it.it_value.tv_sec = 300;
116 it.it_value.tv_usec = 0;
118 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
119 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
122 memset(&sv, 0, sizeof(sv));
123 sv.sa_handler = afp_dsi_die;
124 sigemptyset( &sv.sa_mask );
125 sigaddset(&sv.sa_mask, SIGHUP);
126 sigaddset(&sv.sa_mask, SIGTERM);
127 sv.sa_flags = SA_RESTART;
128 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
129 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
134 sv.sa_handler = SIG_IGN;
135 sigemptyset( &sv.sa_mask );
136 sv.sa_flags = SA_RESTART;
137 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
138 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
144 /* ---------------------------------
145 * SIGHUP reload configuration file
146 * FIXME here or we wait ?
148 volatile int reload_request = 0;
150 static void afp_dsi_reload()
155 /* ---------------------- */
157 static void afp_dsi_getmesg (int sig)
160 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
162 #endif /* SERVERTEXT */
164 static void alarm_handler()
168 /* if we're in the midst of processing something,
170 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
172 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
173 if (!(err = pollvoltime(child.obj)))
174 err = dsi_tickle(child.obj->handle);
178 } else { /* didn't receive a tickle. close connection */
179 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
185 /* ---------------------------------
186 * old signal handler for SIGUSR1 - set the debug flag and
187 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
189 void afp_set_debug (int sig)
191 char fname[MAXPATHLEN];
193 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
194 freopen(fname, "w", stdout);
195 child.obj->options.flags |= OPTION_DEBUG;
200 /* -------------------------------------------
201 afp over dsi. this never returns.
203 void afp_over_dsi(AFPObj *obj)
205 DSI *dsi = (DSI *) obj->handle;
208 struct sigaction action;
210 obj->exit = afp_dsi_die;
211 obj->reply = (int (*)()) dsi_cmdreply;
212 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
214 obj->sleep = afp_dsi_sleep;
216 child.tickle = child.flags = 0;
218 memset(&action, 0, sizeof(action));
221 action.sa_handler = afp_dsi_reload;
222 sigemptyset( &action.sa_mask );
223 sigaddset(&action.sa_mask, SIGALRM);
224 sigaddset(&action.sa_mask, SIGTERM);
225 sigaddset(&action.sa_mask, SIGUSR1);
226 action.sa_flags = SA_RESTART;
227 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
228 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
232 /* install SIGTERM */
233 action.sa_handler = afp_dsi_die;
234 sigemptyset( &action.sa_mask );
235 sigaddset(&action.sa_mask, SIGALRM);
236 sigaddset(&action.sa_mask, SIGHUP);
237 sigaddset(&action.sa_mask, SIGUSR1);
238 action.sa_flags = SA_RESTART;
239 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
240 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
245 /* Added for server message support */
246 action.sa_handler = afp_dsi_getmesg;
247 sigemptyset( &action.sa_mask );
248 sigaddset(&action.sa_mask, SIGUSR2);
249 action.sa_flags = SA_RESTART;
250 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
251 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
254 #endif /* SERVERTEXT */
256 /* SIGUSR1 - set down in 5 minutes */
257 action.sa_handler = afp_dsi_timedown;
258 sigemptyset( &action.sa_mask );
259 sigaddset(&action.sa_mask, SIGALRM);
260 sigaddset(&action.sa_mask, SIGHUP);
261 sigaddset(&action.sa_mask, SIGTERM);
262 action.sa_flags = SA_RESTART;
263 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
264 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
269 action.sa_handler = alarm_handler;
270 sigemptyset(&action.sa_mask);
271 sigaddset(&action.sa_mask, SIGHUP);
272 sigaddset(&action.sa_mask, SIGTERM);
273 action.sa_flags = SA_RESTART;
274 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
275 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
279 /* get stuck here until the end */
280 while ((cmd = dsi_receive(dsi))) {
282 child.flags &= ~CHILD_SLEEPING;
283 dsi_sleep(dsi, 0); /* wake up */
284 if (reload_request) {
286 load_volumes(child.obj);
289 if (cmd == DSIFUNC_TICKLE) {
290 /* so we don't get killed on the client side. */
291 if ((child.flags & CHILD_DIE))
294 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
295 setitimer(ITIMER_REAL, &dsi->timer, NULL);
300 LOG(log_info, logtype_afpd, "done");
301 if (obj->options.flags & OPTION_DEBUG )
309 if ( flushfork( writtenfork ) < 0 ) {
310 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
316 function = (u_char) dsi->commands[0];
317 if (obj->options.flags & OPTION_DEBUG ) {
318 printf("command: %d (%s)\n", function, AfpNum2name(function));
319 bprint((char *) dsi->commands, dsi->cmdlen);
322 /* send off an afp command. in a couple cases, we take advantage
323 * of the fact that we're a stream-based protocol. */
324 if (afp_switch[function]) {
325 dsi->datalen = DSI_DATASIZ;
326 child.flags |= CHILD_RUNNING;
328 err = (*afp_switch[function])(obj,
329 dsi->commands, dsi->cmdlen,
330 dsi->data, &dsi->datalen);
332 /* bring everything back to old euid, egid */
334 restore_uidgid ( &obj->uidgid );
335 #endif /* FORCE_UIDGID */
336 child.flags &= ~CHILD_RUNNING;
338 LOG(log_error, logtype_afpd, "bad function %X", function);
343 /* single shot toggle that gets set by dsi_readinit. */
349 if (obj->options.flags & OPTION_DEBUG ) {
350 printf( "reply: %d, %d\n", err, dsi->clientID);
351 bprint((char *) dsi->data, dsi->datalen);
354 if (!dsi_cmdreply(dsi, err)) {
355 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
360 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
361 function = (u_char) dsi->commands[0];
362 if ( obj->options.flags & OPTION_DEBUG ) {
363 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
364 bprint((char *) dsi->commands, dsi->cmdlen);
367 if ( afp_switch[ function ] != NULL ) {
368 dsi->datalen = DSI_DATASIZ;
369 child.flags |= CHILD_RUNNING;
370 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
371 dsi->data, &dsi->datalen);
372 child.flags &= ~CHILD_RUNNING;
374 /* bring everything back to old euid, egid */
376 restore_uidgid ( &obj->uidgid );
377 #endif /* FORCE_UIDGID */
379 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
384 if (obj->options.flags & OPTION_DEBUG ) {
385 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
386 bprint((char *) dsi->data, dsi->datalen);
389 if (!dsi_wrtreply(dsi, err)) {
390 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
395 case DSIFUNC_ATTN: /* attention replies */
399 /* error. this usually implies a mismatch of some kind
400 * between server and client. if things are correct,
401 * we need to flush the rest of the packet if necessary. */
403 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
404 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
409 if ( obj->options.flags & OPTION_DEBUG ) {
413 of_pforkdesc( stdout );