2 * $Id: afp_dsi.c,v 1.25 2002-10-11 14:18:23 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)
58 static __inline__ void afp_dsi_close(AFPObj *obj)
60 DSI *dsi = obj->handle;
65 /* UAM had syslog control; afpd needs to reassert itself */
66 set_processname("afpd");
67 syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
68 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
69 dsi->read_count/1024.0, dsi->write_count/1024.0);
74 /* a little bit of code duplication. */
75 static void afp_dsi_die(int sig)
77 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
78 afp_dsi_close(child.obj);
79 if (sig) /* if no signal, assume dieing because logins are disabled &
80 don't log it (maintenance mode)*/
81 LOG(log_info, logtype_afpd, "Connection terminated");
82 if (sig == SIGTERM || sig == SIGALRM) {
90 static void afp_dsi_timedown()
95 child.flags |= CHILD_DIE;
96 /* shutdown and don't reconnect. server going down in 5 minutes. */
97 setmessage("The server is going down for maintenance.");
98 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
99 AFPATTN_MESG | AFPATTN_TIME(5));
101 it.it_interval.tv_sec = 0;
102 it.it_interval.tv_usec = 0;
103 it.it_value.tv_sec = 300;
104 it.it_value.tv_usec = 0;
106 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
107 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
110 memset(&sv, 0, sizeof(sv));
111 sv.sa_handler = afp_dsi_die;
112 sigemptyset( &sv.sa_mask );
113 sigaddset(&sv.sa_mask, SIGHUP);
114 sigaddset(&sv.sa_mask, SIGTERM);
115 sv.sa_flags = SA_RESTART;
116 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
117 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
123 static void afp_dsi_getmesg (int sig)
126 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
128 #endif /* SERVERTEXT */
130 static void alarm_handler()
132 /* if we're in the midst of processing something,
134 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
135 if (!pollvoltime(child.obj))
136 dsi_tickle(child.obj->handle);
137 } else { /* didn't receive a tickle. close connection */
138 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
145 * Signal handler for SIGUSR1 - set the debug flag and
146 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
148 void afp_set_debug (int sig)
150 char fname[MAXPATHLEN];
152 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
153 freopen(fname, "w", stdout);
154 child.obj->options.flags |= OPTION_DEBUG;
160 /* afp over dsi. this never returns. */
161 void afp_over_dsi(AFPObj *obj)
163 DSI *dsi = (DSI *) obj->handle;
166 struct sigaction action;
168 obj->exit = afp_dsi_die;
169 obj->reply = (int (*)()) dsi_cmdreply;
170 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
173 child.tickle = child.flags = 0;
175 /* install SIGTERM and SIGHUP */
176 memset(&action, 0, sizeof(action));
177 action.sa_handler = afp_dsi_timedown;
178 sigemptyset( &action.sa_mask );
179 sigaddset(&action.sa_mask, SIGALRM);
180 sigaddset(&action.sa_mask, SIGTERM);
181 action.sa_flags = SA_RESTART;
182 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
183 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
187 action.sa_handler = afp_dsi_die;
188 sigemptyset( &action.sa_mask );
189 sigaddset(&action.sa_mask, SIGALRM);
190 sigaddset(&action.sa_mask, SIGHUP);
191 action.sa_flags = SA_RESTART;
192 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
193 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
198 /* Added for server message support */
199 action.sa_handler = afp_dsi_getmesg;
200 sigemptyset( &action.sa_mask );
201 sigaddset(&action.sa_mask, SIGUSR2);
202 action.sa_flags = SA_RESTART;
203 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
204 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
207 #endif /* SERVERTEXT */
209 /* SIGUSR1 - set "debug" flag on this process. */
210 action.sa_handler = afp_set_debug;
211 sigemptyset( &action.sa_mask );
212 sigaddset(&action.sa_mask, SIGUSR1);
213 action.sa_flags = SA_RESTART;
214 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
215 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
220 action.sa_handler = alarm_handler;
221 sigemptyset(&action.sa_mask);
222 sigaddset(&action.sa_mask, SIGHUP);
223 sigaddset(&action.sa_mask, SIGTERM);
224 action.sa_flags = SA_RESTART;
225 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
226 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
230 /* get stuck here until the end */
231 while ((cmd = dsi_receive(dsi))) {
234 if (cmd == DSIFUNC_TICKLE) {
235 /* so we don't get killed on the client side. */
236 if (child.flags & CHILD_DIE)
239 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
240 setitimer(ITIMER_REAL, &dsi->timer, NULL);
245 LOG(log_info, logtype_afpd, "done");
246 if (obj->options.flags & OPTION_DEBUG )
254 if ( flushfork( writtenfork ) < 0 ) {
255 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
261 function = (u_char) dsi->commands[0];
262 if (obj->options.flags & OPTION_DEBUG ) {
263 printf("command: %d (%s)\n", function, AfpNum2name(function));
264 bprint((char *) dsi->commands, dsi->cmdlen);
267 /* send off an afp command. in a couple cases, we take advantage
268 * of the fact that we're a stream-based protocol. */
269 if (afp_switch[function]) {
270 dsi->datalen = DSI_DATASIZ;
271 child.flags |= CHILD_RUNNING;
273 err = (*afp_switch[function])(obj,
274 dsi->commands, dsi->cmdlen,
275 dsi->data, &dsi->datalen);
277 /* bring everything back to old euid, egid */
279 restore_uidgid ( &obj->uidgid );
280 #endif /* FORCE_UIDGID */
281 child.flags &= ~CHILD_RUNNING;
283 LOG(log_error, logtype_afpd, "bad function %X", function);
288 /* single shot toggle that gets set by dsi_readinit. */
294 if (obj->options.flags & OPTION_DEBUG ) {
295 printf( "reply: %d, %d\n", err, dsi->clientID);
296 bprint((char *) dsi->data, dsi->datalen);
299 if (!dsi_cmdreply(dsi, err)) {
300 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
305 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
306 function = (u_char) dsi->commands[0];
307 if ( obj->options.flags & OPTION_DEBUG ) {
308 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
309 bprint((char *) dsi->commands, dsi->cmdlen);
312 if ( afp_switch[ function ] != NULL ) {
313 dsi->datalen = DSI_DATASIZ;
314 child.flags |= CHILD_RUNNING;
315 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
316 dsi->data, &dsi->datalen);
317 child.flags &= ~CHILD_RUNNING;
319 /* bring everything back to old euid, egid */
321 restore_uidgid ( &obj->uidgid );
322 #endif /* FORCE_UIDGID */
324 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
329 if (obj->options.flags & OPTION_DEBUG ) {
330 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
331 bprint((char *) dsi->data, dsi->datalen);
334 if (!dsi_wrtreply(dsi, err)) {
335 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
340 case DSIFUNC_ATTN: /* attention replies */
344 /* error. this usually implies a mismatch of some kind
345 * between server and client. if things are correct,
346 * we need to flush the rest of the packet if necessary. */
348 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
349 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
354 if ( obj->options.flags & OPTION_DEBUG ) {
358 of_pforkdesc( stdout );