Replace asprintf by snprintf

This commit is contained in:
dlezcano 2008-10-04 19:47:17 +00:00
parent 19df00c503
commit 22ebac194e
6 changed files with 96 additions and 91 deletions

View File

@ -44,7 +44,8 @@ LXC_TTY_HANDLER(SIGQUIT);
int lxc_execute(const char *name, int argc, char *argv[], int lxc_execute(const char *name, int argc, char *argv[],
lxc_callback_t preexec, void *data) lxc_callback_t preexec, void *data)
{ {
char *init = NULL, *val = NULL, *vinit = "[vinit]"; char init[MAXPATHLEN];
char *val = NULL, *vinit = "[vinit]";
int fd, lock, sv[2], sync = 0, err = -1; int fd, lock, sv[2], sync = 0, err = -1;
pid_t pid; pid_t pid;
int clone_flags; int clone_flags;
@ -194,14 +195,19 @@ int lxc_execute(const char *name, int argc, char *argv[],
goto err_child_failed; goto err_child_failed;
} }
asprintf(&init, LXCPATH "/%s/init", name); snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0) { if (fd < 0) {
lxc_log_syserror("failed to open %s", init); lxc_log_syserror("failed to open %s", init);
goto err_open; goto err_open;
} }
asprintf(&val, "%d", pid); if (!asprintf(&val, "%d", pid)) {
lxc_log_syserror("failed to allocate memory");
goto err_open;
}
if (write(fd, val, strlen(val)) < 0) { if (write(fd, val, strlen(val)) < 0) {
lxc_log_syserror("failed to write init pid"); lxc_log_syserror("failed to write init pid");
goto err_write; goto err_write;
@ -236,7 +242,6 @@ out:
lxc_unlink_nsgroup(name); lxc_unlink_nsgroup(name);
unlink(init); unlink(init);
free(init);
free(val); free(val);
lxc_put_lock(lock); lxc_put_lock(lock);

View File

@ -68,7 +68,9 @@ out:
int lxc_link_nsgroup(const char *name, pid_t pid) int lxc_link_nsgroup(const char *name, pid_t pid)
{ {
char *lxc, *nsgroup, cgroup[MAXPATHLEN]; char lxc[MAXPATHLEN];
char nsgroup[MAXPATHLEN];
char cgroup[MAXPATHLEN];
int ret; int ret;
if (get_cgroup_mount(MTAB, cgroup)) { if (get_cgroup_mount(MTAB, cgroup)) {
@ -76,29 +78,23 @@ int lxc_link_nsgroup(const char *name, pid_t pid)
return -1; return -1;
} }
asprintf(&lxc, LXCPATH "/%s/nsgroup", name); snprintf(lxc, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
asprintf(&nsgroup, "%s/%d", cgroup, pid); snprintf(nsgroup, MAXPATHLEN, "%s/%d", cgroup, pid);
unlink(lxc); unlink(lxc);
ret = symlink(nsgroup, lxc); ret = symlink(nsgroup, lxc);
if (ret) if (ret)
lxc_log_syserror("failed to create symlink %s->%s", lxc_log_syserror("failed to create symlink %s->%s",
nsgroup, lxc); nsgroup, lxc);
free(lxc); return ret;
free(nsgroup);
return ret;
} }
int lxc_unlink_nsgroup(const char *name) int lxc_unlink_nsgroup(const char *name)
{ {
char *nsgroup; char nsgroup[MAXPATHLEN];
int ret;
asprintf(&nsgroup, LXCPATH "/%s/nsgroup", name); snprintf(nsgroup, MAXPATHLEN, LXCPATH "/%s/nsgroup", name);
ret = unlink(nsgroup); return unlink(nsgroup);
free(nsgroup);
return ret;
} }
int lxc_cgroup_set_priority(const char *name, int priority) int lxc_cgroup_set_priority(const char *name, int priority)
@ -137,28 +133,26 @@ out:
int lxc_cgroup_get_priority(const char *name, int *priority) int lxc_cgroup_get_priority(const char *name, int *priority)
{ {
int fd, ret = -1; int fd, ret = -1;
char *path, prio[MAXPRIOLEN]; char path[MAXPATHLEN], prio[MAXPRIOLEN];
asprintf(&path, LXCPATH "/%s/nsgroup/cpu.shares", name); snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/cpu.shares", name);
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
lxc_log_syserror("failed to open '%s'", path); lxc_log_syserror("failed to open '%s'", path);
goto out; return -1;
} }
if (read(fd, prio, MAXPRIOLEN) < 0) { if (read(fd, prio, MAXPRIOLEN) < 0) {
lxc_log_syserror("failed to read from '%s'", path); lxc_log_syserror("failed to read from '%s'", path);
close(fd);
goto out; goto out;
} }
close(fd);
*priority = atoi(prio); *priority = atoi(prio);
ret = 0; ret = 0;
out: out:
free(path); close(fd);
return ret; return ret;
} }

View File

@ -132,9 +132,9 @@ out:
static int write_info(const char *path, const char *file, const char *info) static int write_info(const char *path, const char *file, const char *info)
{ {
int fd, err = -1; int fd, err = -1;
char *f; char f[MAXPATHLEN];
asprintf(&f, "%s/%s", path, file); snprintf(f, MAXPATHLEN, "%s/%s", path, file);
fd = creat(f, 0755); fd = creat(f, 0755);
if (fd < 0) if (fd < 0)
goto out; goto out;
@ -145,7 +145,6 @@ static int write_info(const char *path, const char *file, const char *info)
err = 0; err = 0;
out: out:
close(fd); close(fd);
free(f);
return err; return err;
out_write: out_write:
@ -156,9 +155,9 @@ out_write:
static int read_info(const char *path, const char *file, char *info, size_t len) static int read_info(const char *path, const char *file, char *info, size_t len)
{ {
int fd, ret = -1; int fd, ret = -1;
char *f, *token; char f[MAXPATHLEN], *token;
asprintf(&f, "%s/%s", path, file); snprintf(f, MAXPATHLEN, "%s/%s", path, file);
fd = open(f, O_RDONLY); fd = open(f, O_RDONLY);
if (fd < 0) { if (fd < 0) {
if (errno == ENOENT) if (errno == ENOENT)
@ -176,18 +175,16 @@ static int read_info(const char *path, const char *file, char *info, size_t len)
ret = 0; ret = 0;
out: out:
close(fd); close(fd);
free(f);
return ret; return ret;
} }
static int delete_info(const char *path, const char *file) static int delete_info(const char *path, const char *file)
{ {
char *info; char info[MAXPATHLEN];
int ret; int ret;
asprintf(&info, "%s/%s", path, file); snprintf(info, MAXPATHLEN, "%s/%s", path, file);
ret = unlink(info); ret = unlink(info);
free(info);
return ret; return ret;
} }
@ -196,7 +193,7 @@ static int configure_ip4addr(int fd, struct lxc_inetdev *in)
{ {
char addr[INET6_ADDRSTRLEN]; char addr[INET6_ADDRSTRLEN];
char bcast[INET_ADDRSTRLEN]; char bcast[INET_ADDRSTRLEN];
char *line = NULL; char line[MAXLINELEN];
int err = -1; int err = -1;
if (!inet_ntop(AF_INET, &in->addr, addr, sizeof(addr))) { if (!inet_ntop(AF_INET, &in->addr, addr, sizeof(addr))) {
@ -210,9 +207,9 @@ static int configure_ip4addr(int fd, struct lxc_inetdev *in)
} }
if (in->prefix) if (in->prefix)
asprintf(&line, "%s/%d %s\n", addr, in->prefix, bcast); snprintf(line, MAXLINELEN, "%s/%d %s\n", addr, in->prefix, bcast);
else else
asprintf(&line, "%s %s\n", addr, bcast); snprintf(line, MAXLINELEN, "%s %s\n", addr, bcast);
if (write(fd, line, strlen(line)) < 0) { if (write(fd, line, strlen(line)) < 0) {
lxc_log_syserror("failed to write address info"); lxc_log_syserror("failed to write address info");
@ -221,14 +218,13 @@ static int configure_ip4addr(int fd, struct lxc_inetdev *in)
err = 0; err = 0;
err: err:
free(line);
return err; return err;
} }
static int configure_ip6addr(int fd, struct lxc_inet6dev *in6) static int configure_ip6addr(int fd, struct lxc_inet6dev *in6)
{ {
char addr[INET6_ADDRSTRLEN]; char addr[INET6_ADDRSTRLEN];
char *line = NULL; char line[MAXLINELEN];
int err = -1; int err = -1;
if (!inet_ntop(AF_INET6, &in6->addr, addr, sizeof(addr))) { if (!inet_ntop(AF_INET6, &in6->addr, addr, sizeof(addr))) {
@ -236,7 +232,7 @@ static int configure_ip6addr(int fd, struct lxc_inet6dev *in6)
goto err; goto err;
} }
asprintf(&line, "%s/%d\n", addr, in6->prefix?in6->prefix:64); snprintf(line, MAXLINELEN, "%s/%d\n", addr, in6->prefix?in6->prefix:64);
if (write(fd, line, strlen(line)) < 0) { if (write(fd, line, strlen(line)) < 0) {
lxc_log_syserror("failed to write address info"); lxc_log_syserror("failed to write address info");
@ -245,7 +241,6 @@ static int configure_ip6addr(int fd, struct lxc_inet6dev *in6)
err = 0; err = 0;
err: err:
free(line);
return err; return err;
} }
@ -429,14 +424,14 @@ static int configure_rootfs(const char *name, const char *rootfs)
static int configure_mount(const char *name, const char *fstab) static int configure_mount(const char *name, const char *fstab)
{ {
char *path; char path[MAXPATHLEN];
struct stat stat; struct stat stat;
int infd, outfd; int infd, outfd;
void *src, *dst; void *src, *dst;
char c = '\0'; char c = '\0';
int ret = -1; int ret = -1;
asprintf(&path, LXCPATH "/%s/fstab", name); snprintf(path, MAXPATHLEN, LXCPATH "/%s/fstab", name);
outfd = open(path, O_RDWR|O_CREAT|O_EXCL, 0640); outfd = open(path, O_RDWR|O_CREAT|O_EXCL, 0640);
if (outfd < 0) { if (outfd < 0) {
@ -447,35 +442,35 @@ static int configure_mount(const char *name, const char *fstab)
infd = open(fstab, O_RDONLY); infd = open(fstab, O_RDONLY);
if (infd < 0) { if (infd < 0) {
lxc_log_syserror("failed to open '%s'", fstab); lxc_log_syserror("failed to open '%s'", fstab);
goto out; goto out_open;
} }
if (fstat(infd, &stat)) { if (fstat(infd, &stat)) {
lxc_log_syserror("failed to stat '%s'", fstab); lxc_log_syserror("failed to stat '%s'", fstab);
goto out; goto out_open2;
} }
if (lseek(outfd, stat.st_size - 1, SEEK_SET) < 0) { if (lseek(outfd, stat.st_size - 1, SEEK_SET) < 0) {
lxc_log_syserror("failed to seek dest file '%s'", path); lxc_log_syserror("failed to seek dest file '%s'", path);
goto out; goto out_open2;
} }
/* fixup length */ /* fixup length */
if (write(outfd, &c, 1) < 0) { if (write(outfd, &c, 1) < 0) {
lxc_log_syserror("failed to write to '%s'", path); lxc_log_syserror("failed to write to '%s'", path);
goto out; goto out_open2;
} }
src = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, infd, 0L); src = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, infd, 0L);
if (src == MAP_FAILED) { if (src == MAP_FAILED) {
lxc_log_syserror("failed to mmap '%s'", fstab); lxc_log_syserror("failed to mmap '%s'", fstab);
goto out; goto out_open2;
} }
dst = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, outfd, 0L); dst = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, outfd, 0L);
if (dst == MAP_FAILED) { if (dst == MAP_FAILED) {
lxc_log_syserror("failed to mmap '%s'", path); lxc_log_syserror("failed to mmap '%s'", path);
goto out; goto out_mmap;
} }
memcpy(dst, src, stat.st_size); memcpy(dst, src, stat.st_size);
@ -485,8 +480,16 @@ static int configure_mount(const char *name, const char *fstab)
ret = 0; ret = 0;
out: out:
free(path);
return ret; return ret;
out_mmap:
munmap(src, stat.st_size);
out_open2:
close(infd);
out_open:
unlink(path);
close(outfd);
goto out;
} }
static int unconfigure_ip_addresses(const char *dirname) static int unconfigure_ip_addresses(const char *dirname)
@ -946,9 +949,12 @@ static int instanciate_veth(const char *dirname, const char *file, pid_t pid)
char bridge[IFNAMSIZ]; char bridge[IFNAMSIZ];
int ifindex, ret = -1; int ifindex, ret = -1;
asprintf(&veth1, "%s_%d", file, pid); if (!asprintf(&veth1, "%s_%d", file, pid) ||
asprintf(&veth2, "%s~%d", file, pid); !asprintf(&veth2, "%s~%d", file, pid) ||
asprintf(&path, "%s/%s", dirname, file); !asprintf(&path, "%s/%s", dirname, file)) {
lxc_log_syserror("failed to allocate memory");
goto out;
}
if (read_info(path, "link", bridge, IFNAMSIZ)) { if (read_info(path, "link", bridge, IFNAMSIZ)) {
lxc_log_error("failed to read bridge info"); lxc_log_error("failed to read bridge info");
@ -966,7 +972,11 @@ static int instanciate_veth(const char *dirname, const char *file, pid_t pid)
goto out; goto out;
} }
asprintf(&strindex, "%d", ifindex); if (!asprintf(&strindex, "%d", ifindex)) {
lxc_log_syserror("failed to allocate memory");
goto out;
}
if (write_info(path, "ifindex", strindex)) { if (write_info(path, "ifindex", strindex)) {
lxc_log_error("failed to write interface index to %s", path); lxc_log_error("failed to write interface index to %s", path);
goto out; goto out;
@ -993,7 +1003,11 @@ static int instanciate_macvlan(const char *dirname, const char *file, pid_t pid)
char link[IFNAMSIZ]; char link[IFNAMSIZ];
int ifindex, ret = -1; int ifindex, ret = -1;
asprintf(&peer, "%s~%d", file, pid); if (!asprintf(&peer, "%s~%d", file, pid)) {
lxc_log_syserror("failed to allocate memory");
return -1;
}
snprintf(path, MAXPATHLEN, "%s/%s", dirname, file); snprintf(path, MAXPATHLEN, "%s/%s", dirname, file);
if (read_info(path, "link", link, IFNAMSIZ)) { if (read_info(path, "link", link, IFNAMSIZ)) {
lxc_log_error("failed to read bridge info"); lxc_log_error("failed to read bridge info");
@ -1011,7 +1025,11 @@ static int instanciate_macvlan(const char *dirname, const char *file, pid_t pid)
goto out; goto out;
} }
asprintf(&strindex, "%d", ifindex); if (!asprintf(&strindex, "%d", ifindex)) {
lxc_log_syserror("failed to allocate memory");
return -1;
}
if (write_info(path, "ifindex", strindex)) { if (write_info(path, "ifindex", strindex)) {
lxc_log_error("failed to write interface index to %s", path); lxc_log_error("failed to write interface index to %s", path);
goto out; goto out;
@ -1042,7 +1060,11 @@ static int instanciate_phys(const char *dirname, const char *file, pid_t pid)
goto out; goto out;
} }
asprintf(&strindex, "%d", ifindex); if (!asprintf(&strindex, "%d", ifindex)) {
lxc_log_syserror("failed to allocate memory");
return -1;
}
if (write_info(path, "ifindex", strindex)) { if (write_info(path, "ifindex", strindex)) {
lxc_log_error("failed to write interface index to %s", path); lxc_log_error("failed to write interface index to %s", path);
goto out; goto out;
@ -1095,14 +1117,10 @@ static int instanciate_netdev_cb(const char *name, const char *dirname,
static int instanciate_netdev(const char *name, pid_t pid) static int instanciate_netdev(const char *name, pid_t pid)
{ {
char *dirname; char dirname[MAXPATHLEN];
int ret;
asprintf(&dirname, LXCPATH "/%s/network", name); snprintf(dirname, MAXPATHLEN, LXCPATH "/%s/network", name);
ret = dir_for_each(name, dirname, instanciate_netdev_cb, &pid); return dir_for_each(name, dirname, instanciate_netdev_cb, &pid);
free(dirname);
return ret;
} }
static int move_netdev_cb(const char *name, const char *dirname, static int move_netdev_cb(const char *name, const char *dirname,
@ -1138,14 +1156,9 @@ static int move_netdev_cb(const char *name, const char *dirname,
static int move_netdev(const char *name, pid_t pid) static int move_netdev(const char *name, pid_t pid)
{ {
char *dirname; char dirname[MAXPATHLEN];
int ret; snprintf(dirname, MAXPATHLEN, LXCPATH "/%s/network", name);
return dir_for_each(name, dirname, move_netdev_cb, &pid);
asprintf(&dirname, LXCPATH "/%s/network", name);
ret = dir_for_each(name, dirname, move_netdev_cb, &pid);
free(dirname);
return ret;
} }
int conf_create_network(const char *name, pid_t pid) int conf_create_network(const char *name, pid_t pid)
@ -1206,14 +1219,10 @@ static int delete_netdev_cb(const char *name, const char *dirname,
static int delete_netdev(const char *name) static int delete_netdev(const char *name)
{ {
char *dirname; char dirname[MAXPATHLEN];
int ret;
asprintf(&dirname, LXCPATH "/%s/network", name); snprintf(dirname, MAXPATHLEN, LXCPATH "/%s/network", name);
ret = dir_for_each(name, dirname, delete_netdev_cb, NULL); return dir_for_each(name, dirname, delete_netdev_cb, NULL);
free(dirname);
return ret;
} }
int conf_destroy_network(const char *name) int conf_destroy_network(const char *name)

View File

@ -28,15 +28,16 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/param.h>
#include <lxc/lxc.h> #include <lxc/lxc.h>
int lxc_get_lock(const char *name) int lxc_get_lock(const char *name)
{ {
char *lock; char lock[MAXPATHLEN];
int fd, ret; int fd, ret;
asprintf(&lock, LXCPATH "/%s", name); snprintf(lock, MAXPATHLEN, LXCPATH "/%s", name);
fd = open(lock, O_RDONLY|O_DIRECTORY, S_IRUSR|S_IWUSR); fd = open(lock, O_RDONLY|O_DIRECTORY, S_IRUSR|S_IWUSR);
if (fd < 0) { if (fd < 0) {
ret = -errno; ret = -errno;
@ -53,7 +54,6 @@ int lxc_get_lock(const char *name)
ret = fd; ret = fd;
out: out:
free(lock);
return ret; return ret;
} }

View File

@ -436,19 +436,15 @@ static int proc_sys_net_write(const char *path, const char *value)
static int ip_forward_set(const char *ifname, int family, int flag) static int ip_forward_set(const char *ifname, int family, int flag)
{ {
char *path; char path[MAXPATHLEN];
int ret;
if (family != AF_INET && family != AF_INET6) if (family != AF_INET && family != AF_INET6)
return -1; return -1;
asprintf(&path, "/proc/sys/net/%s/conf/%s/forwarding", snprintf(path, MAXPATHLEN, "/proc/sys/net/%s/conf/%s/forwarding",
family == AF_INET?"ipv4":"ipv6" , ifname); family == AF_INET?"ipv4":"ipv6" , ifname);
ret = proc_sys_net_write(path, flag?"1":"0"); return proc_sys_net_write(path, flag?"1":"0");
free(path);
return ret;
} }
int ip_forward_on(const char *ifname, int family) int ip_forward_on(const char *ifname, int family)

View File

@ -70,7 +70,8 @@ int opentty(const char *ttyname)
int lxc_start(const char *name, int argc, char *argv[], int lxc_start(const char *name, int argc, char *argv[],
lxc_callback_t prestart, void *data) lxc_callback_t prestart, void *data)
{ {
char *init = NULL, *val = NULL; char init[MAXPATHLEN];
char *val = NULL;
char ttyname[MAXPATHLEN]; char ttyname[MAXPATHLEN];
int fd, lock, sv[2], sync = 0, err = -1; int fd, lock, sv[2], sync = 0, err = -1;
pid_t pid; pid_t pid;
@ -213,7 +214,9 @@ int lxc_start(const char *name, int argc, char *argv[],
} }
asprintf(&val, "%d\n", pid); asprintf(&val, "%d\n", pid);
asprintf(&init, LXCPATH "/%s/init", name);
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); fd = open(init, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0) { if (fd < 0) {
lxc_log_syserror("failed to open '%s'", init); lxc_log_syserror("failed to open '%s'", init);
@ -257,7 +260,6 @@ out:
lxc_unlink_nsgroup(name); lxc_unlink_nsgroup(name);
unlink(init); unlink(init);
free(init);
free(val); free(val);
lxc_put_lock(lock); lxc_put_lock(lock);
@ -284,6 +286,5 @@ err_fork_ns:
LXC_TTY_DEL_HANDLER(SIGINT); LXC_TTY_DEL_HANDLER(SIGINT);
close(sv[0]); close(sv[0]);
close(sv[1]); close(sv[1]);
err:
goto out; goto out;
} }