2 * $Id: afp_dsi.c,v 1.21 2002-05-03 22:51:33 jmarcus 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>
41 extern struct oforks *writtenfork;
43 #define CHILD_DIE (1 << 0)
44 #define CHILD_RUNNING (1 << 1)
53 static __inline__ void afp_dsi_close(AFPObj *obj)
55 DSI *dsi = obj->handle;
60 /* UAM had syslog control; afpd needs to reassert itself */
61 set_processname("afpd");
62 syslog_setup(log_debug, logtype_default, logoption_ndelay | logoption_pid, logfacility_daemon);
63 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
64 dsi->read_count/1024.0, dsi->write_count/1024.0);
69 /* a little bit of code duplication. */
70 static void afp_dsi_die(int sig)
72 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
73 afp_dsi_close(child.obj);
74 if (sig) /* if no signal, assume dieing because logins are disabled &
75 don't log it (maintenance mode)*/
76 LOG(log_info, logtype_afpd, "Connection terminated");
77 if (sig == SIGTERM || sig == SIGALRM) {
85 static void afp_dsi_timedown()
90 child.flags |= CHILD_DIE;
91 /* shutdown and don't reconnect. server going down in 5 minutes. */
92 setmessage("The server is going down for maintenance.");
93 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
94 AFPATTN_MESG | AFPATTN_TIME(5));
96 it.it_interval.tv_sec = 0;
97 it.it_interval.tv_usec = 0;
98 it.it_value.tv_sec = 300;
99 it.it_value.tv_usec = 0;
100 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
101 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
105 memset(&sv, 0, sizeof(sv));
106 sv.sa_handler = afp_dsi_die;
107 sigemptyset( &sv.sa_mask );
108 sigaddset(&sv.sa_mask, SIGHUP);
109 sigaddset(&sv.sa_mask, SIGTERM);
110 sv.sa_flags = SA_RESTART;
111 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
112 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
118 static void afp_dsi_getmesg (int sig)
121 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
123 #endif /* SERVERTEXT */
125 static void alarm_handler()
127 /* if we're in the midst of processing something,
129 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
130 dsi_tickle(child.obj->handle);
131 } else { /* didn't receive a tickle. close connection */
132 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
139 * Signal handler for SIGUSR1 - set the debug flag and
140 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
142 void afp_set_debug (int sig)
144 char fname[MAXPATHLEN];
146 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
147 freopen(fname, "w", stdout);
148 child.obj->options.flags |= OPTION_DEBUG;
154 /* afp over dsi. this never returns. */
155 void afp_over_dsi(AFPObj *obj)
157 DSI *dsi = (DSI *) obj->handle;
160 struct sigaction action;
162 obj->exit = afp_dsi_die;
163 obj->reply = (int (*)()) dsi_cmdreply;
164 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
167 child.tickle = child.flags = 0;
169 /* install SIGTERM and SIGHUP */
170 memset(&action, 0, sizeof(action));
171 action.sa_handler = afp_dsi_timedown;
172 sigemptyset( &action.sa_mask );
173 sigaddset(&action.sa_mask, SIGALRM);
174 sigaddset(&action.sa_mask, SIGTERM);
175 action.sa_flags = SA_RESTART;
176 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
177 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
181 action.sa_handler = afp_dsi_die;
182 sigemptyset( &action.sa_mask );
183 sigaddset(&action.sa_mask, SIGALRM);
184 sigaddset(&action.sa_mask, SIGHUP);
185 action.sa_flags = SA_RESTART;
186 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
187 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
192 /* Added for server message support */
193 action.sa_handler = afp_dsi_getmesg;
194 sigemptyset( &action.sa_mask );
195 sigaddset(&action.sa_mask, SIGUSR2);
196 action.sa_flags = SA_RESTART;
197 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
198 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
201 #endif /* SERVERTEXT */
203 /* SIGUSR1 - set "debug" flag on this process. */
204 action.sa_handler = afp_set_debug;
205 sigemptyset( &action.sa_mask );
206 sigaddset(&action.sa_mask, SIGUSR1);
207 action.sa_flags = SA_RESTART;
208 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
209 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
214 action.sa_handler = alarm_handler;
215 sigemptyset(&action.sa_mask);
216 sigaddset(&action.sa_mask, SIGHUP);
217 sigaddset(&action.sa_mask, SIGTERM);
218 action.sa_flags = SA_RESTART;
219 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
220 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
224 /* get stuck here until the end */
225 while ((cmd = dsi_receive(dsi))) {
228 if (cmd == DSIFUNC_TICKLE) {
229 /* so we don't get killed on the client side. */
230 if (child.flags & CHILD_DIE)
233 } else if (!(child.flags & CHILD_DIE)) /* reset tickle timer */
234 setitimer(ITIMER_REAL, &dsi->timer, NULL);
239 LOG(log_info, logtype_afpd, "done");
240 if (obj->options.flags & OPTION_DEBUG )
248 if ( flushfork( writtenfork ) < 0 ) {
249 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
255 function = (u_char) dsi->commands[0];
256 if (obj->options.flags & OPTION_DEBUG ) {
257 printf("command: %d (%s)\n", function, AfpNum2name(function));
258 bprint((char *) dsi->commands, dsi->cmdlen);
261 /* send off an afp command. in a couple cases, we take advantage
262 * of the fact that we're a stream-based protocol. */
263 if (afp_switch[function]) {
264 dsi->datalen = DSI_DATASIZ;
265 child.flags |= CHILD_RUNNING;
266 err = (*afp_switch[function])(obj,
267 dsi->commands, dsi->cmdlen,
268 dsi->data, &dsi->datalen);
269 child.flags &= ~CHILD_RUNNING;
271 LOG(log_error, logtype_afpd, "bad function %X", function);
276 /* single shot toggle that gets set by dsi_readinit. */
282 if (obj->options.flags & OPTION_DEBUG ) {
283 printf( "reply: %d, %d\n", err, dsi->clientID);
284 bprint((char *) dsi->data, dsi->datalen);
287 if (!dsi_cmdreply(dsi, err)) {
288 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
293 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
294 function = (u_char) dsi->commands[0];
295 if ( obj->options.flags & OPTION_DEBUG ) {
296 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
297 bprint((char *) dsi->commands, dsi->cmdlen);
300 if ( afp_switch[ function ] != NULL ) {
301 dsi->datalen = DSI_DATASIZ;
302 child.flags |= CHILD_RUNNING;
303 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
304 dsi->data, &dsi->datalen);
305 child.flags &= ~CHILD_RUNNING;
307 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
312 if (obj->options.flags & OPTION_DEBUG ) {
313 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
314 bprint((char *) dsi->data, dsi->datalen);
317 if (!dsi_wrtreply(dsi, err)) {
318 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
323 case DSIFUNC_ATTN: /* attention replies */
327 /* error. this usually implies a mismatch of some kind
328 * between server and client. if things are correct,
329 * we need to flush the rest of the packet if necessary. */
331 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
332 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
337 if ( obj->options.flags & OPTION_DEBUG ) {
341 of_pforkdesc( stdout );