X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=7aed4442b5a635d6ba731ad6f2f8fc6224ae7643;hb=a2f8cd58605942314ebf734b3e95c65721e6394a;hp=210a7a6a1e1b6860fba64f2b82a842b5311ff7f2;hpb=4e4e511415375c14912856d0fd6aa3be89d96edc;p=lttng-tools.git diff --git a/src/common/utils.c b/src/common/utils.c index 210a7a6a1..7aed4442b 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -224,30 +224,40 @@ end: LTTNG_HIDDEN int utils_create_pid_file(pid_t pid, const char *filepath) { - int ret; - FILE *fp; + int ret, fd = -1; + FILE *fp = NULL; assert(filepath); - fp = fopen(filepath, "w"); + fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH); + if (fd < 0) { + PERROR("open file %s", filepath); + ret = -1; + goto error; + } + + fp = fdopen(fd, "w"); if (fp == NULL) { - PERROR("open pid file %s", filepath); + PERROR("fdopen file %s", filepath); ret = -1; + close(fd); goto error; } ret = fprintf(fp, "%d\n", (int) pid); if (ret < 0) { - PERROR("fprintf pid file"); + PERROR("fprintf file %s", filepath); + ret = -1; goto error; } - if (fclose(fp)) { - PERROR("fclose"); - } - DBG("Pid %d written in file %s", (int) pid, filepath); + DBG("'%d' written in file %s", (int) pid, filepath); ret = 0; + error: + if (fp && fclose(fp)) { + PERROR("fclose file %s", filepath); + } return ret; }