From: Ross S. W. W. <RW...@me...> - 2010-06-29 17:19:52
|
The PID file test wasn't performed early enough which allowed a second ietd instance starting up to mess up the netlink socket state for the running ietd. With this patch the new order of ietd startup: - process options - initialize log (if daemon) - disassociate tty (if daemon) - create PID file, prevents second instance from starting - create IPC socket, if PID was removed this will also prevent a second instance from starting - open control device - test version - open netlink socket for ietd<->kernel messaging - read config - set effective GID/UID - enter event loop Signed-off-by: Ross Walker Index: usr/ietd.c =================================================================== --- usr/ietd.c (revision 339) +++ usr/ietd.c (working copy) @@ -434,11 +434,11 @@ void event_loop(int timeout) int main(int argc, char **argv) { int ch, longindex, timeout = -1; - char *config = NULL; + char *config = NULL, pid_buf[64]; uid_t uid = 0; gid_t gid = 0; char *isns = NULL; - int isns_ac = 0; + int isns_ac = 0, pid_fd; /* otherwise we would die in some later write() during the event_loop * instead of getting EPIPE! */ @@ -480,81 +480,86 @@ int main(int argc, char **argv) } } - log_init(); + if (log_daemon) { + pid_t pid; - if ((nl_fd = nl_open()) < 0) { - log_error("unable to open netlink fd: %m"); - exit(-1); - }; + log_init(); - if ((ctrl_fd = ki->ctldev_open()) < 0) { - log_error("unable to open ctldev fd: %m"); + pid = fork(); + if (pid < 0) { + log_error("error starting daemon: %m"); + exit(-1); + } else if (pid) + exit(0); + + close(0); + open("/dev/null", O_RDWR); + dup2(0, 1); + dup2(0, 2); + + setsid(); + } + + pid_fd = open("/var/run/ietd.pid", O_WRONLY|O_CREAT, 0644); + if (pid_fd < 0) { + log_error("unable to create pid file: %m"); exit(-1); } - if ((ipc_fd = ietadm_request_listen()) < 0) { - log_error("unable to open ipc fd: %m"); + if (chdir("/") < 0) { + log_error("failed to set working dir to /: %m"); exit(-1); } - if (!check_version()) { - log_error("kernel module version mismatch!"); + if (lockf(pid_fd, F_TLOCK, 0) < 0) { + log_error("unable to lock pid file: %m"); exit(-1); } - if (log_daemon) { - char buf[64]; - pid_t pid; - int fd; + if (ftruncate(pid_fd, 0) < 0) { + log_error("failed to ftruncate the PID file: %m"); + exit(-1); + } - fd = open("/var/run/iscsi_trgt.pid", O_WRONLY|O_CREAT, 0644); - if (fd < 0) { - log_error("unable to create pid file: %m"); - exit(1); - } - pid = fork(); - if (pid < 0) { - log_error("error starting daemon: %m"); - exit(1); - } else if (pid) - exit(0); + sprintf(pid_buf, "%d\n", getpid()); + if (write(pid_fd, pid_buf, strlen(pid_buf)) < strlen(pid_buf)) { + log_error("failed to write PID to PID file: %m"); + exit(-1); + } - if (chdir("/") < 0) { - log_error("failed to set working dir to /: %m"); - exit(1); - } + if ((ipc_fd = ietadm_request_listen()) < 0) { + log_error("unable to open ipc fd: %m"); + exit(-1); + } - if (lockf(fd, F_TLOCK, 0) < 0) { - log_error("unable to lock pid file: %m"); - exit(1); - } - if (ftruncate(fd, 0) < 0) { - log_error("failed to ftruncate the PID file: %m"); - exit(1); - } + if ((ctrl_fd = ki->ctldev_open()) < 0) { + log_error("unable to open ctldev fd: %m"); + exit(-1); + } - sprintf(buf, "%d\n", getpid()); - if (write(fd, buf, strlen(buf)) < strlen(buf)) { - log_error("failed to write PID to PID file: %m"); - exit(1); - } + if (!check_version()) { + log_error("kernel module version mismatch!"); + exit(-1); + } - close(0); - open("/dev/null", O_RDWR); - dup2(0, 1); - dup2(0, 2); - setsid(); + if ((nl_fd = nl_open()) < 0) { + log_error("unable to open netlink fd: %m"); + exit(-1); } cops->init(config, &isns, &isns_ac); if (isns) timeout = isns_init(isns, isns_ac); - if (gid && setgid(gid) < 0) + if (gid && setgid(gid) < 0) { log_error("unable to setgid: %m"); + exit(-1); + } - if (uid && setuid(uid) < 0) + if (uid && setuid(uid) < 0) { log_error("unable to setuid: %m"); + exit(-1); + } event_loop(timeout); ______________________________________________________________________ This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender and permanently delete the original and any copy or printout thereof. |