2 * $Id: afp_dsi.c,v 1.27.2.2 2003-06-23 10:25:07 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 /* -------------------------------
76 * a little bit of code duplication.
78 static void afp_dsi_die(int sig)
80 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
81 afp_dsi_close(child.obj);
82 if (sig) /* if no signal, assume dieing because logins are disabled &
83 don't log it (maintenance mode)*/
84 LOG(log_info, logtype_afpd, "Connection terminated");
85 if (sig == SIGTERM || sig == SIGALRM) {
93 /* ---------------------------------
94 * SIGUSR1 down in five mn.
96 static void afp_dsi_timedown()
101 child.flags |= CHILD_DIE;
102 /* shutdown and don't reconnect. server going down in 5 minutes. */
103 setmessage("The server is going down for maintenance.");
104 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
105 AFPATTN_MESG | AFPATTN_TIME(5));
107 it.it_interval.tv_sec = 0;
108 it.it_interval.tv_usec = 0;
109 it.it_value.tv_sec = 300;
110 it.it_value.tv_usec = 0;
112 if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
113 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
116 memset(&sv, 0, sizeof(sv));
117 sv.sa_handler = afp_dsi_die;
118 sigemptyset( &sv.sa_mask );
119 sigaddset(&sv.sa_mask, SIGHUP);
120 sigaddset(&sv.sa_mask, SIGTERM);
121 sv.sa_flags = SA_RESTART;
122 if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
123 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
128 sv.sa_handler = SIG_IGN;
129 sigemptyset( &sv.sa_mask );
130 sv.sa_flags = SA_RESTART;
131 if ( sigaction( SIGUSR1, &sv, 0 ) < 0 ) {
132 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
138 /* ---------------------------------
139 * SIGHUP reload configuration file
140 * FIXME here or we wait ?
142 volatile int reload_request = 0;
144 static void afp_dsi_reload()
149 /* ---------------------- */
151 static void afp_dsi_getmesg (int sig)
154 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
156 #endif /* SERVERTEXT */
158 /* ---------------------- */
159 static void alarm_handler()
162 /* if we're in the midst of processing something,
164 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
165 if (!(err = pollvoltime(child.obj)))
166 err = dsi_tickle(child.obj->handle);
170 } else { /* didn't receive a tickle. close connection */
171 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
177 /* ---------------------------------
178 * old signal handler for SIGUSR1 - set the debug flag and
179 * redirect stdout to <tmpdir>/afpd-debug-<pid>.
181 void afp_set_debug (int sig)
183 char fname[MAXPATHLEN];
185 snprintf(fname, MAXPATHLEN-1, "%safpd-debug-%d", P_tmpdir, getpid());
186 freopen(fname, "w", stdout);
187 child.obj->options.flags |= OPTION_DEBUG;
192 /* -------------------------------------------
193 afp over dsi. this never returns.
195 void afp_over_dsi(AFPObj *obj)
197 DSI *dsi = (DSI *) obj->handle;
200 struct sigaction action;
202 obj->exit = afp_dsi_die;
203 obj->reply = (int (*)()) dsi_cmdreply;
204 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
207 child.tickle = child.flags = 0;
209 memset(&action, 0, sizeof(action));
212 action.sa_handler = afp_dsi_reload;
213 sigemptyset( &action.sa_mask );
214 sigaddset(&action.sa_mask, SIGALRM);
215 sigaddset(&action.sa_mask, SIGTERM);
216 sigaddset(&action.sa_mask, SIGUSR1);
217 action.sa_flags = SA_RESTART;
218 if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
219 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
223 /* install SIGTERM */
224 action.sa_handler = afp_dsi_die;
225 sigemptyset( &action.sa_mask );
226 sigaddset(&action.sa_mask, SIGALRM);
227 sigaddset(&action.sa_mask, SIGHUP);
228 sigaddset(&action.sa_mask, SIGUSR1);
229 action.sa_flags = SA_RESTART;
230 if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
231 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
236 /* Added for server message support */
237 action.sa_handler = afp_dsi_getmesg;
238 sigemptyset( &action.sa_mask );
239 sigaddset(&action.sa_mask, SIGUSR2);
240 action.sa_flags = SA_RESTART;
241 if ( sigaction( SIGUSR2, &action, 0) < 0 ) {
242 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
245 #endif /* SERVERTEXT */
247 /* SIGUSR1 - set down in 5 minutes */
248 action.sa_handler = afp_dsi_timedown;
249 sigemptyset( &action.sa_mask );
250 sigaddset(&action.sa_mask, SIGALRM);
251 sigaddset(&action.sa_mask, SIGHUP);
252 sigaddset(&action.sa_mask, SIGTERM);
253 action.sa_flags = SA_RESTART;
254 if ( sigaction( SIGUSR1, &action, 0) < 0 ) {
255 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
260 action.sa_handler = alarm_handler;
261 sigemptyset(&action.sa_mask);
262 sigaddset(&action.sa_mask, SIGHUP);
263 sigaddset(&action.sa_mask, SIGTERM);
264 action.sa_flags = SA_RESTART;
265 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
266 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
270 /* get stuck here until the end */
271 while ((cmd = dsi_receive(dsi))) {
273 dsi_sleep(dsi, 0); /* wake up */
274 if (reload_request) {
276 load_volumes(child.obj);
279 if (cmd == DSIFUNC_TICKLE) {
280 /* so we don't get killed on the client side. */
281 if (child.flags & CHILD_DIE)
284 } else if (!(child.flags & CHILD_DIE)) { /* reset tickle timer */
285 setitimer(ITIMER_REAL, &dsi->timer, NULL);
290 LOG(log_info, logtype_afpd, "done");
291 if (obj->options.flags & OPTION_DEBUG )
299 if ( flushfork( writtenfork ) < 0 ) {
300 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
306 function = (u_char) dsi->commands[0];
307 if (obj->options.flags & OPTION_DEBUG ) {
308 printf("command: %d (%s)\n", function, AfpNum2name(function));
309 bprint((char *) dsi->commands, dsi->cmdlen);
312 /* send off an afp command. in a couple cases, we take advantage
313 * of the fact that we're a stream-based protocol. */
314 if (afp_switch[function]) {
315 dsi->datalen = DSI_DATASIZ;
316 child.flags |= CHILD_RUNNING;
318 err = (*afp_switch[function])(obj,
319 dsi->commands, dsi->cmdlen,
320 dsi->data, &dsi->datalen);
322 /* bring everything back to old euid, egid */
324 restore_uidgid ( &obj->uidgid );
325 #endif /* FORCE_UIDGID */
326 child.flags &= ~CHILD_RUNNING;
328 LOG(log_error, logtype_afpd, "bad function %X", function);
333 /* single shot toggle that gets set by dsi_readinit. */
339 if (obj->options.flags & OPTION_DEBUG ) {
340 printf( "reply: %d, %d\n", err, dsi->clientID);
341 bprint((char *) dsi->data, dsi->datalen);
344 if (!dsi_cmdreply(dsi, err)) {
345 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
350 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
351 function = (u_char) dsi->commands[0];
352 if ( obj->options.flags & OPTION_DEBUG ) {
353 printf("(write) command: %d, %d\n", function, dsi->cmdlen);
354 bprint((char *) dsi->commands, dsi->cmdlen);
357 if ( afp_switch[ function ] != NULL ) {
358 dsi->datalen = DSI_DATASIZ;
359 child.flags |= CHILD_RUNNING;
360 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
361 dsi->data, &dsi->datalen);
362 child.flags &= ~CHILD_RUNNING;
364 /* bring everything back to old euid, egid */
366 restore_uidgid ( &obj->uidgid );
367 #endif /* FORCE_UIDGID */
369 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
374 if (obj->options.flags & OPTION_DEBUG ) {
375 printf( "(write) reply code: %d, %d\n", err, dsi->clientID);
376 bprint((char *) dsi->data, dsi->datalen);
379 if (!dsi_wrtreply(dsi, err)) {
380 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
385 case DSIFUNC_ATTN: /* attention replies */
389 /* error. this usually implies a mismatch of some kind
390 * between server and client. if things are correct,
391 * we need to flush the rest of the packet if necessary. */
393 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
394 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
399 if ( obj->options.flags & OPTION_DEBUG ) {
403 of_pforkdesc( stdout );