Fix: agent port file is o+w when launching as root
[lttng-tools.git] / src / common / utils.c
index febd75c185ddd87c177f388dfb7ba464e44cbf5c..682084c0f67d0cf32eb39bb2c9e76a5975a0f559 100644 (file)
@@ -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;
 }
 
This page took 0.027959 seconds and 4 git commands to generate.