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