[PATCH] replace fgets() with mmap() and introduce udev_lib.[hc]
Here we replace the various fgets() with a mmap() call for the config file reading, due to the reported performance problems with klibc. Thanks to Patrick's testing, it makes a very small, close to nothing speed gain for libc users, but a 6 times speed increase for klibc users with a 1000 line config file. I've created a udev_lib.[hc] for this and also moved all the generic stuff from udev.h in there and uninlined the functions.
This commit is contained in:
parent
c58286656e
commit
c81b35c08b
8
Makefile
8
Makefile
|
@ -201,7 +201,8 @@ SYSFS = $(PWD)/libsysfs/sysfs_bus.o \
|
||||||
$(PWD)/libsysfs/sysfs_utils.o \
|
$(PWD)/libsysfs/sysfs_utils.o \
|
||||||
$(PWD)/libsysfs/dlist.o
|
$(PWD)/libsysfs/dlist.o
|
||||||
|
|
||||||
OBJS = udev_config.o \
|
OBJS = udev_lib.o \
|
||||||
|
udev_config.o \
|
||||||
udev-add.o \
|
udev-add.o \
|
||||||
udev-remove.o \
|
udev-remove.o \
|
||||||
udevdb.o \
|
udevdb.o \
|
||||||
|
@ -211,6 +212,7 @@ OBJS = udev_config.o \
|
||||||
$(TDB)
|
$(TDB)
|
||||||
|
|
||||||
HEADERS = udev.h \
|
HEADERS = udev.h \
|
||||||
|
udev_lib.h \
|
||||||
namedev.h \
|
namedev.h \
|
||||||
udev_version.h \
|
udev_version.h \
|
||||||
udev_dbus.h \
|
udev_dbus.h \
|
||||||
|
@ -279,7 +281,7 @@ $(TESTER): $(TESTER).o $(OBJS) $(HEADERS) $(LIBC)
|
||||||
$(STRIPCMD) $@
|
$(STRIPCMD) $@
|
||||||
|
|
||||||
$(INFO): $(INFO).o $(OBJS) $(HEADERS) $(LIBC)
|
$(INFO): $(INFO).o $(OBJS) $(HEADERS) $(LIBC)
|
||||||
$(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_config.o udevdb.o $(SYSFS) $(TDB) $(LIB_OBJS) $(ARCH_LIB_OBJS)
|
$(LD) $(LDFLAGS) -o $@ $(CRT0) udevinfo.o udev_lib.o udev_config.o udevdb.o $(SYSFS) $(TDB) $(LIB_OBJS) $(ARCH_LIB_OBJS)
|
||||||
$(STRIPCMD) $@
|
$(STRIPCMD) $@
|
||||||
|
|
||||||
$(DAEMON): $(DAEMON).o udevd.h $(LIBC)
|
$(DAEMON): $(DAEMON).o udevd.h $(LIBC)
|
||||||
|
@ -287,7 +289,7 @@ $(DAEMON): $(DAEMON).o udevd.h $(LIBC)
|
||||||
$(STRIPCMD) $@
|
$(STRIPCMD) $@
|
||||||
|
|
||||||
$(SENDER): $(SENDER).o udevd.h $(LIBC)
|
$(SENDER): $(SENDER).o udevd.h $(LIBC)
|
||||||
$(LD) $(LDFLAGS) -o $@ $(CRT0) udevsend.o $(LIB_OBJS) $(ARCH_LIB_OBJS)
|
$(LD) $(LDFLAGS) -o $@ $(CRT0) udevsend.o udev_lib.o $(LIB_OBJS) $(ARCH_LIB_OBJS)
|
||||||
$(STRIPCMD) $@
|
$(STRIPCMD) $@
|
||||||
|
|
||||||
$(STARTER): $(STARTER).o $(HEADERS) $(LIBC)
|
$(STARTER): $(STARTER).o $(HEADERS) $(LIBC)
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "udev.h"
|
||||||
#include "klibc_fixups.h"
|
#include "klibc_fixups.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
|
@ -40,22 +41,35 @@
|
||||||
static unsigned long get_id_by_name(const char *uname, const char *dbfile)
|
static unsigned long get_id_by_name(const char *uname, const char *dbfile)
|
||||||
{
|
{
|
||||||
unsigned long id = -1;
|
unsigned long id = -1;
|
||||||
FILE *file;
|
char line[255];
|
||||||
char buf[255];
|
char *buf;
|
||||||
|
size_t bufsize;
|
||||||
|
size_t cur;
|
||||||
|
size_t count;
|
||||||
char *pos;
|
char *pos;
|
||||||
char *name;
|
char *name;
|
||||||
char *idstr;
|
char *idstr;
|
||||||
char *tail;
|
char *tail;
|
||||||
|
|
||||||
file = fopen(dbfile, "r");
|
if (file_map(dbfile, &buf, &bufsize) == 0) {
|
||||||
if (file == NULL) {
|
dbg("reading '%s' as db file", dbfile);
|
||||||
dbg("unable to open file '%s'", dbfile);
|
} else {
|
||||||
|
dbg("can't open '%s' as db file", dbfile);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* loop through the whole file */
|
||||||
|
|
||||||
|
cur = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
pos = fgets(buf, sizeof(buf), file);
|
count = buf_get_line(buf, bufsize, cur);
|
||||||
if (pos == NULL)
|
|
||||||
|
strncpy(line, buf + cur, count);
|
||||||
|
line[count] = '\0';
|
||||||
|
pos = line;
|
||||||
|
|
||||||
|
cur += count+1;
|
||||||
|
if (cur > bufsize)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* get name */
|
/* get name */
|
||||||
|
@ -82,7 +96,7 @@ static unsigned long get_id_by_name(const char *uname, const char *dbfile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
file_unmap(buf, bufsize);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "namedev.h"
|
#include "namedev.h"
|
||||||
|
|
|
@ -30,7 +30,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -38,6 +37,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "namedev.h"
|
#include "namedev.h"
|
||||||
|
|
||||||
|
@ -153,27 +153,36 @@ static int namedev_parse_rules(char *filename)
|
||||||
char *temp2;
|
char *temp2;
|
||||||
char *temp3;
|
char *temp3;
|
||||||
char *attr;
|
char *attr;
|
||||||
FILE *fd;
|
char *buf;
|
||||||
|
size_t bufsize;
|
||||||
|
size_t cur;
|
||||||
|
size_t count;
|
||||||
int program_given = 0;
|
int program_given = 0;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
struct config_device dev;
|
struct config_device dev;
|
||||||
|
|
||||||
fd = fopen(filename, "r");
|
if (file_map(filename, &buf, &bufsize) == 0) {
|
||||||
if (fd != NULL) {
|
|
||||||
dbg("reading '%s' as rules file", filename);
|
dbg("reading '%s' as rules file", filename);
|
||||||
} else {
|
} else {
|
||||||
dbg("can't open '%s' as a rules file", filename);
|
dbg("can't open '%s' as rules file", filename);
|
||||||
return -ENODEV;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* loop through the whole file */
|
/* loop through the whole file */
|
||||||
|
cur = 0;
|
||||||
lineno = 0;
|
lineno = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
/* get a line */
|
count = buf_get_line(buf, bufsize, cur);
|
||||||
temp = fgets(line, sizeof(line), fd);
|
|
||||||
if (temp == NULL)
|
strncpy(line, buf + cur, count);
|
||||||
goto exit;
|
line[count] = '\0';
|
||||||
|
temp = line;
|
||||||
lineno++;
|
lineno++;
|
||||||
|
|
||||||
|
cur += count+1;
|
||||||
|
if (cur > bufsize)
|
||||||
|
break;
|
||||||
|
|
||||||
dbg_parse("read '%s'", temp);
|
dbg_parse("read '%s'", temp);
|
||||||
|
|
||||||
/* eat the whitespace */
|
/* eat the whitespace */
|
||||||
|
@ -311,8 +320,8 @@ error:
|
||||||
filename, lineno, temp - line);
|
filename, lineno, temp - line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exit:
|
|
||||||
fclose(fd);
|
file_unmap(buf, bufsize);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,22 +330,31 @@ static int namedev_parse_permissions(char *filename)
|
||||||
char line[255];
|
char line[255];
|
||||||
char *temp;
|
char *temp;
|
||||||
char *temp2;
|
char *temp2;
|
||||||
FILE *fd;
|
char *buf;
|
||||||
|
size_t bufsize;
|
||||||
|
size_t cur;
|
||||||
|
size_t count;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
struct perm_device dev;
|
struct perm_device dev;
|
||||||
|
|
||||||
fd = fopen(filename, "r");
|
if (file_map(filename, &buf, &bufsize) == 0) {
|
||||||
if (fd != NULL) {
|
|
||||||
dbg("reading '%s' as permissions file", filename);
|
dbg("reading '%s' as permissions file", filename);
|
||||||
} else {
|
} else {
|
||||||
dbg("can't open '%s' as permissions file", filename);
|
dbg("can't open '%s' as permissions file", filename);
|
||||||
return -ENODEV;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* loop through the whole file */
|
/* loop through the whole file */
|
||||||
|
cur = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = fgets(line, sizeof(line), fd);
|
count = buf_get_line(buf, bufsize, cur);
|
||||||
if (temp == NULL)
|
|
||||||
|
strncpy(line, buf + cur, count);
|
||||||
|
line[count] = '\0';
|
||||||
|
temp = line;
|
||||||
|
|
||||||
|
cur += count+1;
|
||||||
|
if (cur > bufsize)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
dbg_parse("read '%s'", temp);
|
dbg_parse("read '%s'", temp);
|
||||||
|
@ -394,7 +412,7 @@ static int namedev_parse_permissions(char *filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
fclose(fd);
|
file_unmap(buf, bufsize);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "udev_dbus.h"
|
#include "udev_dbus.h"
|
||||||
#include "udev_selinux.h"
|
#include "udev_selinux.h"
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "udev_dbus.h"
|
#include "udev_dbus.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
6
udev.c
6
udev.c
|
@ -20,15 +20,17 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "udev_dbus.h"
|
#include "udev_dbus.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
104
udev.h
104
udev.h
|
@ -20,25 +20,22 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef UDEV_H
|
#ifndef _UDEV_H_
|
||||||
#define UDEV_H
|
#define _UDEV_H_
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sysfs/libsysfs.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
|
|
||||||
#define COMMENT_CHARACTER '#'
|
#define COMMENT_CHARACTER '#'
|
||||||
|
|
||||||
#define NAME_SIZE 100
|
#define NAME_SIZE 100
|
||||||
#define OWNER_SIZE 30
|
#define OWNER_SIZE 30
|
||||||
#define GROUP_SIZE 30
|
#define GROUP_SIZE 30
|
||||||
#define MODE_SIZE 8
|
#define MODE_SIZE 8
|
||||||
|
|
||||||
#define ACTION_SIZE 30
|
#define ACTION_SIZE 30
|
||||||
#define DEVPATH_SIZE 255
|
#define DEVPATH_SIZE 255
|
||||||
#define SUBSYSTEM_SIZE 30
|
#define SUBSYSTEM_SIZE 30
|
||||||
|
|
||||||
/* length of public data */
|
/* length of public data */
|
||||||
#define UDEVICE_LEN (offsetof(struct udevice, bus_id))
|
#define UDEVICE_LEN (offsetof(struct udevice, bus_id))
|
||||||
|
@ -61,87 +58,6 @@ struct udevice {
|
||||||
char kernel_name[NAME_SIZE];
|
char kernel_name[NAME_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
#define strfieldcpy(to, from) \
|
|
||||||
do { \
|
|
||||||
to[sizeof(to)-1] = '\0'; \
|
|
||||||
strncpy(to, from, sizeof(to)-1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define strfieldcat(to, from) \
|
|
||||||
do { \
|
|
||||||
to[sizeof(to)-1] = '\0'; \
|
|
||||||
strncat(to, from, sizeof(to) - strlen(to)-1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define strfieldcpymax(to, from, maxsize) \
|
|
||||||
do { \
|
|
||||||
to[maxsize-1] = '\0'; \
|
|
||||||
strncpy(to, from, maxsize-1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define strfieldcatmax(to, from, maxsize) \
|
|
||||||
do { \
|
|
||||||
to[maxsize-1] = '\0'; \
|
|
||||||
strncat(to, from, maxsize - strlen(to)-1); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define strintcat(to, i) \
|
|
||||||
do { \
|
|
||||||
to[sizeof(to)-1] = '\0'; \
|
|
||||||
snprintf((to) + strlen(to), sizeof(to) - strlen(to)-1, "%u", i); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define strintcatmax(to, i, maxsize) \
|
|
||||||
do { \
|
|
||||||
to[maxsize-1] = '\0'; \
|
|
||||||
snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define foreach_strpart(str, separator, pos, len) \
|
|
||||||
for(pos = str, len = 0; \
|
|
||||||
(pos) < ((str) + strlen(str)); \
|
|
||||||
pos = pos + len + strspn(pos, separator), len = strcspn(pos, separator)) \
|
|
||||||
if (len > 0)
|
|
||||||
|
|
||||||
static inline char *get_action(void)
|
|
||||||
{
|
|
||||||
char *action;
|
|
||||||
|
|
||||||
action = getenv("ACTION");
|
|
||||||
if (action != NULL && strlen(action) > ACTION_SIZE)
|
|
||||||
action[ACTION_SIZE-1] = '\0';
|
|
||||||
|
|
||||||
return action;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline char *get_devpath(void)
|
|
||||||
{
|
|
||||||
char *devpath;
|
|
||||||
|
|
||||||
devpath = getenv("DEVPATH");
|
|
||||||
if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
|
|
||||||
devpath[DEVPATH_SIZE-1] = '\0';
|
|
||||||
|
|
||||||
return devpath;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline char *get_seqnum(void)
|
|
||||||
{
|
|
||||||
char *seqnum;
|
|
||||||
|
|
||||||
seqnum = getenv("SEQNUM");
|
|
||||||
|
|
||||||
return seqnum;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline char *get_subsystem(char *subsystem)
|
|
||||||
{
|
|
||||||
if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
|
|
||||||
subsystem[SUBSYSTEM_SIZE-1] = '\0';
|
|
||||||
|
|
||||||
return subsystem;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern int udev_add_device(char *path, char *subsystem, int fake);
|
extern int udev_add_device(char *path, char *subsystem, int fake);
|
||||||
extern int udev_remove_device(char *path, char *subsystem);
|
extern int udev_remove_device(char *path, char *subsystem);
|
||||||
extern void udev_init_config(void);
|
extern void udev_init_config(void);
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "namedev.h"
|
#include "namedev.h"
|
||||||
|
@ -131,12 +132,14 @@ static int parse_config_file(void)
|
||||||
char *temp;
|
char *temp;
|
||||||
char *variable;
|
char *variable;
|
||||||
char *value;
|
char *value;
|
||||||
FILE *fd;
|
char *buf;
|
||||||
int lineno = 0;
|
size_t bufsize;
|
||||||
|
size_t cur;
|
||||||
|
size_t count;
|
||||||
|
int lineno;
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
fd = fopen(udev_config_filename, "r");
|
if (file_map(udev_config_filename, &buf, &bufsize) == 0) {
|
||||||
if (fd != NULL) {
|
|
||||||
dbg("reading '%s' as config file", udev_config_filename);
|
dbg("reading '%s' as config file", udev_config_filename);
|
||||||
} else {
|
} else {
|
||||||
dbg("can't open '%s' as config file", udev_config_filename);
|
dbg("can't open '%s' as config file", udev_config_filename);
|
||||||
|
@ -144,13 +147,20 @@ static int parse_config_file(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* loop through the whole file */
|
/* loop through the whole file */
|
||||||
|
lineno = 0;
|
||||||
|
cur = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
/* get a line */
|
count = buf_get_line(buf, bufsize, cur);
|
||||||
temp = fgets(line, sizeof(line), fd);
|
|
||||||
if (temp == NULL)
|
strncpy(line, buf + cur, count);
|
||||||
goto exit;
|
line[count] = '\0';
|
||||||
|
temp = line;
|
||||||
lineno++;
|
lineno++;
|
||||||
|
|
||||||
|
cur += count+1;
|
||||||
|
if (cur > bufsize)
|
||||||
|
break;
|
||||||
|
|
||||||
dbg_parse("read '%s'", temp);
|
dbg_parse("read '%s'", temp);
|
||||||
|
|
||||||
/* eat the whitespace at the beginning of the line */
|
/* eat the whitespace at the beginning of the line */
|
||||||
|
@ -182,8 +192,8 @@ static int parse_config_file(void)
|
||||||
}
|
}
|
||||||
dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
|
dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
|
||||||
lineno, temp - line, temp);
|
lineno, temp - line, temp);
|
||||||
exit:
|
|
||||||
fclose(fd);
|
file_unmap(buf, bufsize);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
111
udev_lib.c
Normal file
111
udev_lib.c
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/*
|
||||||
|
* udev_lib - generic stuff used by udev
|
||||||
|
*
|
||||||
|
* Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation version 2 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
|
|
||||||
|
|
||||||
|
char *get_action(void)
|
||||||
|
{
|
||||||
|
char *action;
|
||||||
|
|
||||||
|
action = getenv("ACTION");
|
||||||
|
if (action != NULL && strlen(action) > ACTION_SIZE)
|
||||||
|
action[ACTION_SIZE-1] = '\0';
|
||||||
|
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_devpath(void)
|
||||||
|
{
|
||||||
|
char *devpath;
|
||||||
|
|
||||||
|
devpath = getenv("DEVPATH");
|
||||||
|
if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
|
||||||
|
devpath[DEVPATH_SIZE-1] = '\0';
|
||||||
|
|
||||||
|
return devpath;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_seqnum(void)
|
||||||
|
{
|
||||||
|
char *seqnum;
|
||||||
|
|
||||||
|
seqnum = getenv("SEQNUM");
|
||||||
|
|
||||||
|
return seqnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_subsystem(char *subsystem)
|
||||||
|
{
|
||||||
|
if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
|
||||||
|
subsystem[SUBSYSTEM_SIZE-1] = '\0';
|
||||||
|
|
||||||
|
return subsystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
int file_map(const char *filename, char **buf, size_t *bufsize)
|
||||||
|
{
|
||||||
|
struct stat stats;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
fd = open(filename, O_RDONLY);
|
||||||
|
if (fd < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fstat(fd, &stats) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
if (*buf == MAP_FAILED) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*bufsize = stats.st_size;
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void file_unmap(char *buf, size_t bufsize)
|
||||||
|
{
|
||||||
|
munmap(buf, bufsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t buf_get_line(char *buf, size_t buflen, size_t cur)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
|
||||||
|
for (count = cur; count < buflen && buf[count] != '\n'; count++);
|
||||||
|
|
||||||
|
return count - cur;
|
||||||
|
}
|
||||||
|
|
78
udev_lib.h
Normal file
78
udev_lib.h
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* udev_lib - generic stuff used by udev
|
||||||
|
*
|
||||||
|
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation version 2 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _UDEV_LIB_H_
|
||||||
|
#define _UDEV_LIB_H_
|
||||||
|
|
||||||
|
|
||||||
|
#define strfieldcpy(to, from) \
|
||||||
|
do { \
|
||||||
|
to[sizeof(to)-1] = '\0'; \
|
||||||
|
strncpy(to, from, sizeof(to)-1); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define strfieldcat(to, from) \
|
||||||
|
do { \
|
||||||
|
to[sizeof(to)-1] = '\0'; \
|
||||||
|
strncat(to, from, sizeof(to) - strlen(to)-1); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define strfieldcpymax(to, from, maxsize) \
|
||||||
|
do { \
|
||||||
|
to[maxsize-1] = '\0'; \
|
||||||
|
strncpy(to, from, maxsize-1); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define strfieldcatmax(to, from, maxsize) \
|
||||||
|
do { \
|
||||||
|
to[maxsize-1] = '\0'; \
|
||||||
|
strncat(to, from, maxsize - strlen(to)-1); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define strintcat(to, i) \
|
||||||
|
do { \
|
||||||
|
to[sizeof(to)-1] = '\0'; \
|
||||||
|
snprintf((to) + strlen(to), sizeof(to) - strlen(to)-1, "%u", i); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define strintcatmax(to, i, maxsize) \
|
||||||
|
do { \
|
||||||
|
to[maxsize-1] = '\0'; \
|
||||||
|
snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define foreach_strpart(str, separator, pos, len) \
|
||||||
|
for(pos = str, len = 0; \
|
||||||
|
(pos) < ((str) + strlen(str)); \
|
||||||
|
pos = pos + len + strspn(pos, separator), len = strcspn(pos, separator)) \
|
||||||
|
if (len > 0)
|
||||||
|
|
||||||
|
|
||||||
|
extern char *get_action(void);
|
||||||
|
extern char *get_devpath(void);
|
||||||
|
extern char *get_seqnum(void);
|
||||||
|
extern char *get_subsystem(char *subsystem);
|
||||||
|
extern int file_map(const char *filename, char **buf, size_t *bufsize);
|
||||||
|
extern void file_unmap(char *buf, size_t bufsize);
|
||||||
|
extern size_t buf_get_line(char *buf, size_t buflen, size_t cur);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
1
udevd.c
1
udevd.c
|
@ -36,6 +36,7 @@
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "udevd.h"
|
#include "udevd.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
9
udevdb.c
9
udevdb.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* udevdb.c
|
* udevdb.c - udev database library
|
||||||
*
|
*
|
||||||
* Userspace devfs
|
* Userspace devfs
|
||||||
*
|
*
|
||||||
|
@ -21,13 +21,11 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
* udev database library
|
|
||||||
*/
|
|
||||||
#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
|
#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
@ -35,8 +33,9 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "udev_version.h"
|
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
|
#include "udev_version.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "namedev.h"
|
#include "namedev.h"
|
||||||
#include "udevdb.h"
|
#include "udevdb.h"
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "libsysfs/sysfs/libsysfs.h"
|
#include "libsysfs/sysfs/libsysfs.h"
|
||||||
#include "libsysfs/dlist.h"
|
#include "libsysfs/dlist.h"
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "udevdb.h"
|
#include "udevdb.h"
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include <linux/stddef.h>
|
#include <linux/stddef.h>
|
||||||
|
|
||||||
#include "udev.h"
|
#include "udev.h"
|
||||||
|
#include "udev_lib.h"
|
||||||
#include "udev_version.h"
|
#include "udev_version.h"
|
||||||
#include "udevd.h"
|
#include "udevd.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
Loading…
Reference in a new issue