2 * $Id: afp_dsi.c,v 1.26 2002-12-04 10:59:36 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) );
122 sv.sa_handler = SIG_IGN;
123 sigemptyset( &sv.sa_mask );
124 sv.sa_flags = SA_RESTART;
125 if ( sigaction( SIGHUP, &sv, 0 ) < 0 ) {
126 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
133 static void afp_dsi_getmesg (int sig)
136 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
138 #endif /* SERVERTEXT */
140 static void alarm_handler()
143 /* if we're in the midst of processing something,
145 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
146 if (!(err = pollvoltime(child.obj)))
147 err = dsi_tickle(child.obj->handle);
151 } else { /* didn't receive a tickle. close connection */
152 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
159 * Signal handler for SIGUSR1 - set the debug flag and
160 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
162 void afp_set_debug (int sig)
164 char fname[MAXPATHLEN];
166 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
167 freopen(fname, "w", stdout);
168 child.obj->options.flags |= OPTION_DEBUG;
174 /* afp over dsi. this never returns. */
175 void afp_over_dsi(AFPObj *obj)
177 DSI *dsi = (DSI *) obj->handle;
180 struct sigaction action;
182 obj->exit = afp_dsi_die;
183 obj->reply = (int (*)()) dsi_cmdreply;
184 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
187 child.tickle = child.flags = 0;
189 /* install SIGTERM and SIGHUP */
190 memset(&action, 0, sizeof(action));
191 action.sa_handler = afp_dsi_timedown;
192 sigemptyset( &action.sa_mask );
193 sigaddset(&action.sa_mask, SIGALRM);
194 sigaddset(&action.sa_mask, SIGTERM);
195 action.sa_flags = SA_RESTART;
196 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
197 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
201 action.sa_handler = afp_dsi_die;
202 sigemptyset( &action.sa_mask );
203 sigaddset(&action.sa_mask, SIGALRM);
204 sigaddset(&action.sa_mask, SIGHUP);
205 action.sa_flags = SA_RESTART;
206 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
207 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
212 /* Added for server message support */
213 action.sa_handler = afp_dsi_getmesg;
214 sigemptyset( &action.sa_mask );
215 sigaddset(&action.sa_mask, SIGUSR2);
216 action.sa_flags = SA_RESTART;
217 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
218 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
221 #endif /* SERVERTEXT */
223 /* SIGUSR1 - set "debug" flag on this process. */
224 action.sa_handler = afp_set_debug;
225 sigemptyset( &action.sa_mask );
226 sigaddset(&action.sa_mask, SIGUSR1);
227 action.sa_flags = SA_RESTART;
228 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
229 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
234 action.sa_handler = alarm_handler;
235 sigemptyset(&action.sa_mask);
236 sigaddset(&action.sa_mask, SIGHUP);
237 sigaddset(&action.sa_mask, SIGTERM);
238 action.sa_flags = SA_RESTART;
239 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
240 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
244 /* get stuck here until the end */
245 while ((cmd = dsi_receive(dsi))) {
248 if (cmd == DSIFUNC_TICKLE) {
249 /* so we don't get killed on the client side. */
250 if (child.flags & CHILD_DIE)
253 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
254 setitimer(ITIMER_REAL, &dsi->timer, NULL);
259 LOG(log_info, logtype_afpd, "done");
260 if (obj->options.flags & OPTION_DEBUG )
268 if ( flushfork( writtenfork ) < 0 ) {
269 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
275 function = (u_char) dsi->commands[0];
276 if (obj->options.flags & OPTION_DEBUG ) {
277 printf("command: %d (%s)\n", function, AfpNum2name(function));
278 bprint((char *) dsi->commands, dsi->cmdlen);
281 /* send off an afp command. in a couple cases, we take advantage
282 * of the fact that we're a stream-based protocol. */
283 if (afp_switch[function]) {
284 dsi->datalen = DSI_DATASIZ;
285 child.flags |= CHILD_RUNNING;
287 err = (*afp_switch[function])(obj,
288 dsi->commands, dsi->cmdlen,
289 dsi->data, &dsi->datalen);
291 /* bring everything back to old euid, egid */
293 restore_uidgid ( &obj->uidgid );
294 #endif /* FORCE_UIDGID */
295 child.flags &= ~CHILD_RUNNING;
297 LOG(log_error, logtype_afpd, "bad function %X", function);
302 /* single shot toggle that gets set by dsi_readinit. */
308 if (obj->options.flags & OPTION_DEBUG ) {
309 printf( "reply: %d, %d\n", err, dsi->clientID);
310 bprint((char *) dsi->data, dsi->datalen);
313 if (!dsi_cmdreply(dsi, err)) {
314 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
319 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
320 function = (u_char) dsi->commands[0];
321 if ( obj->options.flags & OPTION_DEBUG ) {
322 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
323 bprint((char *) dsi->commands, dsi->cmdlen);
326 if ( afp_switch[ function ] != NULL ) {
327 dsi->datalen = DSI_DATASIZ;
328 child.flags |= CHILD_RUNNING;
329 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
330 dsi->data, &dsi->datalen);
331 child.flags &= ~CHILD_RUNNING;
333 /* bring everything back to old euid, egid */
335 restore_uidgid ( &obj->uidgid );
336 #endif /* FORCE_UIDGID */
338 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
343 if (obj->options.flags & OPTION_DEBUG ) {
344 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
345 bprint((char *) dsi->data, dsi->datalen);
348 if (!dsi_wrtreply(dsi, err)) {
349 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
354 case DSIFUNC_ATTN: /* attention replies */
358 /* error. this usually implies a mismatch of some kind
359 * between server and client. if things are correct,
360 * we need to flush the rest of the packet if necessary. */
362 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
363 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
368 if ( obj->options.flags & OPTION_DEBUG ) {
372 of_pforkdesc( stdout );