Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 96eba45

Browse files
committed
check the result of killing a container
the kill(2) may have some failure condition: ``` RETURN VALUE On success (at least one signal was sent), zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS EINVAL An invalid signal was specified. EPERM The process does not have permission to send the signal to any of the target processes. ESRCH The process or process group does not exist. Note that an existing process might be a zombie, a process that has terminated execution, but has not yet been wait(2)ed for. ``` Signed-off-by: Wang Xu <[email protected]>
1 parent b6197ed commit 96eba45

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/init.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,12 @@ static int hyper_new_container(struct hyper_pod *pod, char *json, int length)
640640
return ret;
641641
}
642642

643-
static int hyper_kill_container(struct hyper_pod *pod, char *json, int length)
643+
static int hyper_kill_container(struct hyper_pod *pod, char *json, int length, uint8_t **rmsg)
644644
{
645645
struct hyper_container *c;
646646
int ret = -1;
647+
size_t message_len = 0;
648+
const char *emsg = NULL;
647649

648650
JSON_Value *value = hyper_json_parse(json, length);
649651
if (value == NULL) {
@@ -654,13 +656,36 @@ static int hyper_kill_container(struct hyper_pod *pod, char *json, int length)
654656
c = hyper_find_container(pod, id);
655657
if (c == NULL) {
656658
fprintf(stderr, "can not find container whose id is %s\n", id);
659+
emsg = "no such container";
657660
goto out;
658661
}
659662

660-
kill(c->exec.pid, (int)json_object_get_number(json_object(value), "signal"));
661-
ret = 0;
663+
ret = kill(c->exec.pid, (int)json_object_get_number(json_object(value), "signal"));
664+
if (ret <0) {
665+
switch(errno) {
666+
case EINVAL:
667+
emsg = "invalid signal";
668+
break;
669+
case EPERM:
670+
emsg = "no permission";
671+
break;
672+
case ESRCH:
673+
emsg = "no such process";
674+
break;
675+
default:
676+
emsg = "kill failed";
677+
break;
678+
}
679+
}
662680
out:
663681
json_value_free(value);
682+
if (emsg != NULL) {
683+
message_len = strlen(emsg) + 1;
684+
*rmsg = (uint8_t*)malloc(message_len);
685+
if (*rmsg != NULL) {
686+
strncpy((char*)*rmsg, emsg, message_len);
687+
}
688+
}
664689
return ret;
665690
}
666691

@@ -1209,7 +1234,10 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
12091234
ret = hyper_new_container(pod, (char *)buf->data + 8, len - 8);
12101235
break;
12111236
case KILLCONTAINER:
1212-
ret = hyper_kill_container(pod, (char *)buf->data + 8, len - 8);
1237+
ret = hyper_kill_container(pod, (char *)buf->data + 8, len - 8, &data);
1238+
if (data != NULL) {
1239+
datalen = strlen((char*)data) + 1;
1240+
}
12131241
break;
12141242
case REMOVECONTAINER:
12151243
ret = hyper_remove_container(pod, (char *)buf->data + 8, len - 8);

0 commit comments

Comments
 (0)