]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/stop.c
From: Daniel Lezcano <daniel.lezcano@free.fr>
[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>
29#include <sys/signal.h>
0ad19a3f 30#include <sys/types.h>
31#include <sys/stat.h>
06efeb5c 32#include <fcntl.h>
0ad19a3f 33
b113348e 34#include <lxc/lxc.h>
0ad19a3f 35
2aa79ee7 36#define MAXPIDLEN 20
37
0ad19a3f 38int lxc_stop(const char *name)
39{
40 char init[MAXPATHLEN];
41 char val[MAXPIDLEN];
e2bcd7db 42 int fd, lock, ret = -LXC_ERROR_INTERNAL;
0ad19a3f 43 size_t pid;
44
45 lock = lxc_get_lock(name);
1f3da8f8 46 if (lock >= 0) {
0ad19a3f 47 lxc_put_lock(lock);
b0a33c1e 48 return -LXC_ERROR_ESRCH;
0ad19a3f 49 }
50
b0a33c1e 51 if (lock < 0 && lock != -LXC_ERROR_EBUSY)
52 return lock;
0ad19a3f 53
54 snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
55 fd = open(init, O_RDONLY);
56 if (fd < 0) {
57 lxc_log_syserror("failed to open init file for %s", name);
b0a33c1e 58 goto out_close;
0ad19a3f 59 }
60
61 if (read(fd, val, sizeof(val)) < 0) {
62 lxc_log_syserror("failed to read %s", init);
63 goto out_close;
64 }
65
66 pid = atoi(val);
67
68 if (kill(pid, SIGKILL)) {
69 lxc_log_syserror("failed to kill %zd", pid);
70 goto out_close;
71 }
72
73 if (unlink(init)) {
74 lxc_log_syserror("failed to unlink %s", init);
75 goto out_close;
76 }
77
78 ret = 0;
79
80out_close:
81 close(fd);
0ad19a3f 82 return ret;
83}