]>
Commit | Line | Data |
---|---|---|
5e97c3fc | 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 | #include <stdio.h> | |
25 | #include <string.h> | |
26 | #include <dirent.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
29 | #include <sys/param.h> | |
30 | #include <sys/inotify.h> | |
31 | #include <sys/file.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
34 | #include <netinet/in.h> | |
35 | #include <net/if.h> | |
36 | ||
37 | #include <lxc.h> | |
5e97c3fc | 38 | |
39 | static int dir_filter(const struct dirent *dirent) | |
40 | { | |
41 | if (!strcmp(dirent->d_name, ".") || | |
42 | !strcmp(dirent->d_name, "..")) | |
43 | return 0; | |
44 | return 1; | |
45 | } | |
46 | ||
47 | static int is_empty_directory(const char *dirname) | |
48 | { | |
49 | struct dirent **namelist; | |
50 | int n; | |
51 | ||
52 | n = scandir(dirname, &namelist, dir_filter, alphasort); | |
53 | if (n < 0) | |
54 | lxc_log_syserror("failed to scan %s directory", dirname); | |
55 | return n == 0; | |
56 | } | |
57 | ||
58 | static int create_lxc_directory(const char *dirname) | |
59 | { | |
60 | char path[MAXPATHLEN]; | |
61 | ||
62 | if (mkdir(LXCPATH, 0755) && errno != EEXIST) { | |
63 | lxc_log_syserror("failed to created %s directory", LXCPATH); | |
64 | return -1; | |
65 | } | |
66 | ||
67 | sprintf(path, LXCPATH "/%s", dirname); | |
68 | ||
69 | if (mkdir(path, 0755)) { | |
70 | lxc_log_syserror("failed to created %s directory", path); | |
71 | return -1; | |
72 | } | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
77 | static int remove_lxc_directory(const char *dirname) | |
78 | { | |
79 | char path[MAXPATHLEN]; | |
80 | ||
81 | sprintf(path, LXCPATH "/%s", dirname); | |
82 | ||
83 | if (rmdir(path)) { | |
84 | lxc_log_syserror("failed to remove %s directory", path); | |
85 | return -1; | |
86 | } | |
87 | ||
88 | if (is_empty_directory(LXCPATH)) { | |
89 | if (rmdir(LXCPATH)) { | |
90 | lxc_log_syserror("failed to remove %s directory", LXCPATH); | |
91 | return -1; | |
92 | } | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | int lxc_create(const char *name, struct lxc_conf *conf) | |
99 | { | |
100 | int lock, err = -1; | |
101 | ||
102 | if (create_lxc_directory(name)) { | |
103 | lxc_log_error("failed to create %s directory", name); | |
104 | return -1; | |
105 | } | |
106 | ||
107 | lock = lxc_get_lock(name); | |
108 | if (!lock) { | |
109 | lxc_log_error("'%s' is busy", name); | |
110 | goto err; | |
111 | } | |
112 | ||
113 | if (lock < 0) { | |
114 | lxc_log_error("failed to acquire lock on '%s':%s", | |
115 | name, strerror(-lock)); | |
116 | goto err; | |
117 | } | |
118 | ||
c2cc9f0a | 119 | if (lxc_mkstate(name)) { |
5e97c3fc | 120 | lxc_log_error("failed to create the state file for %s", name); |
121 | goto err; | |
122 | } | |
123 | ||
124 | if (lxc_setstate(name, STOPPED)) { | |
125 | lxc_log_error("failed to set state for %s", name); | |
126 | goto err_state; | |
127 | } | |
128 | ||
129 | if (lxc_configure(name, conf)) { | |
130 | lxc_log_error("failed to set configuration for %s", name); | |
131 | goto err_state; | |
132 | } | |
133 | ||
134 | err = 0; | |
135 | out: | |
136 | lxc_put_lock(lock); | |
137 | return err; | |
138 | ||
139 | err_state: | |
140 | lxc_unconfigure(name); | |
141 | ||
c2cc9f0a | 142 | if (lxc_rmstate(name)) |
5e97c3fc | 143 | lxc_log_error("failed to remove state file for %s", name); |
144 | err: | |
145 | if (remove_lxc_directory(name)) | |
146 | lxc_log_error("failed to cleanup lxc directory for %s", name); | |
147 | goto out; | |
148 | } |