]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/stop.c
pass lxcpath to lxc_command
[mirror_lxc.git] / src / lxc / stop.c
CommitLineData
0ad19a3f 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 */
0ad19a3f 23#include <stdio.h>
0ad19a3f 24#include <stdlib.h>
25#include <string.h>
0ad19a3f 26#include <errno.h>
06efeb5c 27#include <unistd.h>
28#include <sys/param.h>
8173e600 29#include <signal.h>
0ad19a3f 30#include <sys/types.h>
31#include <sys/stat.h>
00b3c2e2 32#include <sys/socket.h>
06efeb5c 33#include <fcntl.h>
0ad19a3f 34
36eb9bde 35#include <lxc/log.h>
00b3c2e2
CLG
36#include <lxc/start.h>
37
d1c383f3 38#include "lxc.h"
2137dc99 39#include "commands.h"
36eb9bde
CLG
40
41lxc_log_define(lxc_stop, lxc);
0ad19a3f 42
13f5be62 43int lxc_stop(const char *name, const char *lxcpath)
2137dc99
DL
44{
45 struct lxc_command command = {
46 .request = { .type = LXC_COMMAND_STOP },
47 };
48
d97b36f8
DL
49 int ret, stopped = 0;
50
13f5be62 51 ret = lxc_command(name, &command,&stopped, lxcpath);
d97b36f8
DL
52 if (ret < 0 && stopped) {
53 INFO("'%s' is already stopped", name);
54 return 0;
55 }
2137dc99 56
2137dc99
DL
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) {
d97b36f8 66 ERROR("failed to stop '%s': %s",
2137dc99
DL
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 *--------------------------------------------------------------------------*/
80extern 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);
371828c4
SB
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 }
2137dc99
DL
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
107out:
108 return -1;
109}
110