2 * $Id: afp_dsi.c,v 1.29 2003-06-06 19:41:48 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) );
119 memset(&sv, 0, sizeof(sv));
120 sv.sa_handler = afp_dsi_die;
121 sigemptyset( &sv.sa_mask );
122 sigaddset(&sv.sa_mask, SIGHUP);
123 sigaddset(&sv.sa_mask, SIGTERM);
124 sv.sa_flags = SA_RESTART;
125 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
126 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
131 sv.sa_handler = SIG_IGN;
132 sigemptyset( &sv.sa_mask );
133 sv.sa_flags = SA_RESTART;
134 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
135 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
142 static void afp_dsi_getmesg (int sig)
145 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
147 #endif /* SERVERTEXT */
149 static void alarm_handler()
152 /* if we're in the midst of processing something,
154 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
156 } else if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
157 if (!(err = pollvoltime(child.obj)))
158 err = dsi_tickle(child.obj->handle);
162 } else { /* didn't receive a tickle. close connection */
163 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
170 * Signal handler for SIGUSR1 - set the debug flag and
171 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
173 void afp_set_debug (int sig)
175 char fname[MAXPATHLEN];
177 snprintf(fname, MAXPATHLEN-1, "%s/afpd-debug-%d", P_tmpdir, getpid());
178 freopen(fname, "w", stdout);
179 child.obj->options.flags |= OPTION_DEBUG;
185 /* afp over dsi. this never returns. */
186 void afp_over_dsi(AFPObj *obj)
188 DSI *dsi = (DSI *) obj->handle;
191 struct sigaction action;
193 obj->exit = afp_dsi_die;
194 obj->reply = (int (*)()) dsi_cmdreply;
195 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
196 obj->sleep = afp_dsi_sleep;
198 child.tickle = child.flags = 0;
200 /* install SIGTERM and SIGHUP */
201 memset(&action, 0, sizeof(action));
202 action.sa_handler = afp_dsi_timedown;
203 sigemptyset( &action.sa_mask );
204 sigaddset(&action.sa_mask, SIGALRM);
205 sigaddset(&action.sa_mask, SIGTERM);
206 action.sa_flags = SA_RESTART;
207 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
208 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
212 action.sa_handler = afp_dsi_die;
213 sigemptyset( &action.sa_mask );
214 sigaddset(&action.sa_mask, SIGALRM);
215 sigaddset(&action.sa_mask, SIGHUP);
216 action.sa_flags = SA_RESTART;
217 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
218 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
223 /* Added for server message support */
224 action.sa_handler = afp_dsi_getmesg;
225 sigemptyset( &action.sa_mask );
226 sigaddset(&action.sa_mask, SIGUSR2);
227 action.sa_flags = SA_RESTART;
228 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
229 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
232 #endif /* SERVERTEXT */
234 /* SIGUSR1 - set "debug" flag on this process. */
235 action.sa_handler = afp_set_debug;
236 sigemptyset( &action.sa_mask );
237 sigaddset(&action.sa_mask, SIGUSR1);
238 action.sa_flags = SA_RESTART;
239 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
240 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
245 action.sa_handler = alarm_handler;
246 sigemptyset(&action.sa_mask);
247 sigaddset(&action.sa_mask, SIGHUP);
248 sigaddset(&action.sa_mask, SIGTERM);
249 action.sa_flags = SA_RESTART;
250 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
251 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
255 /* get stuck here until the end */
256 while ((cmd = dsi_receive(dsi))) {
258 child.flags &= ~CHILD_SLEEPING;
259 dsi_sleep(dsi, 0); /* wake up */
261 if (cmd == DSIFUNC_TICKLE) {
262 /* so we don't get killed on the client side. */
263 if ((child.flags & CHILD_DIE))
266 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
267 setitimer(ITIMER_REAL, &dsi->timer, NULL);
272 LOG(log_info, logtype_afpd, "done");
273 if (obj->options.flags & OPTION_DEBUG )
281 if ( flushfork( writtenfork ) < 0 ) {
282 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
288 function = (u_char) dsi->commands[0];
289 if (obj->options.flags & OPTION_DEBUG ) {
290 printf("command: %d (%s)\n", function, AfpNum2name(function));
291 bprint((char *) dsi->commands, dsi->cmdlen);
294 /* send off an afp command. in a couple cases, we take advantage
295 * of the fact that we're a stream-based protocol. */
296 if (afp_switch[function]) {
297 dsi->datalen = DSI_DATASIZ;
298 child.flags |= CHILD_RUNNING;
300 err = (*afp_switch[function])(obj,
301 dsi->commands, dsi->cmdlen,
302 dsi->data, &dsi->datalen);
304 /* bring everything back to old euid, egid */
306 restore_uidgid ( &obj->uidgid );
307 #endif /* FORCE_UIDGID */
308 child.flags &= ~CHILD_RUNNING;
310 LOG(log_error, logtype_afpd, "bad function %X", function);
315 /* single shot toggle that gets set by dsi_readinit. */
321 if (obj->options.flags & OPTION_DEBUG ) {
322 printf( "reply: %d, %d\n", err, dsi->clientID);
323 bprint((char *) dsi->data, dsi->datalen);
326 if (!dsi_cmdreply(dsi, err)) {
327 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
332 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
333 function = (u_char) dsi->commands[0];
334 if ( obj->options.flags & OPTION_DEBUG ) {
335 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
336 bprint((char *) dsi->commands, dsi->cmdlen);
339 if ( afp_switch[ function ] != NULL ) {
340 dsi->datalen = DSI_DATASIZ;
341 child.flags |= CHILD_RUNNING;
342 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
343 dsi->data, &dsi->datalen);
344 child.flags &= ~CHILD_RUNNING;
346 /* bring everything back to old euid, egid */
348 restore_uidgid ( &obj->uidgid );
349 #endif /* FORCE_UIDGID */
351 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
356 if (obj->options.flags & OPTION_DEBUG ) {
357 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
358 bprint((char *) dsi->data, dsi->datalen);
361 if (!dsi_wrtreply(dsi, err)) {
362 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
367 case DSIFUNC_ATTN: /* attention replies */
371 /* error. this usually implies a mismatch of some kind
372 * between server and client. if things are correct,
373 * we need to flush the rest of the packet if necessary. */
375 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
376 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
381 if ( obj->options.flags & OPTION_DEBUG ) {
385 of_pforkdesc( stdout );