[PATCH 01/11] Add error handling to libevent allocations.

Doug Nazar
2020-07-18T09:25:00Z

utils/gssd/gssd.c | 37 +++++++++++++++++++++++++++----------
utils/idmapd/idmapd.c | 32 ++++++++++++++++++++++++++++++++
utils/nfsdcld/nfsdcld.c | 18 +++++++++++++++++-
3 files changed, 76 insertions(+), 11 deletions(-)

diff --git a/utils/gssd/gssd.c b/utils/gssd/gssd.c
index 3c7c703a..85bc4b07 100644
--- a/utils/gssd/gssd.c
+++ b/utils/gssd/gssd.c
@@ -560,14 +560,9 @@ static int
gssd_scan_clnt(struct clnt_info *clp)
{
int clntfd;
- bool gssd_was_closed;
- bool krb5_was_closed;

printerr(3, "scanning client %s\n", clp->relpath);

- gssd_was_closed = clp->gssd_fd < 0 ? true : false;
- krb5_was_closed = clp->krb5_fd < 0 ? true : false;
-
clntfd = openat(pipefs_fd, clp->relpath, O_RDONLY);
if (clntfd < 0) {
if (errno != ENOENT)
@@ -582,16 +577,30 @@ gssd_scan_clnt(struct clnt_info *clp)
if (clp->gssd_fd == -1 && clp->krb5_fd == -1)
clp->krb5_fd = openat(clntfd, "krb5", O_RDWR | O_NONBLOCK);

- if (gssd_was_closed && clp->gssd_fd >= 0) {
+ if (!clp->gssd_ev && clp->gssd_fd >= 0) {
clp->gssd_ev = event_new(evbase, clp->gssd_fd, EV_READ | EV_PERSIST,
gssd_clnt_gssd_cb, clp);
- event_add(clp->gssd_ev, NULL);
+ if (!clp->gssd_ev) {
+ printerr(0, "ERROR: %s: can't create gssd event for %s: %s\n",
+ __FUNCTION__, clp->relpath, strerror(errno));
+ close(clp->gssd_fd);
+ clp->gssd_fd = -1;
+ } else {
+ event_add(clp->gssd_ev, NULL);
+ }
}

- if (krb5_was_closed && clp->krb5_fd >= 0) {
+ if (!clp->krb5_ev && clp->krb5_fd >= 0) {
clp->krb5_ev = event_new(evbase, clp->krb5_fd, EV_READ | EV_PERSIST,
gssd_clnt_krb5_cb, clp);
- event_add(clp->krb5_ev, NULL);
+ if (!clp->krb5_ev) {
+ printerr(0, "ERROR: %s: can't create krb5 event for %s: %s\n",
+ __FUNCTION__, clp->relpath, strerror(errno));
+ close(clp->krb5_fd);
+ clp->krb5_fd = -1;
+ } else {
+ event_add(clp->krb5_ev, NULL);
+ }
}

if (clp->krb5_fd == -1 && clp->gssd_fd == -1)
@@ -1086,7 +1095,7 @@ main(int argc, char *argv[])

evbase = event_base_new();
if (!evbase) {
- printerr(0, "ERROR: failed to create event base\n");
+ printerr(0, "ERROR: failed to create event base: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}

@@ -1111,9 +1120,17 @@ main(int argc, char *argv[])
signal(SIGINT, sig_die);
signal(SIGTERM, sig_die);
sighup_ev = evsignal_new(evbase, SIGHUP, gssd_scan_cb, NULL);
+ if (!sighup_ev) {
+ printerr(0, "ERROR: failed to create SIGHUP event: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
evsignal_add(sighup_ev, NULL);
inotify_ev = event_new(evbase, inotify_fd, EV_READ | EV_PERSIST,
gssd_inotify_cb, NULL);
+ if (!inotify_ev) {
+ printerr(0, "ERROR: failed to create inotify event: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
event_add(inotify_ev, NULL);

TAILQ_INIT(&topdir_list);
diff --git a/utils/idmapd/idmapd.c b/utils/idmapd/idmapd.c
index 12648f67..491ef54c 100644
--- a/utils/idmapd/idmapd.c
+++ b/utils/idmapd/idmapd.c
@@ -400,13 +400,21 @@ main(int argc, char **argv)

/* These events are persistent */
rootdirev = evsignal_new(evbase, SIGUSR1, dirscancb, &icq);
+ if (rootdirev == NULL)
+ errx(1, "Failed to create SIGUSR1 event.");
evsignal_add(rootdirev, NULL);
clntdirev = evsignal_new(evbase, SIGUSR2, clntscancb, &icq);
+ if (clntdirev == NULL)
+ errx(1, "Failed to create SIGUSR2 event.");
evsignal_add(clntdirev, NULL);
svrdirev = evsignal_new(evbase, SIGHUP, svrreopen, NULL);
+ if (svrdirev == NULL)
+ errx(1, "Failed to create SIGHUP event.");
evsignal_add(svrdirev, NULL);
if ( wd >= 0) {
inotifyev = event_new(evbase, inotify_fd, EV_READ, dirscancb, &icq);
+ if (inotifyev == NULL)
+ errx(1, "Failed to create inotify read event.");
event_add(inotifyev, NULL);
}

@@ -414,6 +422,8 @@ main(int argc, char **argv)
/* (Delay till start of event_dispatch to avoid possibly losing
* a SIGUSR1 between here and the call to event_dispatch().) */
initialize = evtimer_new(evbase, dirscancb, &icq);
+ if (initialize == NULL)
+ errx(1, "Failed to create initialize event.");
evtimer_add(initialize, &now);
}

@@ -768,6 +778,13 @@ nfsdreopen_one(struct idmap_client *ic)

ic->ic_fd = fd;
ic->ic_event = event_new(evbase, ic->ic_fd, EV_READ, nfsdcb, ic);
+ if (ic->ic_event == NULL) {
+ xlog_warn("nfsdreopen: Failed to create event for '%s'",
+ ic->ic_path);
+ close(ic->ic_fd);
+ ic->ic_fd = -1;
+ return;
+ }
event_add(ic->ic_event, NULL);
} else {
xlog_warn("nfsdreopen: Opening '%s' failed: errno %d (%s)",
@@ -802,6 +819,14 @@ nfsdopenone(struct idmap_client *ic)
}

ic->ic_event = event_new(evbase, ic->ic_fd, EV_READ, nfsdcb, ic);
+ if (ic->ic_event == NULL) {
+ if (verbose > 0)
+ xlog_warn("nfsdopenone: Create event for %s failed",
+ ic->ic_path);
+ close(ic->ic_fd);
+ ic->ic_fd = -1;
+ return (-1);
+ }
event_add(ic->ic_event, NULL);

if (verbose > 0)
@@ -826,6 +851,13 @@ nfsopen(struct idmap_client *ic)
}
} else {
ic->ic_event = event_new(evbase, ic->ic_fd, EV_READ, nfscb, ic);
+ if (ic->ic_event == NULL) {
+ xlog_warn("nfsdopenone: Create event for %s failed",
+ ic->ic_path);
+ close(ic->ic_fd);
+ ic->ic_fd = -1;
+ return -1;
+ }
event_add(ic->ic_event, NULL);
fcntl(ic->ic_dirfd, F_NOTIFY, 0);
fcntl(ic->ic_dirfd, F_SETSIG, 0);
diff --git a/utils/nfsdcld/nfsdcld.c b/utils/nfsdcld/nfsdcld.c
index 6cefcf24..5ad94ce2 100644
--- a/utils/nfsdcld/nfsdcld.c
+++ b/utils/nfsdcld/nfsdcld.c
@@ -142,6 +142,7 @@ static int
cld_pipe_open(struct cld_client *clnt)
{
int fd;
+ struct event *ev;

xlog(D_GENERAL, "%s: opening upcall pipe %s", __func__, pipepath);
fd = open(pipepath, O_RDWR, 0);
@@ -150,6 +151,13 @@ cld_pipe_open(struct cld_client *clnt)
return -errno;
}

+ ev = event_new(evbase, fd, EV_READ, cldcb, clnt);
+ if (ev == NULL) {
+ xlog(D_GENERAL, "%s: failed to create event for %s", __func__, pipepath);
+ close(fd);
+ return -ENOMEM;
+ }
+
if (clnt->cl_event && event_initialized(clnt->cl_event)) {
event_del(clnt->cl_event);
event_free(clnt->cl_event);
@@ -158,7 +166,7 @@ cld_pipe_open(struct cld_client *clnt)
close(clnt->cl_fd);

clnt->cl_fd = fd;
- clnt->cl_event = event_new(evbase, clnt->cl_fd, EV_READ, cldcb, clnt);
+ clnt->cl_event = ev;
/* event_add is done by the caller */
return 0;
}
@@ -304,6 +312,10 @@ cld_pipe_init(struct cld_client *clnt)

/* set event for inotify read */
pipedir_event = event_new(evbase, inotify_fd, EV_READ, cld_inotify_cb, clnt);
+ if (pipedir_event == NULL) {
+ close(inotify_fd);
+ return -ENOMEM;
+ }
event_add(pipedir_event, NULL);
out:
return ret;
@@ -768,6 +780,10 @@ main(int argc, char **argv)
}

evbase = event_base_new();
+ if (evbase == NULL) {
+ fprintf(stderr, "%s: unable to allocate event base.\n", argv[0]);
+ return 1;
+ }
xlog_syslog(0);
xlog_stderr(1);

--
2.26.2