2 * $Id: afp_dsi.c,v 1.44 2009-10-22 07:40:50 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)
49 #define CHILD_DATA (1 << 3)
58 static void afp_dsi_close(AFPObj *obj)
60 DSI *dsi = obj->handle;
66 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written",
67 dsi->read_count/1024.0, dsi->write_count/1024.0);
72 /* -------------------------------
74 * a little bit of code duplication.
76 static void afp_dsi_die(int sig)
78 static volatile int in_handler;
83 /* it's not atomic but we don't care because it's an exit function
84 * ie if a signal is received here, between the test and the affectation,
89 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN);
90 afp_dsi_close(child.obj);
91 if (sig) /* if no signal, assume dieing because logins are disabled &
92 don't log it (maintenance mode)*/
93 LOG(log_info, logtype_afpd, "Connection terminated");
94 if (sig == SIGTERM || sig == SIGALRM) {
103 static void afp_dsi_sleep(void)
105 child.flags |= CHILD_SLEEPING;
106 dsi_sleep(child.obj->handle, 1);
109 /* ------------------- */
110 static void afp_dsi_timedown(int sig _U_)
115 child.flags |= CHILD_DIE;
116 /* shutdown and don't reconnect. server going down in 5 minutes. */
117 setmessage("The server is going down for maintenance.");
118 dsi_attention(child.obj->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT |
119 AFPATTN_MESG | AFPATTN_TIME(5));
121 it.it_interval.tv_sec = 0;
122 it.it_interval.tv_usec = 0;
123 it.it_value.tv_sec = 300;
124 it.it_value.tv_usec = 0;
126 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
127 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
128 afp_dsi_die(EXITERR_SYS);
130 memset(&sv, 0, sizeof(sv));
131 sv.sa_handler = afp_dsi_die;
132 sigemptyset( &sv.sa_mask );
133 sigaddset(&sv.sa_mask, SIGHUP);
134 sigaddset(&sv.sa_mask, SIGTERM);
135 sv.sa_flags = SA_RESTART;
136 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
137 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
138 afp_dsi_die(EXITERR_SYS);
142 sv.sa_handler = SIG_IGN;
143 sigemptyset( &sv.sa_mask );
144 sv.sa_flags = SA_RESTART;
145 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
146 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGHUP: %s", strerror(errno) );
147 afp_dsi_die(EXITERR_SYS);
152 /* ---------------------------------
153 * SIGHUP reload configuration file
154 * FIXME here or we wait ?
156 volatile int reload_request = 0;
158 static void afp_dsi_reload(int sig _U_)
163 /* ---------------------- */
165 static void afp_dsi_getmesg (int sig _U_)
167 DSI *dsi = (DSI *) child.obj->handle;
169 dsi->msg_request = 1;
170 dsi_attention(child.obj->handle, AFPATTN_MESG | AFPATTN_TIME(5));
172 #endif /* SERVERTEXT */
174 static void alarm_handler(int sig _U_)
177 DSI *dsi = (DSI *) child.obj->handle;
179 /* we have to restart the timer because some libraries
181 setitimer(ITIMER_REAL, &dsi->timer, NULL);
183 /* we got some traffic from the client since the previous timer
185 if ((child.flags & CHILD_DATA)) {
186 child.flags &= ~CHILD_DATA;
190 /* if we're in the midst of processing something,
192 if ((child.flags & CHILD_SLEEPING) && child.tickle++ < child.obj->options.sleep) {
196 if ((child.flags & CHILD_RUNNING) || (child.tickle++ < child.obj->options.timeout)) {
197 if (!(err = pollvoltime(child.obj)))
198 err = dsi_tickle(child.obj->handle);
200 afp_dsi_die(EXITERR_CLNT);
202 } else { /* didn't receive a tickle. close connection */
203 LOG(log_error, logtype_afpd, "afp_alarm: child timed out");
204 afp_dsi_die(EXITERR_CLNT);
209 if dsi->in_write is set attention, tickle (and close?) msg
210 aren't sent. We don't care about tickle
212 static void pending_request(DSI *dsi)
214 /* send pending attention */
216 /* read msg if any, it could be done in afp_getsrvrmesg */
217 if (dsi->msg_request) {
218 dsi->msg_request = 0;
219 readmessage(child.obj);
223 /* -------------------------------------------
224 afp over dsi. this never returns.
226 void afp_over_dsi(AFPObj *obj)
228 DSI *dsi = (DSI *) obj->handle;
231 struct sigaction action;
232 const char *afpcmpstr;
234 obj->exit = afp_dsi_die;
235 obj->reply = (int (*)()) dsi_cmdreply;
236 obj->attention = (int (*)(void *, AFPUserBytes)) dsi_attention;
238 obj->sleep = afp_dsi_sleep;
240 child.tickle = child.flags = 0;
242 memset(&action, 0, sizeof(action));
245 action.sa_handler = afp_dsi_reload;
246 sigemptyset( &action.sa_mask );
247 sigaddset(&action.sa_mask, SIGALRM);
248 sigaddset(&action.sa_mask, SIGTERM);
249 sigaddset(&action.sa_mask, SIGUSR1);
251 sigaddset(&action.sa_mask, SIGUSR2);
253 action.sa_flags = SA_RESTART;
254 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
255 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
256 afp_dsi_die(EXITERR_SYS);
259 /* install SIGTERM */
260 action.sa_handler = afp_dsi_die;
261 sigemptyset( &action.sa_mask );
262 sigaddset(&action.sa_mask, SIGALRM);
263 sigaddset(&action.sa_mask, SIGHUP);
264 sigaddset(&action.sa_mask, SIGUSR1);
266 sigaddset(&action.sa_mask, SIGUSR2);
268 action.sa_flags = SA_RESTART;
269 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
270 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
271 afp_dsi_die(EXITERR_SYS);
275 /* Added for server message support */
276 action.sa_handler = afp_dsi_getmesg;
277 sigemptyset( &action.sa_mask );
278 sigaddset(&action.sa_mask, SIGALRM);
279 sigaddset(&action.sa_mask, SIGTERM);
280 sigaddset(&action.sa_mask, SIGUSR1);
281 sigaddset(&action.sa_mask, SIGHUP);
282 action.sa_flags = SA_RESTART;
283 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
284 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
285 afp_dsi_die(EXITERR_SYS);
287 #endif /* SERVERTEXT */
289 /* SIGUSR1 - set down in 5 minutes */
290 action.sa_handler = afp_dsi_timedown;
291 sigemptyset( &action.sa_mask );
292 sigaddset(&action.sa_mask, SIGALRM);
293 sigaddset(&action.sa_mask, SIGHUP);
294 sigaddset(&action.sa_mask, SIGTERM);
296 sigaddset(&action.sa_mask, SIGUSR2);
298 action.sa_flags = SA_RESTART;
299 if ( sigaction( SIGUSR1, &action, NULL) < 0 ) {
300 LOG(log_error, logtype_afpd, "afp_over_dsi: sigaction: %s", strerror(errno) );
301 afp_dsi_die(EXITERR_SYS);
306 action.sa_handler = alarm_handler;
307 sigemptyset(&action.sa_mask);
308 sigaddset(&action.sa_mask, SIGHUP);
309 sigaddset(&action.sa_mask, SIGTERM);
310 sigaddset(&action.sa_mask, SIGUSR1);
312 sigaddset(&action.sa_mask, SIGUSR2);
314 action.sa_flags = SA_RESTART;
315 if ((sigaction(SIGALRM, &action, NULL) < 0) ||
316 (setitimer(ITIMER_REAL, &dsi->timer, NULL) < 0)) {
317 afp_dsi_die(EXITERR_SYS);
319 #endif /* DEBUGGING */
321 /* get stuck here until the end */
322 while ((cmd = dsi_receive(dsi))) {
324 child.flags &= ~CHILD_SLEEPING;
325 dsi_sleep(dsi, 0); /* wake up */
326 if (reload_request) {
328 load_volumes(child.obj);
331 if (cmd == DSIFUNC_TICKLE) {
332 /* timer is not every 30 seconds anymore, so we don't get killed on the client side. */
333 if ((child.flags & CHILD_DIE))
335 pending_request(dsi);
339 child.flags |= CHILD_DATA;
343 LOG(log_info, logtype_afpd, "done");
350 if ( flushfork( writtenfork ) < 0 ) {
351 LOG(log_error, logtype_afpd, "main flushfork: %s", strerror(errno) );
357 function = (u_char) dsi->commands[0];
359 /* send off an afp command. in a couple cases, we take advantage
360 * of the fact that we're a stream-based protocol. */
361 if (afp_switch[function]) {
362 dsi->datalen = DSI_DATASIZ;
363 child.flags |= CHILD_RUNNING;
365 afpcmpstr = AfpNum2name(function);
366 LOG(log_debug, logtype_afpd, "=> Start AFP command: %s", afpcmpstr);
368 err = (*afp_switch[function])(obj,
369 dsi->commands, dsi->cmdlen,
370 dsi->data, &dsi->datalen);
372 LOG(log_debug, logtype_afpd, "=> Finished AFP command: %s", afpcmpstr);
374 /* bring everything back to old euid, egid */
376 restore_uidgid ( &obj->uidgid );
377 #endif /* FORCE_UIDGID */
378 child.flags &= ~CHILD_RUNNING;
380 LOG(log_error, logtype_afpd, "bad function %X", function);
385 /* single shot toggle that gets set by dsi_readinit. */
391 if (!dsi_cmdreply(dsi, err)) {
392 LOG(log_error, logtype_afpd, "dsi_cmdreply(%d): %s", dsi->socket, strerror(errno) );
393 afp_dsi_die(EXITERR_CLNT);
397 case DSIFUNC_WRITE: /* FPWrite and FPAddIcon */
398 function = (u_char) dsi->commands[0];
399 if ( afp_switch[ function ] != NULL ) {
400 dsi->datalen = DSI_DATASIZ;
401 child.flags |= CHILD_RUNNING;
402 err = (*afp_switch[function])(obj, dsi->commands, dsi->cmdlen,
403 dsi->data, &dsi->datalen);
404 child.flags &= ~CHILD_RUNNING;
406 /* bring everything back to old euid, egid */
408 restore_uidgid ( &obj->uidgid );
409 #endif /* FORCE_UIDGID */
411 LOG(log_error, logtype_afpd, "(write) bad function %x", function);
416 if (!dsi_wrtreply(dsi, err)) {
417 LOG(log_error, logtype_afpd, "dsi_wrtreply: %s", strerror(errno) );
418 afp_dsi_die(EXITERR_CLNT);
422 case DSIFUNC_ATTN: /* attention replies */
425 /* error. this usually implies a mismatch of some kind
426 * between server and client. if things are correct,
427 * we need to flush the rest of the packet if necessary. */
429 LOG(log_info, logtype_afpd,"afp_dsi: spurious command %d", cmd);
430 dsi_writeinit(dsi, dsi->data, DSI_DATASIZ);
434 pending_request(dsi);
438 afp_dsi_die(EXITERR_CLNT);