]>
git.proxmox.com Git - mirror_lxc.git/blob - src/liblxc/create.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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
29 #include <sys/param.h>
30 #include <sys/inotify.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
44 static int dir_filter(const struct dirent
*dirent
)
46 if (!strcmp(dirent
->d_name
, ".") ||
47 !strcmp(dirent
->d_name
, ".."))
52 static int is_empty_directory(const char *dirname
)
54 struct dirent
**namelist
;
57 n
= scandir(dirname
, &namelist
, dir_filter
, alphasort
);
59 lxc_log_syserror("failed to scan %s directory", dirname
);
63 static int create_lxc_directory(const char *dirname
)
65 char path
[MAXPATHLEN
];
67 if (mkdir(LXCPATH
, 0755) && errno
!= EEXIST
) {
68 lxc_log_syserror("failed to created %s directory", LXCPATH
);
72 sprintf(path
, LXCPATH
"/%s", dirname
);
74 if (mkdir(path
, 0755)) {
75 lxc_log_syserror("failed to created %s directory", path
);
82 static int remove_lxc_directory(const char *dirname
)
84 char path
[MAXPATHLEN
];
86 sprintf(path
, LXCPATH
"/%s", dirname
);
89 lxc_log_syserror("failed to remove %s directory", path
);
93 if (is_empty_directory(LXCPATH
)) {
95 lxc_log_syserror("failed to remove %s directory", LXCPATH
);
103 int lxc_create(const char *name
, struct lxc_conf
*conf
)
107 if (create_lxc_directory(name
)) {
108 lxc_log_error("failed to create %s directory", name
);
112 lock
= lxc_get_lock(name
);
114 lxc_log_error("'%s' is busy", name
);
119 lxc_log_error("failed to acquire lock on '%s':%s",
120 name
, strerror(-lock
));
125 lxc_log_error("failed to create the state file for %s", name
);
129 if (lxc_setstate(name
, STOPPED
)) {
130 lxc_log_error("failed to set state for %s", name
);
134 if (lxc_configure(name
, conf
)) {
135 lxc_log_error("failed to set configuration for %s", name
);
145 lxc_unconfigure(name
);
148 lxc_log_error("failed to remove state file for %s", name
);
150 if (remove_lxc_directory(name
))
151 lxc_log_error("failed to cleanup lxc directory for %s", name
);