From 40ae5ef68734fe8a46529ac7c9545210f4798fec Mon Sep 17 00:00:00 2001 From: adilger Date: Wed, 10 Aug 2005 17:23:16 +0000 Subject: [PATCH] Land b_release_1_4_4 onto HEAD (20050810_0253) Check for error writing to acceptor $PIDFILE. Accptor reports when it has exited to syslog, so we can trace if it is a culprit in the "bind: address already in use" problem. b=5509 b=7283 --- lnet/ulnds/connection.c | 2 +- lnet/ulnds/socklnd/connection.c | 2 +- lnet/utils/acceptor.c | 52 ++++++++++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/lnet/ulnds/connection.c b/lnet/ulnds/connection.c index ed68355..49cca96 100644 --- a/lnet/ulnds/connection.c +++ b/lnet/ulnds/connection.c @@ -455,7 +455,7 @@ static int bind_socket(manager m,unsigned short port) addr.sin_port = htons(port); if (bind(m->bound,(struct sockaddr *)&addr,alen)<0){ - perror ("tcpnal bind"); + fprintf(stderr, "tcpnal bind: %s port %u\n", strerror(errno), port); return(0); } diff --git a/lnet/ulnds/socklnd/connection.c b/lnet/ulnds/socklnd/connection.c index ed68355..49cca96 100644 --- a/lnet/ulnds/socklnd/connection.c +++ b/lnet/ulnds/socklnd/connection.c @@ -455,7 +455,7 @@ static int bind_socket(manager m,unsigned short port) addr.sin_port = htons(port); if (bind(m->bound,(struct sockaddr *)&addr,alen)<0){ - perror ("tcpnal bind"); + fprintf(stderr, "tcpnal bind: %s port %u\n", strerror(errno), port); return(0); } diff --git a/lnet/utils/acceptor.c b/lnet/utils/acceptor.c index 03b69ce..a270ad2 100644 --- a/lnet/utils/acceptor.c +++ b/lnet/utils/acceptor.c @@ -81,18 +81,30 @@ char *pidfile_name(char *name_port) return pidfile; } -void pidfile_create(char *name_port) +int pidfile_create(char *name_port) { char *pidfile = pidfile_name(name_port); - FILE *fp; - - if ((fp = fopen(pidfile, "w"))) { - fprintf(fp, "%d\n", getpid()); - fclose(fp); + int fd, rc; + + if ((fd = open(pidfile, O_CREAT | O_WRONLY)) >= 0) { + char pid[16]; + int size = snprintf(pid, sizeof(pid), "%u\n", getpid()); + if (write(fd, pid, size) != size) { + /* hard error or short write */ + rc = errno ? : EIO; + } else { + rc = 0; + } + close(fd); } else { - errlog(LOG_ERR, " error creating %s: %s\n", - pidfile, strerror(errno)); + rc = errno; } + + if (rc) + errlog(LOG_ERR, " error creating %s: %s\n", + pidfile, strerror(rc)); + + return rc; } int pidfile_cleanup(char *name_port) @@ -104,6 +116,7 @@ int pidfile_cleanup(char *name_port) if (rc && errno != -ENOENT) fprintf(stderr, "%s: error removing %s: %s\n", progname, pidfile, strerror(errno)); + errlog(LOG_NOTICE, "exiting\n"); return errno; } @@ -147,10 +160,14 @@ stale: void handler(int sig) { - pidfile_cleanup(name_port); exit(sig); } +void atexit_handler(void) +{ + pidfile_cleanup(name_port); +} + void show_connection(int fd, __u32 net_ip) { static long last_time; @@ -272,7 +289,8 @@ int main(int argc, char **argv) signal(SIGTERM, handler); errlog(LOG_NOTICE, "started, listening on port %d\n", port); - pidfile_create(name_port); + if (pidfile_create(name_port) == 0) + atexit(atexit_handler); while (1) { struct sockaddr_in clntaddr; @@ -293,13 +311,12 @@ int main(int argc, char **argv) //continue; } + inet_ntop(AF_INET, &clntaddr.sin_addr, addrstr,INET_ADDRSTRLEN); #ifdef HAVE_LIBWRAP /* libwrap access control */ request_init(&request, RQ_DAEMON, "lustre", RQ_FILE, cfd, 0); sock_host(&request); if (!hosts_access(&request)) { - inet_ntop(AF_INET, &clntaddr.sin_addr, - addrstr, INET_ADDRSTRLEN); errlog(LOG_WARNING, "unauthorized access from %s:%hd\n", addrstr, ntohs(clntaddr.sin_port)); close (cfd); @@ -309,8 +326,6 @@ int main(int argc, char **argv) if (require_privports && ntohs(clntaddr.sin_port) >= IPPORT_RESERVED) { - inet_ntop(AF_INET, &clntaddr.sin_addr, - addrstr, INET_ADDRSTRLEN); errlog(LOG_ERR, "closing non-privileged connection from %s:%d\n", addrstr, ntohs(clntaddr.sin_port)); @@ -332,18 +347,17 @@ int main(int argc, char **argv) data.ioc_plen1 = sizeof(pcfg); if (ioctl(pfd, IOC_PORTAL_NAL_CMD, &data) < 0) { - errlog(LOG_ERR, - "portals ioctl failed: %s\n", strerror(errno)); + errlog(LOG_ERR, "portals ioctl failed for %s: %s\n", + addrstr, strerror(errno)); } else { - errlog(LOG_DEBUG, "client registered\n"); + errlog(LOG_DEBUG, "client %s registered\n", addrstr); } rc = close(cfd); if (rc) - perror ("close failed"); + perror("close failed"); } closelog(); - pidfile_cleanup(name_port); return (0); } -- 1.8.3.1