2 * $Id: afp_dsi.c,v 1.43 2009-10-22 05:53:20 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 #define CHILD_DIE (1 << 0)
47 #define CHILD_RUNNING (1 << 1)
48 #define CHILD_SLEEPING (1 << 2)
57 static void afp_dsi_close(AFPObj *obj)
59 DSI *dsi = obj->handle;
65 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
66 dsi->read_count/1024.0, dsi->write_count/1024.0);
71 /* -------------------------------
73 * a little bit of code duplication.
75 static void afp_dsi_die(int sig)
77 static volatile int in_handler;
82 /* it's not atomic but we don't care because it's an exit function
83 * ie if a signal is received here, between the test and the affectation,
88 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
89 afp_dsi_close(child.obj);
90 if (sig) /* if no signal, assume dieing because logins are disabled &
91 don't log it (maintenance mode)*/
92 LOG(log_info, logtype_afpd, "Connection terminated");
93 if (sig == SIGTERM || sig == SIGALRM) {
102 static void afp_dsi_sleep(void)
104 child.flags |= CHILD_SLEEPING;
105 dsi_sleep(child.obj->handle, 1);
108 /* ------------------- */
109 static void afp_dsi_timedown(int sig _U_)
114 child.flags |= CHILD_DIE;
115 /* shutdown and don't reconnect. server going down in 5 minutes. */
116 setmessage("The server is going down for maintenance.");
117 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
118 AFPATTN_MESG | AFPATTN_TIME(5));
120 it.it_interval.tv_sec = 0;
121 it.it_interval.tv_usec = 0;
122 it.it_value.tv_sec = 300;
123 it.it_value.tv_usec = 0;
125 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
126 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
127 afp_dsi_die(EXITERR_SYS);
129 memset(&sv, 0, sizeof(sv));
130 sv.sa_handler = afp_dsi_die;
131 sigemptyset( &sv.sa_mask );
132 sigaddset(&sv.sa_mask, SIGHUP);
133 sigaddset(&sv.sa_mask, SIGTERM);
134 sv.sa_flags = SA_RESTART;
135 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
136 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
137 afp_dsi_die(EXITERR_SYS);
141 sv.sa_handler = SIG_IGN;
142 sigemptyset( &sv.sa_mask );
143 sv.sa_flags = SA_RESTART;
144 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
145 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
146 afp_dsi_die(EXITERR_SYS);
151 /* ---------------------------------
152 * SIGHUP reload configuration file
153 * FIXME here or we wait ?
155 volatile int reload_request = 0;
157 static void afp_dsi_reload(int sig _U_)
162 /* ---------------------- */
164 static void afp_dsi_getmesg (int sig _U_)
166 DSI *dsi = (DSI *) child.obj->handle;
168 dsi->msg_request = 1;
169 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
171 #endif /* SERVERTEXT */
173 static void alarm_handler(int sig _U_)
176 DSI *dsi = (DSI *) child.obj->handle;
178 /* we have to restart the timer because some libraries
180 setitimer(ITIMER_REAL, &dsi->timer, NULL);
182 /* if we're in the midst of processing something,
184 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
188 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
189 if (!(err = pollvoltime(child.obj)))
190 err = dsi_tickle(child.obj->handle);
192 afp_dsi_die(EXITERR_CLNT);
194 } else { /* didn't receive a tickle. close connection */
195 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
196 afp_dsi_die(EXITERR_CLNT);
201 if dsi->in_write is set attention, tickle (and close?) msg
202 aren't sent. We don't care about tickle
204 static void pending_request(DSI *dsi)
206 /* send pending attention */
208 /* read msg if any, it could be done in afp_getsrvrmesg */
209 if (dsi->msg_request) {
210 dsi->msg_request = 0;
211 readmessage(child.obj);
215 /* -------------------------------------------
216 afp over dsi. this never returns.
218 void afp_over_dsi(AFPObj *obj)
220 DSI *dsi = (DSI *) obj->handle;
223 struct sigaction action;
224 const char *afpcmpstr;
226 obj->exit = afp_dsi_die;
227 obj->reply = (int (*)()) dsi_cmdreply;
228 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
230 obj->sleep = afp_dsi_sleep;
232 child.tickle = child.flags = 0;
234 memset(&action, 0, sizeof(action));
237 action.sa_handler = afp_dsi_reload;
238 sigemptyset( &action.sa_mask );
239 sigaddset(&action.sa_mask, SIGALRM);
240 sigaddset(&action.sa_mask, SIGTERM);
241 sigaddset(&action.sa_mask, SIGUSR1);
243 sigaddset(&action.sa_mask, SIGUSR2);
245 action.sa_flags = SA_RESTART;
246 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
247 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
248 afp_dsi_die(EXITERR_SYS);
251 /* install SIGTERM */
252 action.sa_handler = afp_dsi_die;
253 sigemptyset( &action.sa_mask );
254 sigaddset(&action.sa_mask, SIGALRM);
255 sigaddset(&action.sa_mask, SIGHUP);
256 sigaddset(&action.sa_mask, SIGUSR1);
258 sigaddset(&action.sa_mask, SIGUSR2);
260 action.sa_flags = SA_RESTART;
261 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
262 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
263 afp_dsi_die(EXITERR_SYS);
267 /* Added for server message support */
268 action.sa_handler = afp_dsi_getmesg;
269 sigemptyset( &action.sa_mask );
270 sigaddset(&action.sa_mask, SIGALRM);
271 sigaddset(&action.sa_mask, SIGTERM);
272 sigaddset(&action.sa_mask, SIGUSR1);
273 sigaddset(&action.sa_mask, SIGHUP);
274 action.sa_flags = SA_RESTART;
275 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
276 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
277 afp_dsi_die(EXITERR_SYS);
279 #endif /* SERVERTEXT */
281 /* SIGUSR1 - set down in 5 minutes */
282 action.sa_handler = afp_dsi_timedown;
283 sigemptyset( &action.sa_mask );
284 sigaddset(&action.sa_mask, SIGALRM);
285 sigaddset(&action.sa_mask, SIGHUP);
286 sigaddset(&action.sa_mask, SIGTERM);
288 sigaddset(&action.sa_mask, SIGUSR2);
290 action.sa_flags = SA_RESTART;
291 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
292 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
293 afp_dsi_die(EXITERR_SYS);
298 action.sa_handler = alarm_handler;
299 sigemptyset(&action.sa_mask);
300 sigaddset(&action.sa_mask, SIGHUP);
301 sigaddset(&action.sa_mask, SIGTERM);
302 sigaddset(&action.sa_mask, SIGUSR1);
304 sigaddset(&action.sa_mask, SIGUSR2);
306 action.sa_flags = SA_RESTART;
307 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
308 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
309 afp_dsi_die(EXITERR_SYS);
311 #endif /* DEBUGGING */
313 /* get stuck here until the end */
314 while ((cmd = dsi_receive(dsi))) {
316 child.flags &= ~CHILD_SLEEPING;
317 dsi_sleep(dsi, 0); /* wake up */
318 if (reload_request) {
320 load_volumes(child.obj);
323 if (cmd == DSIFUNC_TICKLE) {
324 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
325 if ((child.flags & CHILD_DIE))
327 pending_request(dsi);
333 LOG(log_info, logtype_afpd, "done");
340 if ( flushfork( writtenfork ) < 0 ) {
341 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
347 function = (u_char) dsi->commands[0];
349 /* send off an afp command. in a couple cases, we take advantage
350 * of the fact that we're a stream-based protocol. */
351 if (afp_switch[function]) {
352 dsi->datalen = DSI_DATASIZ;
353 child.flags |= CHILD_RUNNING;
355 afpcmpstr = AfpNum2name(function);
356 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
358 err = (*afp_switch[function])(obj,
359 dsi->commands, dsi->cmdlen,
360 dsi->data, &dsi->datalen);
362 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
364 /* bring everything back to old euid, egid */
366 restore_uidgid ( &obj->uidgid );
367 #endif /* FORCE_UIDGID */
368 child.flags &= ~CHILD_RUNNING;
370 LOG(log_error, logtype_afpd, "bad function %X", function);
375 /* single shot toggle that gets set by dsi_readinit. */
381 if (!dsi_cmdreply(dsi, err)) {
382 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
383 afp_dsi_die(EXITERR_CLNT);
387 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
388 function = (u_char) dsi->commands[0];
389 if ( afp_switch[ function ] != NULL ) {
390 dsi->datalen = DSI_DATASIZ;
391 child.flags |= CHILD_RUNNING;
392 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
393 dsi->data, &dsi->datalen);
394 child.flags &= ~CHILD_RUNNING;
396 /* bring everything back to old euid, egid */
398 restore_uidgid ( &obj->uidgid );
399 #endif /* FORCE_UIDGID */
401 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
406 if (!dsi_wrtreply(dsi, err)) {
407 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
408 afp_dsi_die(EXITERR_CLNT);
412 case DSIFUNC_ATTN: /* attention replies */
415 /* error. this usually implies a mismatch of some kind
416 * between server and client. if things are correct,
417 * we need to flush the rest of the packet if necessary. */
419 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
420 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
424 pending_request(dsi);
428 afp_dsi_die(EXITERR_CLNT);