[master] 6db7d75 Use closefd() throughout, take 2

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Tue Feb 21 14:06:05 CET 2017


commit 6db7d750d1340f7c28d1102d2d24ac1a3f290a38
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Tue Feb 21 13:58:17 2017 +0100

    Use closefd() throughout, take 2
    
    This patch was created using Coccinelle and the following steps:
    
        $ cat >closefd.cocci <<EOF
        @@
        expression fd;
        @@
    
        - AZ(close(fd));
        + closefd(&fd);
        EOF
        $ spatch --dir . --in-place --sp-file closefd.cocci
    
    Please note that one use of AZ(close(...)) couldn't be moved to
    closefd() because of a const qualifier. There are other calls to
    close(2) that are currently not checked on purpose. They may need
    to be revisited.
    
    See: git grep -F '(void)close'

diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index 421e3c5..2bd49ba 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -114,18 +114,18 @@ cli_sock(const char *T_arg, const char *S_arg)
 	if (status == CLIS_AUTH) {
 		if (S_arg == NULL) {
 			fprintf(stderr, "Authentication required\n");
-			AZ(close(sock));
+			closefd(&sock);
 			return(-1);
 		}
 		fd = open(S_arg, O_RDONLY);
 		if (fd < 0) {
 			fprintf(stderr, "Cannot open \"%s\": %s\n",
 			    S_arg, strerror(errno));
-			AZ(close(sock));
+			closefd(&sock);
 			return (-1);
 		}
 		VCLI_AuthResponse(fd, answer, buf);
-		AZ(close(fd));
+		closefd(&fd);
 		free(answer);
 
 		cli_write(sock, "auth ");
@@ -135,7 +135,7 @@ cli_sock(const char *T_arg, const char *S_arg)
 	}
 	if (status != CLIS_OK) {
 		fprintf(stderr, "Rejected %u\n%s\n", status, answer);
-		AZ(close(sock));
+		closefd(&sock);
 		free(answer);
 		return (-1);
 	}
@@ -145,7 +145,7 @@ cli_sock(const char *T_arg, const char *S_arg)
 	(void)VCLI_ReadResult(sock, &status, &answer, timeout);
 	if (status != CLIS_OK || strstr(answer, "PONG") == NULL) {
 		fprintf(stderr, "No pong received from server\n");
-		AZ(close(sock));
+		closefd(&sock);
 		free(answer);
 		return (-1);
 	}
diff --git a/bin/varnishtest/vtc_barrier.c b/bin/varnishtest/vtc_barrier.c
index ddbf828..bafcb79 100644
--- a/bin/varnishtest/vtc_barrier.c
+++ b/bin/varnishtest/vtc_barrier.c
@@ -178,7 +178,7 @@ barrier_sock_thread(void *priv)
 		if (i < 0) {
 			if (errno == EINTR)
 				continue;
-			AZ(close(sock));
+			closefd(&sock);
 			vtc_fatal(vl,
 			    "Barrier(%s) select fails: %s (errno=%d)",
 			    b->name, strerror(errno), errno);
@@ -193,7 +193,7 @@ barrier_sock_thread(void *priv)
 
 		i = accept(sock, NULL, NULL);
 		if (i < 0) {
-			AZ(close(sock));
+			closefd(&sock);
 			vtc_fatal(vl,
 			    "Barrier(%s) accept fails: %s (errno=%d)",
 			    b->name, strerror(errno), errno);
@@ -212,7 +212,7 @@ barrier_sock_thread(void *priv)
 
 		vtc_log(vl, 4, "Barrier(%s) wake %u", b->name, b->expected);
 		for (i = 0; i < b->expected; i++)
-			AZ(close(conns[i]));
+			closefd(&conns[i]);
 
 		if (b->cyclic)
 			b->waiters = 0;
@@ -223,7 +223,7 @@ barrier_sock_thread(void *priv)
 	macro_undef(vl, b->name, "addr");
 	macro_undef(vl, b->name, "port");
 	macro_undef(vl, b->name, "sock");
-	AZ(close(sock));
+	closefd(&sock);
 	free(conns);
 
 	return (NULL);
@@ -324,7 +324,7 @@ barrier_sock_sync(struct barrier *b, struct vtclog *vl)
 	AZ(pthread_mutex_lock(&b->mtx));
 
 	i = errno;
-	AZ(close(sock));
+	closefd(&sock);
 
 	if (sz < 0)
 		vtc_fatal(vl, "Barrier(%s) read failed: %s (errno=%d)",
diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index c18f355..f4ed03e 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -45,6 +45,7 @@
 
 #include "vtc.h"
 
+#include "vdef.h"
 #include "vev.h"
 #include "vfil.h"
 #include "vnum.h"
@@ -312,7 +313,7 @@ start_test(void)
 		    jp->tmpdir, jp->buf, jp->bufsiz);
 		exit(retval);
 	}
-	AZ(close(p[1]));
+	closefd(&p[1]);
 
 	jp->ev = vev_new();
 	AN(jp->ev);
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index e0dce91..54ec8b8 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -445,10 +445,10 @@ varnish_launch(struct varnish *v)
 		AZ(dup2(v->fds[0], 0));
 		assert(dup2(v->fds[3], 1) == 1);
 		assert(dup2(1, 2) == 2);
-		AZ(close(v->fds[0]));
-		AZ(close(v->fds[1]));
-		AZ(close(v->fds[2]));
-		AZ(close(v->fds[3]));
+		closefd(&v->fds[0]);
+		closefd(&v->fds[1]);
+		closefd(&v->fds[2]);
+		closefd(&v->fds[3]);
 		VSUB_closefrom(STDERR_FILENO + 1);
 		AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0));
 		exit(1);
@@ -457,8 +457,8 @@ varnish_launch(struct varnish *v)
 		macro_def(v->vl, v->name, "pid", "%ld", (long)v->pid);
 		macro_def(v->vl, v->name, "name", "%s", v->workdir);
 	}
-	AZ(close(v->fds[0]));
-	AZ(close(v->fds[3]));
+	closefd(&v->fds[0]);
+	closefd(&v->fds[3]);
 	v->fds[0] = v->fds[2];
 	v->fds[2] = v->fds[3] = -1;
 	VSB_destroy(&vsb);
@@ -482,12 +482,12 @@ varnish_launch(struct varnish *v)
 		vtc_fatal(v->vl, "FAIL CLI connection wait failure");
 	nfd = accept(v->cli_fd, NULL, NULL);
 	if (nfd < 0) {
-		AZ(close(v->cli_fd));
+		closefd(&v->cli_fd);
 		v->cli_fd = -1;
 		vtc_fatal(v->vl, "FAIL no CLI connection accepted");
 	}
 
-	AZ(close(v->cli_fd));
+	closefd(&v->cli_fd);
 	v->cli_fd = nfd;
 
 	vtc_log(v->vl, 3, "CLI connection fd = %d", v->cli_fd);
@@ -507,7 +507,7 @@ varnish_launch(struct varnish *v)
 	assert(sizeof abuf >= CLI_AUTH_RESPONSE_LEN + 7);
 	strcpy(abuf, "auth ");
 	VCLI_AuthResponse(nfd, r, abuf + 5);
-	AZ(close(nfd));
+	closefd(&nfd);
 	free(r);
 	r = NULL;
 	strcat(abuf, "\n");
@@ -619,15 +619,15 @@ varnish_cleanup(struct varnish *v)
 		(void)usleep(200000);
 
 	/* Close the CLI connection */
-	AZ(close(v->cli_fd));
+	closefd(&v->cli_fd);
 	v->cli_fd = -1;
 
 	/* Close the STDIN connection. */
-	AZ(close(v->fds[1]));
+	closefd(&v->fds[1]);
 
 	/* Wait until STDOUT+STDERR closes */
 	AZ(pthread_join(v->tp, &p));
-	AZ(close(v->fds[0]));
+	closefd(&v->fds[0]);
 
 	r = wait4(v->pid, &status, 0, &ru);
 	v->pid = 0;
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index ac39aff..55da29f 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -71,7 +71,7 @@ VFIL_null_fd(int target)
 	fd = open("/dev/null", O_RDWR);
 	assert(fd >= 0);
 	assert(dup2(fd, target) == target);
-	AZ(close(fd));
+	closefd(&fd);
 }
 
 static char *
@@ -143,7 +143,7 @@ VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz)
 		return (NULL);
 	r = vfil_readfd(fd, sz);
 	err = errno;
-	AZ(close(fd));
+	closefd(&fd);
 	errno = err;
 	return (r);
 }
@@ -159,7 +159,7 @@ VFIL_writefile(const char *pfx, const char *fn, const char *buf, size_t sz)
 		return (fd);
 	r = vfil_writefd(fd, buf, sz);
 	err = errno;
-	AZ(close(fd));
+	closefd(&fd);
 	errno = err;
 	return (r);
 }
diff --git a/lib/libvarnish/vrnd.c b/lib/libvarnish/vrnd.c
index d586b08..e38f0a7 100644
--- a/lib/libvarnish/vrnd.c
+++ b/lib/libvarnish/vrnd.c
@@ -41,6 +41,7 @@
 #include <unistd.h>
 
 #include "vas.h"
+#include "vdef.h"
 #include "vrnd.h"
 
 /**********************************************************************
@@ -158,7 +159,7 @@ VRND_RandomCrypto(void *ptr, size_t len)
 		if (l != 1)
 			break;
 	}
-	AZ(close(fd));
+	closefd(&fd);
 	return (len == 0 ? 0 : -1);
 }
 
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
index 57b40b8..ea86699 100644
--- a/lib/libvarnish/vsub.c
+++ b/lib/libvarnish/vsub.c
@@ -109,8 +109,8 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
 	if ((pid = fork()) < 0) {
 		VSB_printf(sb, "Starting %s: fork() failed: %s",
 		    name, strerror(errno));
-		AZ(close(p[0]));
-		AZ(close(p[1]));
+		closefd(&p[0]);
+		closefd(&p[1]);
 		return (1);
 	}
 	if (pid == 0) {
@@ -127,11 +127,11 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
 		 */
 		_exit(4);
 	}
-	AZ(close(p[1]));
+	closefd(&p[1]);
 	vlu = VLU_New(&sp, vsub_vlu, 0);
 	while (!VLU_Fd(p[0], vlu))
 		continue;
-	AZ(close(p[0]));
+	closefd(&p[0]);
 	VLU_Destroy(vlu);
 	if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
 		VSB_printf(sb, "[%d lines truncated]\n",
diff --git a/lib/libvarnish/vtcp.c b/lib/libvarnish/vtcp.c
index d28b99c..67be8a0 100644
--- a/lib/libvarnish/vtcp.c
+++ b/lib/libvarnish/vtcp.c
@@ -262,7 +262,7 @@ VTCP_connected(int s)
 	/* An error means no connection established */
 	errno = k;
 	if (k) {
-		AZ(close(s));
+		closefd(&s);
 		return (-1);
 	}
 
@@ -302,7 +302,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
 	if (i == 0)
 		return (s);
 	if (errno != EINPROGRESS) {
-		AZ(close(s));
+		closefd(&s);
 		return (-1);
 	}
 
@@ -323,7 +323,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
 
 	if (i == 0) {
 		/* Timeout, close and give up */
-		AZ(close(s));
+		closefd(&s);
 		errno = ETIMEDOUT;
 		return (-1);
 	}
@@ -430,7 +430,7 @@ VTCP_bind(const struct suckaddr *sa, const char **errp)
 		if (errp != NULL)
 			*errp = "setsockopt(SO_REUSEADDR, 1)";
 		e = errno;
-		AZ(close(sd));
+		closefd(&sd);
 		errno = e;
 		return (-1);
 	}
@@ -442,7 +442,7 @@ VTCP_bind(const struct suckaddr *sa, const char **errp)
 		if (errp != NULL)
 			*errp = "setsockopt(IPV6_V6ONLY, 1)";
 		e = errno;
-		AZ(close(sd));
+		closefd(&sd);
 		errno = e;
 		return (-1);
 	}
@@ -452,7 +452,7 @@ VTCP_bind(const struct suckaddr *sa, const char **errp)
 		if (errp != NULL)
 			*errp = "bind(2)";
 		e = errno;
-		AZ(close(sd));
+		closefd(&sd);
 		errno = e;
 		return (-1);
 	}
@@ -476,7 +476,7 @@ VTCP_listen(const struct suckaddr *sa, int depth, const char **errp)
 	if (sd >= 0)  {
 		if (listen(sd, depth) != 0) {
 			e = errno;
-			AZ(close(sd));
+			closefd(&sd);
 			errno = e;
 			if (errp != NULL)
 				*errp = "listen(2)";
diff --git a/lib/libvarnishapi/daemon.c b/lib/libvarnishapi/daemon.c
index 93e3bef..2ac2697 100644
--- a/lib/libvarnishapi/daemon.c
+++ b/lib/libvarnishapi/daemon.c
@@ -55,7 +55,7 @@ vfil_null_fd(int target)
 	fd = open("/dev/null", O_RDWR);
 	assert(fd >= 0);
 	assert(dup2(fd, target) == target);
-	AZ(close(fd));
+	closefd(&fd);
 }
 
 int
diff --git a/lib/libvarnishapi/vsm.c b/lib/libvarnishapi/vsm.c
index f3fd9f4..250bdab 100644
--- a/lib/libvarnishapi/vsm.c
+++ b/lib/libvarnishapi/vsm.c
@@ -239,7 +239,7 @@ VSM_Open(struct VSM_data *vd)
 
 	AZ(fstat(vd->vsm_fd, &vd->fstat));
 	if (!S_ISREG(vd->fstat.st_mode)) {
-		AZ(close(vd->vsm_fd));
+		closefd(&vd->vsm_fd);
 		vd->vsm_fd = -1;
 		return (vsm_diag(vd, "%s is not a regular file",
 		    vd->fname));
@@ -247,20 +247,20 @@ VSM_Open(struct VSM_data *vd)
 
 	i = read(vd->vsm_fd, &slh, sizeof slh);
 	if (i != sizeof slh) {
-		AZ(close(vd->vsm_fd));
+		closefd(&vd->vsm_fd);
 		vd->vsm_fd = -1;
 		return (vsm_diag(vd, "Cannot read %s: %s",
 		    vd->fname, strerror(errno)));
 	}
 
 	if (memcmp(slh.marker, VSM_HEAD_MARKER, sizeof slh.marker)) {
-		AZ(close(vd->vsm_fd));
+		closefd(&vd->vsm_fd);
 		vd->vsm_fd = -1;
 		return (vsm_diag(vd, "Not a VSM file %s", vd->fname));
 	}
 
 	if (!vd->N_opt && slh.alloc_seq == 0) {
-		AZ(close(vd->vsm_fd));
+		closefd(&vd->vsm_fd);
 		vd->vsm_fd = -1;
 		return (vsm_diag(vd,
 		    "Abandoned VSM file (Varnish not running?) %s",
@@ -270,7 +270,7 @@ VSM_Open(struct VSM_data *vd)
 	v = mmap(NULL, slh.shm_size,
 	    PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsm_fd, 0);
 	if (v == MAP_FAILED) {
-		AZ(close(vd->vsm_fd));
+		closefd(&vd->vsm_fd);
 		vd->vsm_fd = -1;
 		return (vsm_diag(vd, "Cannot mmap %s: %s",
 		    vd->fname, strerror(errno)));
@@ -310,7 +310,7 @@ VSM_Close(struct VSM_data *vd)
 	vd->b = NULL;
 	vd->e = NULL;
 	vd->head = NULL;
-	AZ(close(vd->vsm_fd));
+	closefd(&vd->vsm_fd);
 	vd->vsm_fd = -1;
 }
 
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 76519f7..fe07219 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -524,7 +524,7 @@ vmod_barrier_sync(VRT_CTX, VCL_STRING addr)
 
 	sz = read(sock, buf, sizeof buf);
 	i = errno;
-	AZ(close(sock));
+	closefd(&sock);
 	if (sz == 0)
 		return (1);
 	if (sz < 0)



More information about the varnish-commit mailing list