summaryrefslogtreecommitdiff
path: root/udev/lib
diff options
context:
space:
mode:
Diffstat (limited to 'udev/lib')
-rw-r--r--udev/lib/Makefile.am4
-rw-r--r--udev/lib/libudev-ctrl.c3
-rw-r--r--udev/lib/libudev-device.c40
-rw-r--r--udev/lib/libudev-enumerate.c41
-rw-r--r--udev/lib/libudev-monitor.c1
-rw-r--r--udev/lib/libudev-private.h11
-rw-r--r--udev/lib/libudev-util.c83
-rw-r--r--udev/lib/libudev.c3
8 files changed, 116 insertions, 70 deletions
diff --git a/udev/lib/Makefile.am b/udev/lib/Makefile.am
index 5d6438eb8e..000b3f73ca 100644
--- a/udev/lib/Makefile.am
+++ b/udev/lib/Makefile.am
@@ -28,9 +28,7 @@ libudev_la_SOURCES =\
libudev-enumerate.c \
libudev-ctrl.c \
libudev-monitor.c \
- ../list.h \
- ../udev.h \
- ../udev_sysdeps.c
+ list.h
libudev_la_LDFLAGS = \
-version-info $(LIBUDEV_LT_CURRENT):$(LIBUDEV_LT_REVISION):$(LIBUDEV_LT_AGE) \
diff --git a/udev/lib/libudev-ctrl.c b/udev/lib/libudev-ctrl.c
index 6d91ea4dbc..e42f631571 100644
--- a/udev/lib/libudev-ctrl.c
+++ b/udev/lib/libudev-ctrl.c
@@ -29,7 +29,6 @@
#include <sys/socket.h>
#include <sys/un.h>
-#include "../udev.h"
#include "libudev.h"
#include "libudev-private.h"
@@ -155,7 +154,7 @@ static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int
ctrl_msg.type = type;
if (buf != NULL)
- strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
+ util_strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
else
ctrl_msg.intval = intval;
diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c
index 78b644b01d..a182750e61 100644
--- a/udev/lib/libudev-device.c
+++ b/udev/lib/libudev-device.c
@@ -30,7 +30,6 @@
#include "libudev.h"
#include "libudev-private.h"
-#include "../udev.h"
struct udev_device {
int refcount;
@@ -58,17 +57,17 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f
size_t start;
/* translate to location of db file */
- strlcpy(filename, udev_get_dev_path(udev), len);
- start = strlcat(filename, "/.udev/db/", len);
- strlcat(filename, devpath, len);
+ util_strlcpy(filename, udev_get_dev_path(udev), len);
+ start = util_strlcat(filename, "/.udev/db/", len);
+ util_strlcat(filename, devpath, len);
return util_path_encode(&filename[start], len - start);
}
static int device_read_db(struct udev_device *udev_device)
{
struct stat stats;
- char filename[PATH_SIZE];
- char line[PATH_SIZE];
+ char filename[UTIL_PATH_SIZE];
+ char line[UTIL_LINE_SIZE];
FILE *f;
int rc = 0;
@@ -79,7 +78,7 @@ static int device_read_db(struct udev_device *udev_device)
return -1;
}
if ((stats.st_mode & S_IFMT) == S_IFLNK) {
- char target[NAME_SIZE];
+ char target[UTIL_PATH_SIZE];
int target_len;
info(udev_device->udev, "found a symlink as db file\n");
@@ -121,9 +120,9 @@ static int device_read_db(struct udev_device *udev_device)
device_set_devnum(udev_device, makedev(maj, min));
break;
case 'S':
- strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
- strlcat(filename, "/", sizeof(filename));
- strlcat(filename, val, sizeof(filename));
+ util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
+ util_strlcat(filename, "/", sizeof(filename));
+ util_strlcat(filename, val, sizeof(filename));
device_add_devlink(udev_device, filename);
break;
case 'L':
@@ -183,7 +182,7 @@ struct udev_device *device_init(struct udev *udev)
**/
struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
{
- char path[PATH_SIZE];
+ char path[UTIL_PATH_SIZE];
struct stat statbuf;
struct udev_device *udev_device;
@@ -192,8 +191,8 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
if (devpath == NULL)
return NULL;
- strlcpy(path, udev_get_sys_path(udev), sizeof(path));
- strlcat(path, devpath, sizeof(path));
+ util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+ util_strlcat(path, devpath, sizeof(path));
if (stat(path, &statbuf) != 0)
return NULL;
if (!S_ISDIR(statbuf.st_mode))
@@ -204,7 +203,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
return NULL;
/* resolve possible symlink to real path */
- strlcpy(path, devpath, sizeof(path));
+ util_strlcpy(path, devpath, sizeof(path));
util_resolve_sys_link(udev, path, sizeof(path));
device_set_devpath(udev_device, path);
info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
@@ -333,7 +332,7 @@ const char *udev_device_get_devname(struct udev_device *udev_device)
**/
const char *udev_device_get_subsystem(struct udev_device *udev_device)
{
- char subsystem[NAME_SIZE];
+ char subsystem[UTIL_PATH_SIZE];
if (udev_device == NULL)
return NULL;
@@ -363,7 +362,7 @@ int udev_device_get_devlinks(struct udev_device *udev_device,
int (*cb)(struct udev_device *udev_device, const char *value, void *data),
void *data)
{
- struct name_entry *name_loop;
+ struct util_name_entry *name_loop;
int count = 0;
if (udev_device == NULL)
@@ -393,17 +392,16 @@ int udev_device_get_properties(struct udev_device *udev_device,
int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
void *data)
{
- struct name_entry *name_loop;
+ struct util_name_entry *name_loop;
int count = 0;
if (udev_device == NULL)
return -1;
list_for_each_entry(name_loop, &udev_device->env_list, node) {
- char name[PATH_SIZE];
+ char name[UTIL_PATH_SIZE];
char *val;
- strncpy(name, name_loop->name, PATH_SIZE);
- name[PATH_SIZE-1] = '\0';
+ strncpy(name, name_loop->name, sizeof(name));
val = strchr(name, '=');
if (val == NULL)
continue;
@@ -418,7 +416,7 @@ int udev_device_get_properties(struct udev_device *udev_device,
const char *udev_device_get_driver(struct udev_device *udev_device)
{
- char driver[NAME_SIZE];
+ char driver[UTIL_PATH_SIZE];
if (udev_device == NULL)
return NULL;
diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c
index 72b97cebd7..9bd9101052 100644
--- a/udev/lib/libudev-enumerate.c
+++ b/udev/lib/libudev-enumerate.c
@@ -30,34 +30,33 @@
#include "libudev.h"
#include "libudev-private.h"
-#include "../udev.h"
static int devices_scan_subsystem(struct udev *udev,
const char *basedir, const char *subsystem, const char *subdir,
struct list_head *device_list)
{
- char path[PATH_SIZE];
+ char path[UTIL_PATH_SIZE];
DIR *dir;
struct dirent *dent;
size_t len;
- len = strlcpy(path, udev_get_sys_path(udev), sizeof(path));
- strlcat(path, basedir, sizeof(path));
- strlcat(path, "/", sizeof(path));
- strlcat(path, subsystem, sizeof(path));
+ len = util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+ util_strlcat(path, basedir, sizeof(path));
+ util_strlcat(path, "/", sizeof(path));
+ util_strlcat(path, subsystem, sizeof(path));
if (subdir != NULL)
- strlcat(path, subdir, sizeof(path));
+ util_strlcat(path, subdir, sizeof(path));
dir = opendir(path);
if (dir == NULL)
return -1;
for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
- char devpath[PATH_SIZE];
+ char devpath[UTIL_PATH_SIZE];
if (dent->d_name[0] == '.')
continue;
- strlcpy(devpath, &path[len], sizeof(devpath));
- strlcat(devpath, "/", sizeof(devpath));
- strlcat(devpath, dent->d_name, sizeof(devpath));
+ util_strlcpy(devpath, &path[len], sizeof(devpath));
+ util_strlcat(devpath, "/", sizeof(devpath));
+ util_strlcat(devpath, dent->d_name, sizeof(devpath));
util_resolve_sys_link(udev, devpath, sizeof(devpath));
util_name_list_add(udev, device_list, devpath, 1);
}
@@ -69,15 +68,15 @@ static int devices_scan_subsystems(struct udev *udev,
const char *basedir, const char *subsystem, const char *subdir,
struct list_head *device_list)
{
- char path[PATH_SIZE];
+ char path[UTIL_PATH_SIZE];
DIR *dir;
struct dirent *dent;
if (subsystem != NULL)
return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
- strlcpy(path, udev_get_sys_path(udev), sizeof(path));
- strlcat(path, basedir, sizeof(path));
+ util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+ util_strlcat(path, basedir, sizeof(path));
dir = opendir(path);
if (dir == NULL)
return -1;
@@ -115,7 +114,7 @@ static int devices_call(struct udev *udev, const char *devpath,
void *data,
int *cb_rc)
{
- char subsystem[NAME_SIZE];
+ char subsystem[UTIL_PATH_SIZE];
const char *name;
name = strrchr(devpath, '/');
@@ -147,19 +146,19 @@ int udev_enumerate_devices(struct udev *udev, const char *subsystem,
const char *devpath, const char *subsystem, const char *name, void *data),
void *data)
{
- char base[PATH_SIZE];
+ char base[UTIL_PATH_SIZE];
struct stat statbuf;
struct list_head device_list;
- struct name_entry *loop_device;
- struct name_entry *tmp_device;
+ struct util_name_entry *loop_device;
+ struct util_name_entry *tmp_device;
int cb_rc = 0;
int count = 0;
INIT_LIST_HEAD(&device_list);
/* if we have /sys/subsystem/, forget all the old stuff */
- strlcpy(base, udev_get_sys_path(udev), sizeof(base));
- strlcat(base, "/subsystem", sizeof(base));
+ util_strlcpy(base, udev_get_sys_path(udev), sizeof(base));
+ util_strlcat(base, "/subsystem", sizeof(base));
if (stat(base, &statbuf) == 0) {
devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
} else {
@@ -174,6 +173,7 @@ int udev_enumerate_devices(struct udev *udev, const char *subsystem,
if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
count++;
list_del(&loop_device->node);
+ free(loop_device->name);
free(loop_device);
}
@@ -183,6 +183,7 @@ int udev_enumerate_devices(struct udev *udev, const char *subsystem,
if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
count++;
list_del(&loop_device->node);
+ free(loop_device->name);
free(loop_device);
}
diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c
index 3e1731e382..60f7bc29e5 100644
--- a/udev/lib/libudev-monitor.c
+++ b/udev/lib/libudev-monitor.c
@@ -33,7 +33,6 @@
#include "libudev.h"
#include "libudev-private.h"
-#include "../udev.h"
struct udev_monitor {
struct udev *udev;
diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h
index a715067ebb..32837e368d 100644
--- a/udev/lib/libudev-private.h
+++ b/udev/lib/libudev-private.h
@@ -24,7 +24,7 @@
#include <syslog.h>
#include "libudev.h"
-#include "../udev.h"
+#include "list.h"
#ifdef USE_LOG
#ifdef USE_DEBUG
@@ -104,6 +104,13 @@ extern const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg);
/* libudev-utils */
+#define UTIL_PATH_SIZE 1024
+#define UTIL_LINE_SIZE 1024
+struct util_name_entry {
+ struct list_head node;
+ char *name;
+ int *i;
+};
extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
extern ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver, size_t size);
extern int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size);
@@ -114,4 +121,6 @@ extern int util_log_priority(const char *priority);
extern size_t util_path_encode(char *s, size_t len);
extern size_t util_path_decode(char *s);
extern void util_remove_trailing_chars(char *path, char c);
+extern size_t util_strlcpy(char *dst, const char *src, size_t size);
+extern size_t util_strlcat(char *dst, const char *src, size_t size);
#endif
diff --git a/udev/lib/libudev-util.c b/udev/lib/libudev-util.c
index dfd5f3c3fb..ae0adf4b88 100644
--- a/udev/lib/libudev-util.c
+++ b/udev/lib/libudev-util.c
@@ -30,18 +30,17 @@
#include "libudev.h"
#include "libudev-private.h"
-#include "../udev.h"
static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *devpath, char *subsystem, size_t size)
{
- char path[PATH_SIZE];
+ char path[UTIL_PATH_SIZE];
ssize_t len;
const char *pos;
- strlcpy(path, udev_get_sys_path(udev), sizeof(path));
- strlcat(path, devpath, sizeof(path));
- strlcat(path, "/", sizeof(path));
- strlcat(path, slink, sizeof(path));
+ util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
+ util_strlcat(path, devpath, sizeof(path));
+ util_strlcat(path, "/", sizeof(path));
+ util_strlcat(path, slink, sizeof(path));
len = readlink(path, path, sizeof(path));
if (len < 0 || len >= (ssize_t) sizeof(path))
return -1;
@@ -50,7 +49,7 @@ static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *de
if (pos == NULL)
return -1;
pos = &pos[1];
- return strlcpy(subsystem, pos, size);
+ return util_strlcpy(subsystem, pos, size);
}
ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size)
@@ -65,14 +64,15 @@ ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver
int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size)
{
- char link_path[PATH_SIZE];
- char link_target[PATH_SIZE];
+ char link_path[UTIL_PATH_SIZE];
+ char link_target[UTIL_PATH_SIZE];
+
int len;
int i;
int back;
- strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
- strlcat(link_path, devpath, sizeof(link_path));
+ util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
+ util_strlcat(link_path, devpath, sizeof(link_path));
len = readlink(link_path, link_target, sizeof(link_target));
if (len <= 0)
return -1;
@@ -90,17 +90,11 @@ int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size)
pos[0] = '\0';
}
dbg(udev, "after moving back '%s'\n", devpath);
- strlcat(devpath, "/", size);
- strlcat(devpath, &link_target[back * 3], size);
+ util_strlcat(devpath, "/", size);
+ util_strlcat(devpath, &link_target[back * 3], size);
return 0;
}
-struct util_name_entry {
- struct list_head node;
- char *name;
- int *i;
-};
-
struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list,
const char *name, int sort)
{
@@ -122,9 +116,15 @@ struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *
}
}
+ name_new = malloc(sizeof(struct util_name_entry));
+ if (name_new == NULL)
+ return NULL;
+ memset(name_new, 0x00, sizeof(struct util_name_entry));
name_new->name = strdup(name);
- if (name_new->name == NULL)
+ if (name_new->name == NULL) {
+ free(name_new);
return NULL;
+ }
dbg(udev, "adding '%s'\n", name_new->name);
list_add_tail(&name_new->node, &name_loop->node);
return name_new;
@@ -212,3 +212,46 @@ void util_remove_trailing_chars(char *path, char c)
while (len > 0 && path[len-1] == c)
path[--len] = '\0';
}
+
+size_t util_strlcpy(char *dst, const char *src, size_t size)
+{
+ size_t bytes = 0;
+ char *q = dst;
+ const char *p = src;
+ char ch;
+
+ while ((ch = *p++)) {
+ if (bytes+1 < size)
+ *q++ = ch;
+ bytes++;
+ }
+
+ /* If size == 0 there is no space for a final null... */
+ if (size)
+ *q = '\0';
+ return bytes;
+}
+
+size_t util_strlcat(char *dst, const char *src, size_t size)
+{
+ size_t bytes = 0;
+ char *q = dst;
+ const char *p = src;
+ char ch;
+
+ while (bytes < size && *q) {
+ q++;
+ bytes++;
+ }
+ if (bytes == size)
+ return (bytes + strlen(src));
+
+ while ((ch = *p++)) {
+ if (bytes+1 < size)
+ *q++ = ch;
+ bytes++;
+ }
+
+ *q = '\0';
+ return bytes;
+}
diff --git a/udev/lib/libudev.c b/udev/lib/libudev.c
index d1dd9e4838..04ad26c6fb 100644
--- a/udev/lib/libudev.c
+++ b/udev/lib/libudev.c
@@ -33,7 +33,6 @@
#include "libudev.h"
#include "libudev-private.h"
-#include "../udev.h"
struct udev {
int refcount;
@@ -201,7 +200,7 @@ struct udev *udev_new(void)
goto err;
f = fopen(config_file, "r");
if (f != NULL) {
- char line[LINE_SIZE];
+ char line[UTIL_LINE_SIZE];
int line_nr = 0;
while (fgets(line, sizeof(line), f)) {