OSDN Git Service

Some lib fixes: mark xvfork() noinline, make xsendfile() return bytes copied,
authorRob Landley <rob@landley.net>
Wed, 4 Jan 2017 07:32:44 +0000 (01:32 -0600)
committerRob Landley <rob@landley.net>
Wed, 4 Jan 2017 07:32:44 +0000 (01:32 -0600)
make xsocket()'s returned fd CLOEXEC.

lib/lib.h
lib/net.c
lib/xwrap.c

index d5a4832..2cb0c07 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -211,7 +211,7 @@ off_t fdlength(int fd);
 void loopfiles_rw(char **argv, int flags, int permissions,
   void (*function)(int fd, char *name));
 void loopfiles(char **argv, void (*function)(int fd, char *name));
-void xsendfile(int in, int out);
+long long xsendfile(int in, int out);
 int wfchmodat(int rc, char *name, mode_t mode);
 int copy_tempfile(int fdin, char *name, char **tempname);
 void delete_tempfile(int fdin, int fdout, char **tempname);
index 1a46a5a..df2551f 100644 (file)
--- a/lib/net.c
+++ b/lib/net.c
@@ -5,6 +5,8 @@ int xsocket(int domain, int type, int protocol)
   int fd = socket(domain, type, protocol);
 
   if (fd < 0) perror_exit("socket %x %x", type, protocol);
+  fcntl(fd, F_SETFD, FD_CLOEXEC);
+
   return fd;
 }
 
@@ -35,7 +37,7 @@ int xconnect(char *host, char *port, int family, int socktype, int protocol,
     fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype,
       ai->ai_protocol);
     if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break;
-    else if (!ai2->ai_next) perror_exit("connect");
+    else if (!ai->ai_next) perror_exit("connect");
     close(fd);
   }
   freeaddrinfo(ai2);
index 66972f2..611bdb5 100644 (file)
@@ -163,7 +163,7 @@ void xflush(void)
 // share a stack, so child returning from a function would stomp the return
 // address parent would need. Solution: make vfork() an argument so processes
 // diverge before function gets called.
-pid_t xvforkwrap(pid_t pid)
+pid_t __attribute__((noinline)) xvforkwrap(pid_t pid)
 {
   if (pid == -1) perror_exit("vfork");
 
@@ -732,16 +732,20 @@ void xpidfile(char *name)
 
 // Copy the rest of in to out and close both files.
 
-void xsendfile(int in, int out)
+long long xsendfile(int in, int out)
 {
+  long long total = 0;
   long len;
 
-  if (in<0) return;
+  if (in<0) return 0;
   for (;;) {
     len = xread(in, libbuf, sizeof(libbuf));
     if (len<1) break;
     xwrite(out, libbuf, len);
+    total += len;
   }
+
+  return total;
 }
 
 // parse fractional seconds with optional s/m/h/d suffix