static int create_client_sock(void)
{
int ret, client_sock;
- const mode_t old_umask = umask(0);
/* Create client tool unix socket */
client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
DBG("Created client socket (fd = %i)", client_sock);
ret = client_sock;
end:
- umask(old_umask);
return ret;
}
#include "shm.h"
/*
- * Using fork to set umask in the child process (not multi-thread safe). We
- * deal with the shm_open vs ftruncate race (happening when the sessiond owns
+ * We deal with the shm_open vs ftruncate race (happening when the sessiond owns
* the shm and does not let everybody modify it, to ensure safety against
* shm_unlink) by simply letting the mmap fail and retrying after a few
* seconds. For global shm, everybody has rw access to it until the sessiond
static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
{
int wait_shm_fd, ret;
- mode_t mode;
+ mode_t mode, old_mode;
assert(shm_path);
mode |= S_IROTH | S_IWOTH;
}
- /*
- * We're alone in a child process, so we can modify the process-wide
- * umask.
- */
- umask(~mode);
+ old_mode = umask(~mode);
/*
* Try creating shm (or get rw access). We don't do an exclusive open,
ret = ftruncate(wait_shm_fd, mmap_size);
if (ret < 0) {
PERROR("ftruncate wait shm");
- exit(EXIT_FAILURE);
+ goto error;
}
#ifndef __FreeBSD__
ret = fchown(wait_shm_fd, 0, 0);
if (ret < 0) {
PERROR("fchown");
- exit(EXIT_FAILURE);
+ goto error;
}
/*
* If global session daemon, any application can
ret = fchmod(wait_shm_fd, mode);
if (ret < 0) {
PERROR("fchmod");
- exit(EXIT_FAILURE);
+ goto error;
}
} else {
ret = fchown(wait_shm_fd, getuid(), getgid());
if (ret < 0) {
PERROR("fchown");
- exit(EXIT_FAILURE);
+ goto error;
}
}
#else
#endif
DBG("Got the wait shm fd %d", wait_shm_fd);
-
+end:
+ (void) umask(old_mode);
return wait_shm_fd;
error:
DBG("Failing to get the wait shm fd");
+ if (wait_shm_fd >= 0) {
+ if (close(wait_shm_fd)) {
+ PERROR("Failed to close wait shm file descriptor during error handling");
+ }
+ }
- return -1;
+ wait_shm_fd = -1;
+ goto 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;
}