Skip to content

Commit

Permalink
Merge pull request #423 from giuseppe/dont-fail-delete-on-exited-cont…
Browse files Browse the repository at this point in the history
…ainer

status: do not fail delete if the process exited
  • Loading branch information
rhatdan authored Jul 2, 2020
2 parents 65e35c5 + c06821d commit fe644ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/libcrun/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,6 @@ libcrun_cgroup_pause_unpause_with_mode (const char *cgroup_path, int cgroup_mode
if (ret >= 0)
return 0;
return ret;

}

int
Expand Down Expand Up @@ -1636,7 +1635,7 @@ libcrun_cgroup_killall_signal (const char *path, int signal, libcrun_error_t *er
for (i = 0; pids && pids[i]; i++)
{
ret = kill (pids[i], signal);
if (UNLIKELY (ret < 0))
if (UNLIKELY (ret < 0 && errno != ESRCH))
return crun_make_error (err, errno, "kill process %d", pids[i]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcrun/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ libcrun_container_kill (libcrun_context_t *context, const char *id, int signal,
return ret;

ret = kill (status.pid, signal);
if (UNLIKELY (ret < 0))
if (UNLIKELY (ret < 0 && errno != ESRCH))
return crun_make_error (err, errno, "kill container");
return 0;
}
Expand Down
29 changes: 23 additions & 6 deletions src/libcrun/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,34 @@ get_state_directory_status_file (const char *state_root, const char *id)
static int
read_pid_stat (pid_t pid, struct pid_stat *st, libcrun_error_t *err)
{
int ret;
cleanup_file FILE *f = NULL;
cleanup_free char *pid_stat_file = NULL;
cleanup_free char *buffer = NULL;
cleanup_close int fd = -1;
int ret;

xasprintf (&pid_stat_file, "/proc/%d/stat", pid);

f = fopen (pid_stat_file, "r");
if (f == NULL)
return crun_make_error (err, errno, "open state file %s", pid_stat_file);
fd = open (pid_stat_file, O_RDONLY);
if (fd < 0)
{
/* The process already exited. */
if (errno == ENOENT)
{
memset (st, 0, sizeof (*st));
return 0;
}
return crun_make_error (err, errno, "open state file %s", pid_stat_file);
}

ret = read_all_fd (fd, pid_stat_file, &buffer, NULL, err);
if (ret < 0)
{
/* The process already exited. */
libcrun_error_release (err);
return 0;
}

ret = fscanf (f,"%d %255s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu",
ret = sscanf (buffer, "%d %255s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %llu",
&(st->pid), st->comm, &(st->state), &(st->ppid), &(st->pgrp), &(st->session),
&(st->tty_nr), &(st->tpgid), &(st->flags), &(st->minflt), &(st->cminflt),
&(st->majflt), &(st->cmajflt), &(st->utime), &(st->stime), &(st->cutime),
Expand Down

0 comments on commit fe644ca

Please sign in to comment.