Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

vzctl: allocate a device name dynamically

Before this patch a device name has been limited by 32 symbols
Now we want add input device for exmple:
/dev/input/by-path/pci-0000:00:12.0-usb-0:3:1.1-event

Signed-off-by: Andrey Vagin <avagin@openvz.org>
  • Loading branch information...
commit ea64ee6b9a479a13cd2ba5c4cf73daa911ccaf4a 1 parent 913ebc6
@avagin avagin authored kolyshkin committed
Showing with 34 additions and 11 deletions.
  1. +1 −1  include/dev.h
  2. +31 −10 src/lib/config.c
  3. +2 −0  src/lib/dev.c
View
2  include/dev.h
@@ -28,7 +28,7 @@
*/
typedef struct {
list_elem_t list; /**< prev/next elements. */
- char name[32]; /**< device name. */
+ char *name; /**< device name. */
dev_t dev; /**< device number. */
unsigned int type; /**< S_IFBLK | S_IFCHR. */
unsigned int mask; /**< access mode. */
View
41 src/lib/config.c
@@ -955,8 +955,11 @@ static int parse_dev(vps_param *vps_p, char *val)
for_each_strtok(token, val, " ") {
if (parse_devices_str(token, &dev))
return ERR_INVAL;
- if (add_dev_param(&vps_p->res.dev, &dev))
+ if (add_dev_param(&vps_p->res.dev, &dev)) {
+ if (dev.name)
+ free(dev.name);
return ERR_NOMEM;
+ }
}
return 0;
@@ -1013,36 +1016,51 @@ static int store_dev(vps_param *old_p, vps_param *vps_p, vps_config *conf,
static int parse_devnodes_str(const char *str, dev_res *dev)
{
char *ch;
- unsigned int len;
- char buf[64];
+ unsigned int len, buf_len;
+ char *buf;
struct stat st;
+ int ret = ERR_INVAL;
if ((ch = strrchr(str, ':')) == NULL)
return ERR_INVAL;
ch++;
len = ch - str;
- if (len > sizeof(dev->name))
- return ERR_INVAL;
memset(dev, 0, sizeof(*dev));
+
+ dev->name = malloc(len);
+ if (dev->name == NULL)
+ return ERR_NOMEM;
snprintf(dev->name, len, "%s", str);
- snprintf(buf, sizeof(buf), "/dev/%s", dev->name);
+
+ buf_len = 5 + len;
+ buf = alloca(buf_len);
+ if (buf == NULL) {
+ ret = ERR_NOMEM;
+ goto err;
+ }
+ snprintf(buf, buf_len, "/dev/%s", dev->name);
if (stat(buf, &st)) {
logger(-1, errno, "Incorrect device name %s", buf);
- return ERR_INVAL;
+ goto err;
}
+
if (S_ISCHR(st.st_mode))
dev->type = S_IFCHR;
else if (S_ISBLK(st.st_mode))
dev->type = S_IFBLK;
else {
logger(-1, 0, "The %s is not block or character device", buf);
- return ERR_INVAL;
+ goto err;
}
dev->dev = st.st_rdev;
dev->type |= VE_USE_MINOR;
if (parse_dev_perm(ch, &dev->mask))
- return ERR_INVAL;
+ goto err;
return 0;
+err:
+ free(dev->name);
+ dev->name = NULL;
+ return ret;
}
static int parse_devnodes(vps_param *vps_p, char *val)
@@ -1053,8 +1071,11 @@ static int parse_devnodes(vps_param *vps_p, char *val)
for_each_strtok(token, val, " ") {
if (parse_devnodes_str(token, &dev))
return ERR_INVAL;
- if (add_dev_param(&vps_p->res.dev, &dev))
+ if (add_dev_param(&vps_p->res.dev, &dev)) {
+ if (dev.name)
+ free(dev.name);
return ERR_NOMEM;
+ }
}
return 0;
View
2  src/lib/dev.c
@@ -149,6 +149,7 @@ int add_dev_param(dev_param *dev, dev_res *res)
return -1;
memcpy(tmp, res, sizeof(*tmp));
list_add_tail(&tmp->list, &dev->dev);
+ res->name = NULL;
return 0;
}
@@ -159,6 +160,7 @@ static void free_dev(list_head_t *head)
list_for_each_safe(cur, tmp, head, list) {
list_del(&cur->list);
+ free(cur->name);
free(cur);
}
list_head_init(head);
Please sign in to comment.
Something went wrong with that request. Please try again.