]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/stop.c
lxc: use new logging system
[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 <fcntl.h>
33
34 #include <lxc/lxc.h>
35 #include <lxc/log.h>
36
37 lxc_log_define(lxc_stop, lxc);
38
39 #define MAXPIDLEN 20
40
41 int lxc_stop(const char *name)
42 {
43 char init[MAXPATHLEN];
44 char val[MAXPIDLEN];
45 int fd, lock, ret = -LXC_ERROR_INTERNAL;
46 size_t pid;
47
48 lock = lxc_get_lock(name);
49 if (lock >= 0) {
50 lxc_put_lock(lock);
51 return -LXC_ERROR_ESRCH;
52 }
53
54 if (lock < 0 && lock != -LXC_ERROR_EBUSY)
55 return lock;
56
57 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
58 fd = open(init, O_RDONLY);
59 if (fd < 0) {
60 SYSERROR("failed to open init file for %s", name);
61 goto out_close;
62 }
63
64 if (read(fd, val, sizeof(val)) < 0) {
65 SYSERROR("failed to read %s", init);
66 goto out_close;
67 }
68
69 pid = atoi(val);
70
71 if (kill(pid, SIGKILL)) {
72 SYSERROR("failed to kill %zd", pid);
73 goto out_close;
74 }
75
76 ret = 0;
77
78 out_close:
79 close(fd);
80 return ret;
81 }