Improve logic and logging in server_pid()

This commit is contained in:
Andrew Bettison 2013-10-07 04:36:22 +10:30
parent 55edc74482
commit 5d7ea6e6f5

View File

@ -49,20 +49,18 @@ int server_pid()
{ {
const char *instancepath = serval_instancepath(); const char *instancepath = serval_instancepath();
struct stat st; struct stat st;
if (stat(instancepath, &st) == -1) { if (stat(instancepath, &st) == -1)
WHY_perror("stat"); return WHYF_perror("stat(%s)", alloca_str_toprint(instancepath));
return WHYF("Instance path '%s' non existant or not accessable"
" (Set SERVALINSTANCE_PATH to specify an alternate location)",
instancepath
);
}
if ((st.st_mode & S_IFMT) != S_IFDIR) if ((st.st_mode & S_IFMT) != S_IFDIR)
return WHYF("Instance path '%s' is not a directory", instancepath); return WHYF("Instance path '%s' is not a directory", instancepath);
char filename[1024]; char filename[1024];
if (!FORM_SERVAL_INSTANCE_PATH(filename, PIDFILE_NAME)) if (!FORM_SERVAL_INSTANCE_PATH(filename, PIDFILE_NAME))
return -1; return -1;
FILE *f = NULL; FILE *f = fopen(filename, "r");
if ((f = fopen(filename, "r"))) { if (f == NULL) {
if (errno != ENOENT)
return WHYF_perror("fopen(%s,\"r\")", alloca_str_toprint(filename));
} else {
char buf[20]; char buf[20];
int pid = (fgets(buf, sizeof buf, f) != NULL) ? atoi(buf) : -1; int pid = (fgets(buf, sizeof buf, f) != NULL) ? atoi(buf) : -1;
fclose(f); fclose(f);