Skip to content

Commit

Permalink
Clean up die() vs. fatal()
Browse files Browse the repository at this point in the history
For user-facing error messages, use die() instead fatal().  The latter
prints source-code level information that is more appropriate for
internal and "can't happen" errors.
  • Loading branch information
petere committed Dec 12, 2019
1 parent 527924f commit 2c5ea10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions usual/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool signal_pidfile(const char *pidfile, int sig)
static void check_pidfile(const char *pidfile)
{
if (signal_pidfile(pidfile, 0))
fatal("pidfile exists, another instance running?");
die("pidfile exists, another instance running?");
if (errno == ESRCH) {
log_info("stale pidfile, removing");
unlink(pidfile);
Expand All @@ -123,7 +123,7 @@ static void write_pidfile(const char *pidfile, bool first_write)
free(g_pidfile);
g_pidfile = strdup(pidfile);
if (!g_pidfile)
fatal_perror("cannot alloc pidfile");
die("out of memory");

pid = getpid();
snprintf(buf, sizeof(buf), "%u\n", (unsigned)pid);
Expand All @@ -134,14 +134,14 @@ static void write_pidfile(const char *pidfile, bool first_write)

fd = open(pidfile, flags, 0644);
if (fd < 0)
fatal_perror("cannot write pidfile: '%s'", pidfile);
die("could not open pidfile '%s': %s", pidfile, strerror(errno));
len = strlen(buf);
loop:
res = write(fd, buf, len);
if (res < 0) {
if (errno == EINTR)
goto loop;
fatal_perror("write to pidfile failed: '%s'", pidfile);
die("write to pidfile '%s' failed: %s", pidfile, strerror(errno));
} else if (res < len) {
len -= res;
goto loop;
Expand Down Expand Up @@ -185,7 +185,7 @@ void daemonize(const char *pidfile, bool go_background)
/* send stdin, stdout, stderr to /dev/null */
fd = open("/dev/null", O_RDWR);
if (fd < 0)
fatal_perror("/dev/null");
die("could not open /dev/null: %s", strerror(errno));
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
Expand All @@ -195,19 +195,19 @@ void daemonize(const char *pidfile, bool go_background)
/* fork new process */
pid = fork();
if (pid < 0)
fatal_perror("fork");
die("fork failed: %s", strerror(errno));
if (pid > 0)
_exit(0);

/* create new session */
pid = setsid();
if (pid < 0)
fatal_perror("setsid");
die("setsid: %s", strerror(errno));

/* fork again to avoid being session leader */
pid = fork();
if (pid < 0)
fatal_perror("fork");
die("fork failed; %s", strerror(errno));
if (pid > 0)
_exit(0);

Expand Down
4 changes: 2 additions & 2 deletions usual/pgsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static void wait_event(struct PgSocket *db, short ev, libev_cb fn)
if (db->base)
event_base_set(db->base, &db->ev);
if (event_add(&db->ev, NULL) < 0)
fatal_perror("event_add");
die("event_add failed: %s", strerror(errno));

db->wait_type = W_SOCK;
db->wait_event = ev;
Expand Down Expand Up @@ -328,7 +328,7 @@ void pgs_sleep(struct PgSocket *db, double timeout)
if (db->base)
event_base_set(db->base, &db->ev);
if (evtimer_add(&db->ev, &tv) < 0)
fatal_perror("event_add");
die("evtimer_add failed: %s", strerror(errno));

db->wait_type = W_TIME;
}
Expand Down

0 comments on commit 2c5ea10

Please sign in to comment.