Skip to content

Commit

Permalink
container: truncate pid file before writing it
Browse files Browse the repository at this point in the history
Closes: #504

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 24, 2020
1 parent 9e1c3de commit 51129eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/libcrun/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ wait_for_process (pid_t pid, libcrun_context_t *context, int terminal_fd, int no
{
char buf[12];
size_t buf_len = sprintf (buf, "%d", pid);
ret = write_file (context->pid_file, buf, buf_len, err);
ret = write_file_with_flags (context->pid_file, O_CREAT | O_TRUNC, buf, buf_len, err);
if (UNLIKELY (ret < 0))
return ret;
}
Expand Down Expand Up @@ -2864,7 +2864,7 @@ libcrun_container_restore (libcrun_context_t *context, const char *id, libcrun_c
{
char buf[12];
size_t buf_len = sprintf (buf, "%d", status.pid);
ret = write_file (context->pid_file, buf, buf_len, err);
ret = write_file_with_flags (context->pid_file, O_CREAT | O_TRUNC, buf, buf_len, err);
if (UNLIKELY (ret < 0))
return ret;
}
Expand Down
10 changes: 8 additions & 2 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ write_file_at (int dirfd, const char *name, const void *data, size_t len, libcru
}

int
write_file (const char *name, const void *data, size_t len, libcrun_error_t *err)
write_file_with_flags (const char *name, int flags, const void *data, size_t len, libcrun_error_t *err)
{
cleanup_close int fd = open (name, O_WRONLY | O_CREAT, 0700);
cleanup_close int fd = open (name, O_WRONLY | flags, 0700);
int ret;
if (UNLIKELY (fd < 0))
return crun_make_error (err, errno, "opening file `%s` for writing", name);
Expand All @@ -125,6 +125,12 @@ write_file (const char *name, const void *data, size_t len, libcrun_error_t *err
return ret;
}

int
write_file (const char *name, const void *data, size_t len, libcrun_error_t *err)
{
return write_file_with_flags (name, data, len, O_CREAT, 0700);
}

int
detach_process ()
{
Expand Down
2 changes: 2 additions & 0 deletions src/libcrun/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ int xasprintf (char **str, const char *fmt, ...);

int crun_path_exists (const char *path, libcrun_error_t *err);

int write_file_with_flags (const char *name, int flags, const void *data, size_t len, libcrun_error_t *err);

int write_file (const char *name, const void *data, size_t len, libcrun_error_t *err);

int write_file_at (int dirfd, const char *name, const void *data, size_t len, libcrun_error_t *err);
Expand Down

0 comments on commit 51129eb

Please sign in to comment.