2 * $Id: afp_dsi.c,v 1.30 2003-06-09 15:09:19 srittau 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 /* a little bit of code duplication. */
76 static void afp_dsi_die(int sig)
78 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
79 afp_dsi_close(child.obj);
80 if (sig) /* if no signal, assume dieing because logins are disabled &
81 don't log it (maintenance mode)*/
82 LOG(log_info, logtype_afpd, "Connection terminated");
83 if (sig == SIGTERM || sig == SIGALRM) {
92 static void afp_dsi_sleep(void)
94 child.flags |= CHILD_SLEEPING;
95 dsi_sleep(child.obj->handle, 1);
98 /* ------------------- */
99 static void afp_dsi_timedown()
104 child.flags |= CHILD_DIE;
105 /* shutdown and don't reconnect. server going down in 5 minutes. */
106 setmessage("The server is going down for maintenance.");
107 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
108 AFPATTN_MESG | AFPATTN_TIME(5));
110 it.it_interval.tv_sec = 0;
111 it.it_interval.tv_usec = 0;
112 it.it_value.tv_sec = 300;
113 it.it_value.tv_usec = 0;
115 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
116 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
120 memset(&sv, 0, sizeof(sv));
121 sv.sa_handler = afp_dsi_die;
122 sigemptyset( &sv.sa_mask );
123 sigaddset(&sv.sa_mask, SIGHUP);
124 sigaddset(&sv.sa_mask, SIGTERM);
125 sv.sa_flags = SA_RESTART;
126 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
127 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
132 sv.sa_handler = SIG_IGN;
133 sigemptyset( &sv.sa_mask );
134 sv.sa_flags = SA_RESTART;
135 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
136 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
143 static void afp_dsi_getmesg (int sig)
146 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
148 #endif /* SERVERTEXT */
150 static void alarm_handler()
154 /* if we're in the midst of processing something,
156 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
158 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
159 if (!(err = pollvoltime(child.obj)))
160 err = dsi_tickle(child.obj->handle);
164 } else { /* didn't receive a tickle. close connection */
165 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
172 * Signal handler for SIGUSR1 - set the debug flag and
173 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
175 void afp_set_debug (int sig)
177 char fname[MAXPATHLEN];
179 snprintf(fname, MAXPATHLEN-1, "%s/afpd-debug-%d", P_tmpdir, getpid());
180 freopen(fname, "w", stdout);
181 child.obj->options.flags |= OPTION_DEBUG;
187 /* afp over dsi. this never returns. */
188 void afp_over_dsi(AFPObj *obj)
190 DSI *dsi = (DSI *) obj->handle;
193 struct sigaction action;
195 obj->exit = afp_dsi_die;
196 obj->reply = (int (*)()) dsi_cmdreply;
197 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
198 obj->sleep = afp_dsi_sleep;
200 child.tickle = child.flags = 0;
202 /* install SIGTERM and SIGHUP */
203 memset(&action, 0, sizeof(action));
204 action.sa_handler = afp_dsi_timedown;
205 sigemptyset( &action.sa_mask );
206 sigaddset(&action.sa_mask, SIGALRM);
207 sigaddset(&action.sa_mask, SIGTERM);
208 action.sa_flags = SA_RESTART;
209 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
210 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
214 action.sa_handler = afp_dsi_die;
215 sigemptyset( &action.sa_mask );
216 sigaddset(&action.sa_mask, SIGALRM);
217 sigaddset(&action.sa_mask, SIGHUP);
218 action.sa_flags = SA_RESTART;
219 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
220 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
225 /* Added for server message support */
226 action.sa_handler = afp_dsi_getmesg;
227 sigemptyset( &action.sa_mask );
228 sigaddset(&action.sa_mask, SIGUSR2);
229 action.sa_flags = SA_RESTART;
230 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
231 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
234 #endif /* SERVERTEXT */
236 /* SIGUSR1 - set "debug" flag on this process. */
237 action.sa_handler = afp_set_debug;
238 sigemptyset( &action.sa_mask );
239 sigaddset(&action.sa_mask, SIGUSR1);
240 action.sa_flags = SA_RESTART;
241 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
242 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
247 action.sa_handler = alarm_handler;
248 sigemptyset(&action.sa_mask);
249 sigaddset(&action.sa_mask, SIGHUP);
250 sigaddset(&action.sa_mask, SIGTERM);
251 action.sa_flags = SA_RESTART;
252 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
253 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
257 /* get stuck here until the end */
258 while ((cmd = dsi_receive(dsi))) {
260 child.flags &= ~CHILD_SLEEPING;
261 dsi_sleep(dsi, 0); /* wake up */
263 if (cmd == DSIFUNC_TICKLE) {
264 /* so we don't get killed on the client side. */
265 if ((child.flags & CHILD_DIE))
268 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
269 setitimer(ITIMER_REAL, &dsi->timer, NULL);
275 LOG(log_info, logtype_afpd, "done");
276 if (obj->options.flags & OPTION_DEBUG )
284 if ( flushfork( writtenfork ) < 0 ) {
285 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
291 function = (u_char) dsi->commands[0];
292 if (obj->options.flags & OPTION_DEBUG ) {
293 printf("command: %d (%s)\n", function, AfpNum2name(function));
294 bprint((char *) dsi->commands, dsi->cmdlen);
297 /* send off an afp command. in a couple cases, we take advantage
298 * of the fact that we're a stream-based protocol. */
299 if (afp_switch[function]) {
300 dsi->datalen = DSI_DATASIZ;
301 child.flags |= CHILD_RUNNING;
303 err = (*afp_switch[function])(obj,
304 dsi->commands, dsi->cmdlen,
305 dsi->data, &dsi->datalen);
307 /* bring everything back to old euid, egid */
309 restore_uidgid ( &obj->uidgid );
310 #endif /* FORCE_UIDGID */
311 child.flags &= ~CHILD_RUNNING;
313 LOG(log_error, logtype_afpd, "bad function %X", function);
318 /* single shot toggle that gets set by dsi_readinit. */
324 if (obj->options.flags & OPTION_DEBUG ) {
325 printf( "reply: %d, %d\n", err, dsi->clientID);
326 bprint((char *) dsi->data, dsi->datalen);
329 if (!dsi_cmdreply(dsi, err)) {
330 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
335 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
336 function = (u_char) dsi->commands[0];
337 if ( obj->options.flags & OPTION_DEBUG ) {
338 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
339 bprint((char *) dsi->commands, dsi->cmdlen);
342 if ( afp_switch[ function ] != NULL ) {
343 dsi->datalen = DSI_DATASIZ;
344 child.flags |= CHILD_RUNNING;
345 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
346 dsi->data, &dsi->datalen);
347 child.flags &= ~CHILD_RUNNING;
349 /* bring everything back to old euid, egid */
351 restore_uidgid ( &obj->uidgid );
352 #endif /* FORCE_UIDGID */
354 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
359 if (obj->options.flags & OPTION_DEBUG ) {
360 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
361 bprint((char *) dsi->data, dsi->datalen);
364 if (!dsi_wrtreply(dsi, err)) {
365 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
370 case DSIFUNC_ATTN: /* attention replies */
374 /* error. this usually implies a mismatch of some kind
375 * between server and client. if things are correct,
376 * we need to flush the rest of the packet if necessary. */
378 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
379 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
384 if ( obj->options.flags & OPTION_DEBUG ) {
388 of_pforkdesc( stdout );