X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.cpp;fp=src%2Fcommon%2Futils.cpp;h=6c0fd261c7b8a4033d5eb35cc7247af509ddfca3;hp=b4b7f749f1ef1a6e5286225437f639d01f2966d2;hb=de5abcb02431896a1827dff5d3376e1f2e124cd7;hpb=206e6505316df1b86196d6582ef1e632e47d43a5 diff --git a/src/common/utils.cpp b/src/common/utils.cpp index b4b7f749f..6c0fd261c 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -212,30 +212,40 @@ end: */ int utils_create_pid_file(pid_t pid, const char *filepath) { - int ret; - FILE *fp; + int ret, fd = -1; + FILE *fp = NULL; LTTNG_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; }