[Freeipa-devel] [PATCH] detect failure to write ipa_kpasswd.pid file
Jim Meyering
jim at meyering.net
Thu May 15 11:29:56 UTC 2008
Jim Meyering <jim at meyering.net> wrote:
> Making it more readable while retaining correctness is tricky.
> The catch lies in always closing F, even when fprintf fails.
> The following is shorter and no less correct -- maybe even more
> readable. At least it doesn't set fail=1 three times:
> [note that technically, we should save errno from
> a failed fprintf, so it can't be clobbered by fclose,
> but in practice it probably doesn't matter, since that
> use of fprintf won't ever fail. ]
>
> const char *pid_file = "/var/run/ipa_kpasswd.pid";
> int fail = 1;
> FILE *f = fopen(pid_file, "w");
> if (f) {
> int n_bytes = fprintf(f, "%ld\n", (long) getpid());
> if (fclose(f) == 0 && 0 < n_bytes)
> fail = 0;
> }
> if (fail) {
> syslog(LOG_ERR,"Couldn't create pid file %s: %s",
> pid_file, strerror(errno));
> exit(1);
> }
In case you like that, here's the patch:
>From e9c342f7670c8120695e06351d3e895c2c907910 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Sun, 4 May 2008 15:17:36 +0200
Subject: [PATCH] detect failure to write ipa_kpasswd.pid file
* ipa_kpasswd.c (main): Detect not just open failure,
but also any write failure.
---
ipa-server/ipa-kpasswd/ipa_kpasswd.c | 19 ++++++++++++-------
1 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/ipa-server/ipa-kpasswd/ipa_kpasswd.c b/ipa-server/ipa-kpasswd/ipa_kpasswd.c
index 5782367..2b82f18 100644
--- a/ipa-server/ipa-kpasswd/ipa_kpasswd.c
+++ b/ipa-server/ipa-kpasswd/ipa_kpasswd.c
@@ -3,7 +3,7 @@
/* Authors: Simo Sorce <ssorce at redhat.com>
*
- * Copyright (C) 2007 Red Hat
+ * Copyright (C) 2007, 2008 Red Hat
* see file 'COPYING' for use and warranty information
*
* This program is free software; you can redistribute it and/or
@@ -1188,13 +1188,18 @@ int main(int argc, char *argv[])
}
/* Write out the pid file after the sigterm handler */
- FILE *f = fopen("/var/run/ipa_kpasswd.pid", "w");
- if (f == NULL) {
- syslog(LOG_ERR,"Couldn't create pid file /var/run/ipa_kpasswd.pid: %s", strerror(errno));
+ const char *pid_file = "/var/run/ipa_kpasswd.pid";
+ FILE *f = fopen(pid_file, "w");
+ int fail = 1;
+ if (f) {
+ int n_bytes = fprintf(f, "%ld\n", (long) getpid());
+ if (fclose(f) == 0 && 0 < n_bytes)
+ fail = 0;
+ }
+ if (fail) {
+ syslog(LOG_ERR,"Couldn't create pid file %s: %s",
+ pid_file, strerror(errno));
exit(1);
- } else {
- fprintf(f, "%ld\n", (long) getpid());
- fclose(f);
}
tai = ai;
--
1.5.5.1.216.g33c73
More information about the Freeipa-devel
mailing list