]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/destroy.c
Joined liblxc and lxc directory
[mirror_lxc.git] / src / lxc / destroy.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
24 #define _GNU_SOURCE
25 #include <stdio.h>
26 #undef _GNU_SOURCE
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/param.h>
34 #include <sys/inotify.h>
35 #include <sys/file.h>
36 #include <netinet/in.h>
37 #include <net/if.h>
38
39 #include <lxc.h>
40
41 static int dir_filter(const struct dirent *dirent)
42 {
43 if (!strcmp(dirent->d_name, ".") ||
44 !strcmp(dirent->d_name, ".."))
45 return 0;
46 return 1;
47 }
48
49 static int is_empty_directory(const char *dirname)
50 {
51 struct dirent **namelist;
52 int n;
53
54 n = scandir(dirname, &namelist, dir_filter, alphasort);
55 if (n < 0)
56 lxc_log_syserror("failed to scan %s directory", dirname);
57 return n == 0;
58 }
59
60 static int remove_lxc_directory(const char *dirname)
61 {
62 char path[MAXPATHLEN];
63
64 sprintf(path, LXCPATH "/%s", dirname);
65
66 if (rmdir(path)) {
67 lxc_log_syserror("failed to remove %s directory", path);
68 return -1;
69 }
70
71 if (is_empty_directory(LXCPATH)) {
72 if (rmdir(LXCPATH)) {
73 lxc_log_syserror("failed to remove %s directory", LXCPATH);
74 return -1;
75 }
76 }
77
78 return 0;
79 }
80
81 int lxc_destroy(const char *name)
82 {
83 int ret = -1, lock;
84
85 lock = lxc_get_lock(name);
86 if (!lock) {
87 lxc_log_error("'%s' is busy", name);
88 goto out;
89 }
90
91 if (lock < 0) {
92 lxc_log_error("failed to acquire the lock for '%s':%s",
93 name, strerror(-lock));
94 goto out;
95 }
96
97 if (lxc_rmstate(name)) {
98 lxc_log_error("failed to remove state file for %s", name);
99 goto out_lock;
100 }
101
102 lxc_monitor_cleanup(name);
103
104 if (lxc_unconfigure(name)) {
105 lxc_log_error("failed to cleanup %s", name);
106 goto out_lock;
107 }
108
109 if (remove_lxc_directory(name)) {
110 lxc_log_syserror("failed to remove '%s'", name);
111 goto out_lock;
112 }
113
114 ret = 0;
115
116 out_lock:
117 lxc_put_lock(lock);
118 out:
119 return ret;
120 }