Implement a two new flags, and change the meaning of another one.

'd': now means don't do daemon().
'D': Debug
'n': Don't wait to process all pending events before calling daemon.

In the past, devd would call daemon immediately.  However, this causes
a race.  If anything in the boot process depends on configuring the
devices configured by devd, maybe they would be configured in time,
maybe not.  Now we don't call daemon until all pending events are
processed, unless -n is specified.

# -n is actually the default for a while due to the select(2) bug in devctl
# that I just fixed to give people a chance to upgrade.
This commit is contained in:
Warner Losh 2003-04-21 06:26:08 +00:00
parent e22b0bf4b8
commit 5a882d775d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=113790
2 changed files with 45 additions and 14 deletions

View File

@ -33,7 +33,7 @@
.Nd "device state change daemon" .Nd "device state change daemon"
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl d .Op Fl Ddn
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm .Nm
@ -42,9 +42,13 @@ kernel events happen.
.Pp .Pp
The following options are accepted. The following options are accepted.
.Bl -tag -width indent .Bl -tag -width indent
.It Fl D
Enable debugging messages.
.It Fl d .It Fl d
Enable debugging messages and run in the foreground instead of Run in the foreground instead of becoming a daemon.
becoming a daemon. .It Fl n
Don't process all pending events before becoming a daemon.
Instaed, call daemon right away.
.El .El
.Sh IMPLEMENTATION NOTES .Sh IMPLEMENTATION NOTES
The The

View File

@ -70,7 +70,9 @@ static const char nomatch = '?';
static const char attach = '+'; static const char attach = '+';
static const char detach = '-'; static const char detach = '-';
int Dflag;
int dflag; int dflag;
int nflag = 1;
int romeo_must_die = 0; int romeo_must_die = 0;
static void event_loop(void); static void event_loop(void);
@ -245,7 +247,7 @@ bool
action::do_action(config &c) action::do_action(config &c)
{ {
string s = c.expand_string(_cmd); string s = c.expand_string(_cmd);
if (dflag) if (Dflag)
fprintf(stderr, "Executing '%s'\n", s.c_str()); fprintf(stderr, "Executing '%s'\n", s.c_str());
::system(s.c_str()); ::system(s.c_str());
return (true); return (true);
@ -272,7 +274,7 @@ match::do_match(config &c)
string value = c.get_variable(_var); string value = c.get_variable(_var);
bool retval; bool retval;
if (dflag) if (Dflag)
fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(), fprintf(stderr, "Testing %s=%s against %s\n", _var.c_str(),
value.c_str(), _re.c_str()); value.c_str(), _re.c_str());
@ -303,7 +305,7 @@ var_list::is_set(const string &var) const
void void
var_list::set_variable(const string &var, const string &val) var_list::set_variable(const string &var, const string &val)
{ {
if (dflag) if (Dflag)
fprintf(stderr, "%s=%s\n", var.c_str(), val.c_str()); fprintf(stderr, "%s=%s\n", var.c_str(), val.c_str());
_vars[var] = val; _vars[var] = val;
} }
@ -321,7 +323,7 @@ config::reset(void)
void void
config::parse_one_file(const char *fn) config::parse_one_file(const char *fn)
{ {
if (dflag) if (Dflag)
printf("Parsing %s\n", fn); printf("Parsing %s\n", fn);
yyin = fopen(fn, "r"); yyin = fopen(fn, "r");
if (yyin == NULL) if (yyin == NULL)
@ -338,7 +340,7 @@ config::parse_files_in_dir(const char *dirname)
struct dirent *dp; struct dirent *dp;
char path[PATH_MAX]; char path[PATH_MAX];
if (dflag) if (Dflag)
printf("Parsing files in %s\n", dirname); printf("Parsing files in %s\n", dirname);
dirp = opendir(dirname); dirp = opendir(dirname);
if (dirp == NULL) if (dirp == NULL)
@ -435,7 +437,7 @@ config::push_var_table()
vl = new var_list(); vl = new var_list();
_var_list_table.push_back(vl); _var_list_table.push_back(vl);
if (dflag) if (Dflag)
fprintf(stderr, "Pushing table\n"); fprintf(stderr, "Pushing table\n");
} }
@ -444,7 +446,7 @@ config::pop_var_table()
{ {
delete _var_list_table.back(); delete _var_list_table.back();
_var_list_table.pop_back(); _var_list_table.pop_back();
if (dflag) if (Dflag)
fprintf(stderr, "Popping table\n"); fprintf(stderr, "Popping table\n");
} }
@ -618,7 +620,7 @@ config::find_and_execute(char type)
s = "detach"; s = "detach";
break; break;
} }
if (dflag) if (Dflag)
fprintf(stderr, "Processing %s event\n", s); fprintf(stderr, "Processing %s event\n", s);
for (i = l->begin(); i != l->end(); i++) { for (i = l->begin(); i != l->end(); i++) {
if ((*i)->matches(*this)) { if ((*i)->matches(*this)) {
@ -637,7 +639,7 @@ process_event(char *buffer)
char *sp; char *sp;
sp = buffer + 1; sp = buffer + 1;
if (dflag) if (Dflag)
fprintf(stderr, "Processing event '%s'\n", buffer); fprintf(stderr, "Processing event '%s'\n", buffer);
type = *buffer++; type = *buffer++;
cfg.push_var_table(); cfg.push_var_table();
@ -674,6 +676,9 @@ event_loop(void)
int rv; int rv;
int fd; int fd;
char buffer[DEVCTL_MAXBUF]; char buffer[DEVCTL_MAXBUF];
int once = 0;
timeval tv;
fd_set fds;
fd = open(PATH_DEVCTL, O_RDONLY); fd = open(PATH_DEVCTL, O_RDONLY);
if (fd == -1) if (fd == -1)
@ -683,6 +688,22 @@ event_loop(void)
while (1) { while (1) {
if (romeo_must_die) if (romeo_must_die)
break; break;
if (!once && !dflag && !nflag) {
// Check to see if we have any events pending.
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
rv = select(fd + 1, &fds, &fds, &fds, &tv);
// No events -> we've processed all pending events
fprintf(stderr, "Select returns %d\n", rv);
if (rv == 0) {
if (Dflag)
fprintf(stderr, "Calling daemon\n");
daemon(0, 0);
once++;
}
}
rv = read(fd, buffer, sizeof(buffer) - 1); rv = read(fd, buffer, sizeof(buffer) - 1);
if (rv > 0) { if (rv > 0) {
buffer[rv] = '\0'; buffer[rv] = '\0';
@ -810,18 +831,24 @@ main(int argc, char **argv)
int ch; int ch;
check_devd_enabled(); check_devd_enabled();
while ((ch = getopt(argc, argv, "d")) != -1) { while ((ch = getopt(argc, argv, "Ddn")) != -1) {
switch (ch) { switch (ch) {
case 'D':
Dflag++;
break;
case 'd': case 'd':
dflag++; dflag++;
break; break;
case 'n':
nflag++;
break;
default: default:
usage(); usage();
} }
} }
cfg.parse(); cfg.parse();
if (!dflag) if (!dflag && nflag)
daemon(0, 0); daemon(0, 0);
cfg.drop_pidfile(); cfg.drop_pidfile();
signal(SIGHUP, gensighand); signal(SIGHUP, gensighand);