X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Futils.cpp;h=89ef0e54fd01714ea866c05dd2424cd1f21c26e5;hb=9a30ba1da4e6d59eb30c6535a9dd04cd599e6fae;hp=b4b7f749f1ef1a6e5286225437f639d01f2966d2;hpb=64803277bbdbe0a943360d918298a48157d9da55;p=lttng-tools.git diff --git a/src/common/utils.cpp b/src/common/utils.cpp index b4b7f749f..89ef0e54f 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -212,30 +212,43 @@ 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; + if (close(fd)) { + PERROR("Failed to close `%s` file descriptor while handling fdopen error", filepath); + } + 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; }