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;
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)
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;
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
(void)setlimits();
afp_child_t *child;
- int fd[2]; /* we only use one, but server_child_add expects [2] */
+ int recon_ipc_fd;
pid_t pid;
int saveerrno;
/* 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_fd,
+ IPC_FD,
+ child);
}
break;
child = (afp_child_t *)polldata[i].data;
LOG(log_debug, logtype_afpd, "main: IPC request from child[%u]", child->pid);
- if (ipc_server_read(server_children, child->ipc_fds[0]) != 0) {
- fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fds[0]);
- close(child->ipc_fds[0]);
- child->ipc_fds[0] = -1;
+ if (ipc_server_read(server_children, child->ipc_fd) != 0) {
+ fdset_del_fd(&fdset, &polldata, &fdset_used, &fdset_size, child->ipc_fd);
+ close(child->ipc_fd);
+ child->ipc_fd = -1;
if ((default_options.flags & OPTION_KEEPSESSIONS) && child->disasociated) {
LOG(log_note, logtype_afpd, "main: removing reattached child[%u]", child->pid);
server_child_remove(server_children, CHILD_DSIFORK, child->pid);
case DISASOCIATED_IPC_FD:
LOG(log_debug, logtype_afpd, "main: IPC reconnect request");
- if ((fd[0] = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
+ if ((recon_ipc_fd = accept(disasociated_ipc_fd, NULL, NULL)) == -1) {
LOG(log_error, logtype_afpd, "main: accept: %s", strerror(errno));
break;
}
- if (readt(fd[0], &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
+ if (readt(recon_ipc_fd, &pid, sizeof(pid_t), 0, 1) != sizeof(pid_t)) {
LOG(log_error, logtype_afpd, "main: readt: %s", strerror(errno));
- close(fd[0]);
+ close(recon_ipc_fd);
break;
}
LOG(log_note, logtype_afpd, "main: IPC reconnect from pid [%u]", pid);
- if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, fd)) == NULL) {
+
+ if ((child = server_child_add(server_children, CHILD_DSIFORK, pid, recon_ipc_fd)) == NULL) {
LOG(log_error, logtype_afpd, "main: server_child_add");
- close(fd[0]);
+ close(recon_ipc_fd);
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,
+ recon_ipc_fd,
+ IPC_FD,
+ child);
break;
default: