Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

container: truncate pid file before writing it #505

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, O_CREAT, data, len, err);
}

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