]>
Commit | Line | Data |
---|---|---|
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 | */ | |
23 | ||
cfd1dc09 | 24 | #define _GNU_SOURCE |
0ad19a3f | 25 | #include <stdio.h> |
cfd1dc09 MN |
26 | #undef _GNU_SOURCE |
27 | #include <stdlib.h> | |
0ad19a3f | 28 | #include <string.h> |
29 | #include <dirent.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
32 | #include <sys/param.h> | |
0ad19a3f | 33 | #include <sys/stat.h> |
34 | #include <sys/types.h> | |
e2bcd7db | 35 | #include "error.h" |
b113348e | 36 | #include <lxc/lxc.h> |
36eb9bde CLG |
37 | #include <lxc/log.h> |
38 | ||
881450bb DL |
39 | #include "config.h" |
40 | ||
36eb9bde | 41 | lxc_log_define(lxc_create, lxc); |
0ad19a3f | 42 | |
43 | static int dir_filter(const struct dirent *dirent) | |
44 | { | |
45 | if (!strcmp(dirent->d_name, ".") || | |
46 | !strcmp(dirent->d_name, "..")) | |
47 | return 0; | |
48 | return 1; | |
49 | } | |
50 | ||
51 | static int is_empty_directory(const char *dirname) | |
52 | { | |
53 | struct dirent **namelist; | |
54 | int n; | |
55 | ||
56 | n = scandir(dirname, &namelist, dir_filter, alphasort); | |
57 | if (n < 0) | |
36eb9bde | 58 | SYSERROR("failed to scan %s directory", dirname); |
0ad19a3f | 59 | return n == 0; |
60 | } | |
61 | ||
62 | static int create_lxc_directory(const char *dirname) | |
63 | { | |
64 | char path[MAXPATHLEN]; | |
65 | ||
a6f2de7e | 66 | if (mkdir(LXCPATH, 0755) && errno != EEXIST) { |
36eb9bde | 67 | SYSERROR("failed to create %s directory", LXCPATH); |
7f830613 | 68 | return -1; |
a6f2de7e | 69 | } |
0ad19a3f | 70 | |
7f830613 | 71 | snprintf(path, MAXPATHLEN, LXCPATH "/%s", dirname); |
0ad19a3f | 72 | |
a6f2de7e | 73 | if (mkdir(path, 0755)) { |
36eb9bde | 74 | SYSERROR("failed to create %s directory", path); |
7f830613 | 75 | return -1; |
a6f2de7e | 76 | } |
0ad19a3f | 77 | |
78 | return 0; | |
79 | } | |
80 | ||
81 | static int remove_lxc_directory(const char *dirname) | |
82 | { | |
83 | char path[MAXPATHLEN]; | |
84 | ||
7f830613 | 85 | snprintf(path, MAXPATHLEN, LXCPATH "/%s", dirname); |
0ad19a3f | 86 | |
87 | if (rmdir(path)) { | |
36eb9bde | 88 | SYSERROR("failed to remove %s directory", path); |
0ad19a3f | 89 | return -1; |
90 | } | |
91 | ||
92 | if (is_empty_directory(LXCPATH)) { | |
93 | if (rmdir(LXCPATH)) { | |
36eb9bde | 94 | SYSERROR("failed to remove %s directory", LXCPATH); |
0ad19a3f | 95 | return -1; |
96 | } | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
cfd1dc09 MN |
102 | static int copy_config_file(const char *name, const char *file) |
103 | { | |
104 | char *dst; | |
d685aa80 | 105 | int ret = -1; |
cfd1dc09 MN |
106 | |
107 | if (!asprintf(&dst, LXCPATH "/%s/config", name)) { | |
108 | ERROR("failed to allocate memory"); | |
109 | return -1; | |
110 | } | |
111 | ||
112 | ret = lxc_copy_file(file, dst); | |
113 | if (ret) | |
114 | ERROR("failed to copy '%s' to '%s'", file, dst); | |
d685aa80 | 115 | |
cfd1dc09 MN |
116 | free(dst); |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
d685aa80 | 121 | int lxc_create(const char *name, const char *confile) |
0ad19a3f | 122 | { |
7f830613 | 123 | int lock, err = -1; |
0ad19a3f | 124 | |
7f830613 MN |
125 | if (create_lxc_directory(name)) |
126 | return err; | |
1f3da8f8 | 127 | |
d685aa80 DL |
128 | if (!confile) |
129 | return 0; | |
130 | ||
0ad19a3f | 131 | lock = lxc_get_lock(name); |
1f3da8f8 | 132 | if (lock < 0) |
0ad19a3f | 133 | goto err; |
0ad19a3f | 134 | |
d685aa80 | 135 | if (copy_config_file(name, confile)) { |
cfd1dc09 MN |
136 | ERROR("failed to copy the configuration file"); |
137 | goto err_state; | |
138 | } | |
139 | ||
0ad19a3f | 140 | err = 0; |
141 | out: | |
142 | lxc_put_lock(lock); | |
143 | return err; | |
144 | ||
145 | err_state: | |
146 | lxc_unconfigure(name); | |
147 | ||
148 | if (lxc_rmstate(name)) | |
36eb9bde | 149 | ERROR("failed to remove state file for %s", name); |
0ad19a3f | 150 | err: |
151 | if (remove_lxc_directory(name)) | |
36eb9bde | 152 | ERROR("failed to cleanup lxc directory for %s", name); |
0ad19a3f | 153 | goto out; |
154 | } |