2003-12-03 16:08:46 +07:00
|
|
|
/*
|
|
|
|
* udev_config.c
|
|
|
|
*
|
2004-01-27 10:21:58 +07:00
|
|
|
* Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
|
2005-07-22 23:35:58 +07:00
|
|
|
* Copyright (C) 2004, 2005 Kay Sievers <kay.sievers@vrfy.org>
|
2003-12-03 16:08:46 +07:00
|
|
|
*
|
|
|
|
* 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 <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2005-03-27 06:11:03 +07:00
|
|
|
#include <syslog.h>
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2004-02-24 10:07:25 +07:00
|
|
|
#include "libsysfs/sysfs/libsysfs.h"
|
2005-03-07 10:29:43 +07:00
|
|
|
#include "udev_libc_wrapper.h"
|
2003-12-03 16:08:46 +07:00
|
|
|
#include "udev.h"
|
2004-11-25 08:44:38 +07:00
|
|
|
#include "udev_utils.h"
|
2003-12-03 16:08:46 +07:00
|
|
|
#include "udev_version.h"
|
[PATCH] add udev logging to info log
On Thu, Jan 15, 2004 at 05:14:16AM +0100, Kay Sievers wrote:
> On Wed, Jan 14, 2004 at 01:10:43PM -0800, Greg KH wrote:
> > On Wed, Jan 14, 2004 at 02:34:26PM -0600, Clay Haapala wrote:
> > > On Wed, 14 Jan 2004, Chris Friesen spake thusly:
> > > >
> > > > Maybe for ones with a matching rule, you could print something like:
> > > >
> > > >
> > > Is the act of printing/syslogging a rule in an of itself?
> >
> > No, as currently the only way stuff ends up in the syslog is if
> > DEBUG=true is used on the build line.
> >
> > But it's sounding like we might want to change that... :)
>
> How about this in the syslog after connect/disconnect?
>
> Jan 15 05:07:45 pim udev[28007]: configured rule in '/etc/udev/udev.rules' at line 17 applied, 'video*' becomes 'video/webcam%n'
> Jan 15 05:07:45 pim udev[28007]: creating device node '/udev/video/webcam0'
> Jan 15 05:07:47 pim udev[28015]: removing device node '/udev/video/webcam0'
Here is a slightly better version. I've created a logging.h file and
moved the debug macros from udev.h in there.
If you type:
'make' - you will get a binary that prints one or two lines to syslog
if a device node is created or deleted
'make LOG=false' - you get a binary that prints asolutely nothing
'make DEBUG=true' - the same as today, it will print all debug lines
2004-01-16 12:53:20 +07:00
|
|
|
#include "logging.h"
|
2003-12-03 16:08:46 +07:00
|
|
|
|
|
|
|
/* global variables */
|
2005-03-07 10:29:43 +07:00
|
|
|
char sysfs_path[PATH_SIZE];
|
|
|
|
char udev_root[PATH_SIZE];
|
|
|
|
char udev_config_filename[PATH_SIZE];
|
2005-03-27 06:11:03 +07:00
|
|
|
char udev_rules_filename[PATH_SIZE];
|
|
|
|
int udev_log_priority;
|
2005-04-02 22:45:35 +07:00
|
|
|
int udev_run;
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2005-03-13 11:46:31 +07:00
|
|
|
static int get_key(char **line, char **key, char **value)
|
|
|
|
{
|
|
|
|
char *linepos;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
linepos = *line;
|
|
|
|
if (!linepos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get the key */
|
|
|
|
*key = linepos;
|
|
|
|
while (1) {
|
|
|
|
linepos++;
|
|
|
|
if (linepos[0] == '\0')
|
|
|
|
return -1;
|
|
|
|
if (isspace(linepos[0]))
|
|
|
|
break;
|
|
|
|
if (linepos[0] == '=')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* terminate key */
|
|
|
|
linepos[0] = '\0';
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* skip whitespace */
|
|
|
|
while (isspace(linepos[0]))
|
|
|
|
linepos++;
|
|
|
|
|
|
|
|
/* get the value*/
|
|
|
|
if (linepos[0] == '"')
|
|
|
|
linepos++;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
*value = linepos;
|
|
|
|
|
|
|
|
temp = strchr(linepos, '"');
|
|
|
|
if (!temp)
|
|
|
|
return -1;
|
|
|
|
temp[0] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-03 16:08:46 +07:00
|
|
|
static int parse_config_file(void)
|
|
|
|
{
|
2004-09-11 10:54:04 +07:00
|
|
|
char line[LINE_SIZE];
|
|
|
|
char *bufline;
|
2005-03-13 11:46:31 +07:00
|
|
|
char *linepos;
|
2003-12-03 16:08:46 +07:00
|
|
|
char *variable;
|
|
|
|
char *value;
|
2004-03-23 13:22:20 +07:00
|
|
|
char *buf;
|
|
|
|
size_t bufsize;
|
|
|
|
size_t cur;
|
|
|
|
size_t count;
|
|
|
|
int lineno;
|
2003-12-03 16:08:46 +07:00
|
|
|
int retval = 0;
|
2004-03-23 13:22:20 +07:00
|
|
|
|
2005-03-07 10:29:43 +07:00
|
|
|
if (file_map(udev_config_filename, &buf, &bufsize) != 0) {
|
2005-11-08 00:44:18 +07:00
|
|
|
err("can't open '%s' as config file: %s", udev_config_filename, strerror(errno));
|
2003-12-03 16:08:46 +07:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop through the whole file */
|
2004-03-23 13:22:20 +07:00
|
|
|
lineno = 0;
|
|
|
|
cur = 0;
|
2004-09-11 10:54:04 +07:00
|
|
|
while (cur < bufsize) {
|
2004-03-23 13:22:20 +07:00
|
|
|
count = buf_get_line(buf, bufsize, cur);
|
2004-09-11 10:54:04 +07:00
|
|
|
bufline = &buf[cur];
|
2004-03-23 13:22:20 +07:00
|
|
|
cur += count+1;
|
2004-09-11 10:54:04 +07:00
|
|
|
lineno++;
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2005-03-07 10:29:43 +07:00
|
|
|
if (count >= sizeof(line)) {
|
2005-03-27 06:11:03 +07:00
|
|
|
err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
|
2004-09-11 10:54:04 +07:00
|
|
|
continue;
|
|
|
|
}
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2004-09-11 10:54:04 +07:00
|
|
|
/* eat the whitespace */
|
2004-09-15 07:45:48 +07:00
|
|
|
while ((count > 0) && isspace(bufline[0])) {
|
2004-09-11 10:54:04 +07:00
|
|
|
bufline++;
|
|
|
|
count--;
|
|
|
|
}
|
2004-09-15 07:45:48 +07:00
|
|
|
if (count == 0)
|
|
|
|
continue;
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2003-12-03 16:08:46 +07:00
|
|
|
/* see if this is a comment */
|
2004-09-11 10:54:04 +07:00
|
|
|
if (bufline[0] == COMMENT_CHARACTER)
|
2003-12-03 16:08:46 +07:00
|
|
|
continue;
|
|
|
|
|
2005-08-08 07:21:55 +07:00
|
|
|
memcpy(line, bufline, count);
|
|
|
|
line[count] = '\0';
|
2004-09-11 10:54:04 +07:00
|
|
|
|
2005-03-13 11:46:31 +07:00
|
|
|
linepos = line;
|
|
|
|
retval = get_key(&linepos, &variable, &value);
|
|
|
|
if (retval != 0) {
|
2005-03-27 06:11:03 +07:00
|
|
|
err("error parsing %s, line %d:%d", udev_config_filename, lineno, (int) (linepos-line));
|
2005-03-13 11:46:31 +07:00
|
|
|
continue;
|
|
|
|
}
|
2004-04-24 11:50:27 +07:00
|
|
|
|
|
|
|
if (strcasecmp(variable, "udev_root") == 0) {
|
2005-03-07 10:29:43 +07:00
|
|
|
strlcpy(udev_root, value, sizeof(udev_root));
|
2005-08-29 04:15:51 +07:00
|
|
|
remove_trailing_chars(udev_root, '/');
|
2004-04-24 11:50:27 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(variable, "udev_rules") == 0) {
|
2005-03-07 10:29:43 +07:00
|
|
|
strlcpy(udev_rules_filename, value, sizeof(udev_rules_filename));
|
2005-08-29 04:15:51 +07:00
|
|
|
remove_trailing_chars(udev_rules_filename, '/');
|
2004-04-24 11:50:27 +07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(variable, "udev_log") == 0) {
|
2005-03-27 06:11:03 +07:00
|
|
|
udev_log_priority = log_priority(value);
|
2004-04-24 11:50:27 +07:00
|
|
|
continue;
|
|
|
|
}
|
2003-12-03 16:08:46 +07:00
|
|
|
}
|
2004-03-23 13:22:20 +07:00
|
|
|
|
|
|
|
file_unmap(buf, bufsize);
|
2003-12-03 16:08:46 +07:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2005-03-13 11:46:31 +07:00
|
|
|
void udev_init_config(void)
|
2003-12-03 16:08:46 +07:00
|
|
|
{
|
2005-03-27 06:11:03 +07:00
|
|
|
const char *env;
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2005-03-27 06:11:03 +07:00
|
|
|
strcpy(udev_root, UDEV_ROOT);
|
|
|
|
strcpy(udev_config_filename, UDEV_CONFIG_FILE);
|
|
|
|
strcpy(udev_rules_filename, UDEV_RULES_FILE);
|
|
|
|
udev_log_priority = LOG_ERR;
|
2005-04-02 22:45:35 +07:00
|
|
|
udev_run = 1;
|
2005-03-13 11:46:31 +07:00
|
|
|
sysfs_get_mnt_path(sysfs_path, sizeof(sysfs_path));
|
2003-12-03 16:08:46 +07:00
|
|
|
|
2005-04-02 22:45:35 +07:00
|
|
|
/* disable RUN key execution */
|
|
|
|
env = getenv("UDEV_RUN");
|
|
|
|
if (env && !string_is_true(env))
|
|
|
|
udev_run = 0;
|
|
|
|
|
2005-03-27 06:11:03 +07:00
|
|
|
env = getenv("UDEV_CONFIG_FILE");
|
|
|
|
if (env) {
|
|
|
|
strlcpy(udev_config_filename, env, sizeof(udev_config_filename));
|
2005-08-29 04:15:51 +07:00
|
|
|
remove_trailing_chars(udev_config_filename, '/');
|
2005-03-27 06:11:03 +07:00
|
|
|
}
|
2003-12-03 16:08:46 +07:00
|
|
|
|
|
|
|
parse_config_file();
|
2005-03-27 06:11:03 +07:00
|
|
|
|
2005-11-26 00:56:06 +07:00
|
|
|
env = getenv("UDEV_ROOT");
|
|
|
|
if (env) {
|
|
|
|
strlcpy(udev_root, env, sizeof(udev_root));
|
|
|
|
remove_trailing_chars(udev_root, '/');
|
|
|
|
}
|
|
|
|
|
2005-03-27 06:11:03 +07:00
|
|
|
env = getenv("UDEV_LOG");
|
|
|
|
if (env)
|
|
|
|
udev_log_priority = log_priority(env);
|
|
|
|
|
2004-12-20 13:38:33 +07:00
|
|
|
dbg("sysfs_path='%s'", sysfs_path);
|
2005-03-27 06:11:03 +07:00
|
|
|
dbg("UDEV_CONFIG_FILE='%s'", udev_config_filename);
|
2004-12-20 13:38:33 +07:00
|
|
|
dbg("udev_root='%s'", udev_root);
|
2005-03-27 06:11:03 +07:00
|
|
|
dbg("udev_rules='%s'", udev_rules_filename);
|
|
|
|
dbg("udev_log=%d", udev_log_priority);
|
2003-12-03 16:08:46 +07:00
|
|
|
}
|