]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/main.c
Log warnings instead of exiting when increasing rlimits doesn't work out
[netatalk.git] / etc / afpd / main.c
index 2606f800c4267cc3fcd1a4486e08d21c807e9869..edcfe6a7565d8339e5239b8ecead0a39fb66a821 100644 (file)
@@ -51,6 +51,9 @@ static int argc = 0;
 static char **argv = NULL;
 #endif /* TRU64 */
 
+#define AFP_LISTENERS 32
+#define FDSET_SAFETY  5
+
 unsigned char  nologin = 0;
 struct afp_options default_options;
 static AFPConfig *configs;
@@ -92,11 +95,25 @@ static void fd_set_listening_sockets(void)
     for (config = configs; config; config = config->next) {
         if (config->fd < 0) /* for proxies */
             continue;
-        fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, config->fd, LISTEN_FD, config);
+        fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
+                     &fdset,
+                     &polldata,
+                     &fdset_used,
+                     &fdset_size,
+                     config->fd,
+                     LISTEN_FD,
+                     config);
     }
 
     if (default_options.flags & OPTION_KEEPSESSIONS)
-        fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, disasociated_ipc_fd, DISASOCIATED_IPC_FD, NULL);
+        fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
+                     &fdset,
+                     &polldata,
+                     &fdset_used,
+                     &fdset_size,
+                     disasociated_ipc_fd,
+                     DISASOCIATED_IPC_FD,
+                     NULL);
 }
  
 static void fd_reset_listening_sockets(void)
@@ -211,16 +228,16 @@ static int setlimits(void)
     struct rlimit rlim;
 
     if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
-        LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
-        exit(1);
+        LOG(log_warning, logtype_afpd, "setlimits: reading current limits failed: %s", strerror(errno));
+        return -1;
     }
     if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
         rlim.rlim_cur = 65535;
         if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
             rlim.rlim_max = 65535;
         if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
-            LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
-            exit(1);
+            LOG(log_warning, logtype_afpd, "setlimits: increasing limits failed: %s", strerror(errno));
+            return -1;
         }
     }
     return 0;
@@ -241,29 +258,30 @@ int main(int ac, char **av)
     set_auth_parameters( ac, av );
 #endif /* TRU64 */
 
+    /* Parse argv args and initialize default options */
+    afp_options_init(&default_options);
+    if (!afp_options_parse(ac, av, &default_options))
+        exit(EXITERR_CONF);
+
+    if (check_lockfile("afpd", default_options.pidfile) != 0)
+        exit(EXITERR_SYS);
+
+    if (!(default_options.flags & OPTION_DEBUG) && (daemonize(0, 0) != 0))
+        exit(EXITERR_SYS);
+
+    if (create_lockfile("afpd", default_options.pidfile) != 0)
+        exit(EXITERR_SYS);
+
     /* Log SIGBUS/SIGSEGV SBT */
     fault_setup(NULL);
 
     /* Default log setup: log to syslog */
+    set_processname("afpd");
     setuplog("default log_note");
 
-    afp_options_init(&default_options);
-    if (!afp_options_parse(ac, av, &default_options))
-        exit(EXITERR_CONF);
-
-    /* Save the user's current umask for use with CNID (and maybe some 
-     * other things, too). */
+    /* Save the user's current umask */
     default_options.save_mask = umask( default_options.umask );
 
-    switch(server_lock("afpd", default_options.pidfile,
-                       default_options.flags & OPTION_DEBUG)) {
-    case -1: /* error */
-        exit(EXITERR_SYS);
-    case 0: /* child */
-        break;
-    default: /* server */
-        exit(0);
-    }
     atexit(afp_exit);
 
     /* install child handler for asp and dsi. we do this before afp_goaway
@@ -459,7 +477,14 @@ int main(int ac, char **av)
                     /* config->server_start is afp_config.c:dsi_start() for DSI */
                     if (child = config->server_start(config, configs, server_children)) {
                         /* Add IPC fd to select fd set */
-                        fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0], IPC_FD, child);
+                        fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
+                                     &fdset,
+                                     &polldata,
+                                     &fdset_used,
+                                     &fdset_size,
+                                     child->ipc_fds[0],
+                                     IPC_FD,
+                                     child);
                     }
                     break;
 
@@ -496,7 +521,14 @@ int main(int ac, char **av)
                         break;
                     }
                     child->disasociated = 1;
-                    fdset_add_fd(&fdset, &polldata, &fdset_used, &fdset_size, fd[0], IPC_FD, child);
+                    fdset_add_fd(default_options.connections + AFP_LISTENERS + FDSET_SAFETY,
+                                 &fdset,
+                                 &polldata,
+                                 &fdset_used,
+                                 &fdset_size,
+                                 fd[0],
+                                 IPC_FD,
+                                 child);
                     break;
 
                 default: