]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/stop.c
Makefile.am: use right .h file name for seccomp
[mirror_lxc.git] / src / lxc / stop.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <sys/signal.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <fcntl.h>
34
35 #include <lxc/log.h>
36 #include <lxc/start.h>
37
38 #include "lxc.h"
39 #include "commands.h"
40
41 lxc_log_define(lxc_stop, lxc);
42
43 int lxc_stop(const char *name)
44 {
45 struct lxc_command command = {
46 .request = { .type = LXC_COMMAND_STOP },
47 };
48
49 int ret, stopped = 0;
50
51 ret = lxc_command(name, &command,&stopped);
52 if (ret < 0 && stopped) {
53 INFO("'%s' is already stopped", name);
54 return 0;
55 }
56
57 if (ret < 0) {
58 ERROR("failed to send command");
59 return -1;
60 }
61
62 /* we do not expect any answer, because we wait for the connection to be
63 * closed
64 */
65 if (ret > 0) {
66 ERROR("failed to stop '%s': %s",
67 name, strerror(-command.answer.ret));
68 return -1;
69 }
70
71 INFO("'%s' has stopped", name);
72
73 return 0;
74 }
75
76 /*----------------------------------------------------------------------------
77 * functions used by lxc-start mainloop
78 * to handle above command request.
79 *--------------------------------------------------------------------------*/
80 extern int lxc_stop_callback(int fd, struct lxc_request *request,
81 struct lxc_handler *handler)
82 {
83 struct lxc_answer answer;
84 int ret;
85
86 answer.ret = kill(handler->pid, SIGKILL);
87 if (!answer.ret) {
88 ret = lxc_unfreeze(handler->name);
89 if (!ret)
90 return 0;
91
92 ERROR("failed to unfreeze container");
93 answer.ret = ret;
94 }
95
96 ret = send(fd, &answer, sizeof(answer), 0);
97 if (ret < 0) {
98 WARN("failed to send answer to the peer");
99 goto out;
100 }
101
102 if (ret != sizeof(answer)) {
103 ERROR("partial answer sent");
104 goto out;
105 }
106
107 out:
108 return -1;
109 }
110