Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor llist_add out of last and modprobe #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void llist_free_arg(void *node);
void llist_free_double(void *node);
void llist_traverse(void *list, void (*using)(void *node));
void *llist_pop(void *list); // actually void **list
void llist_add(struct arg_list **old, void *data);
void *dlist_pop(void *list); // actually struct double_list **list
void *dlist_lpop(void *list); // also struct double_list **list
void dlist_add_nomalloc(struct double_list **list, struct double_list *new);
Expand Down
10 changes: 10 additions & 0 deletions lib/llist.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ void *llist_pop(void *list)
return (void *)next;
}

// Add new node at the beginning of the list.
void llist_add(struct arg_list **old, void *data)
{
struct arg_list *new = xmalloc(sizeof(struct arg_list));

new->arg = (char*)data;
new->next = *old;
*old = new;
}

// Remove first item from &list and return it
void *dlist_pop(void *list)
{
Expand Down
13 changes: 2 additions & 11 deletions toys/pending/last.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ static void free_list()
}
}

static void llist_add_node(struct arg_list **old, void *data)
{
struct arg_list *new = xmalloc(sizeof(struct arg_list));

new->arg = (char*)data;
new->next = *old;
*old = new;
}

// Find a node and dlink it from the list.
static struct arg_list *find_and_dlink(struct arg_list **list, char *devname)
{
Expand Down Expand Up @@ -178,9 +169,9 @@ void last_main(void)
ut.ut_line, pwidth, pwidth, ut.ut_host,
toybuf, toybuf+18, toybuf+28);
}
llist_add_node(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
llist_add(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
} else if (ut.ut_type == DEAD_PROCESS && *ut.ut_line)
llist_add_node(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
llist_add(&TT.list, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));

loc -= sizeof(ut);
if(loc < 0) break;
Expand Down
34 changes: 12 additions & 22 deletions toys/pending/modprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ GLOBALS(
uint8_t symreq;
)

/* Note: if "#define DBASE_SIZE" modified,
/* Note: if "#define DBASE_SIZE" modified,
* Please update GLOBALS dbase[256] accordingly.
*/
#define DBASE_SIZE 256
Expand Down Expand Up @@ -68,7 +68,7 @@ static char *path2mod(char *file, char *mod)
if (!mod) mod = xmalloc(MODNAME_LEN);

from = getbasename(file);

for (i = 0; i < (MODNAME_LEN-1) && from[i] && from[i] != '.'; i++)
mod[i] = (from[i] == '-') ? '_' : from[i];
mod[i] = '\0';
Expand Down Expand Up @@ -102,16 +102,6 @@ static void *llist_popme(struct arg_list **head)
return data;
}

// Add new node at the beginning of the list.
static void llist_add(struct arg_list **old, void *data)
{
struct arg_list *new = xmalloc(sizeof(struct arg_list));

new->arg = (char*)data;
new->next = *old;
*old = new;
}

// Add new node at tail of list.
static void llist_add_tail(struct arg_list **head, void *data)
{
Expand Down Expand Up @@ -184,11 +174,11 @@ static int read_line(FILE *fl, char **li)
}
for (;;) {
if (line[len - 1] == '\n') len--;
if (!len) {
if (!len) {
free(line);
return len;
} else if (line[len - 1] != '\\') break;

len--;
nxtlen = getline(&nxtline, &nxtlinelen, fl);
if (nxtlen <= 0) break;
Expand Down Expand Up @@ -226,11 +216,11 @@ static int config_action(struct dirtree *node)
free(filename);
return 0;
}
for (line = linecp = NULL; read_line(fc, &line) > 0;
for (line = linecp = NULL; read_line(fc, &line) > 0;
free(line), free(linecp), line = linecp = NULL) {
char *tk = NULL;

if (!strlen(line)) continue;
if (!strlen(line)) continue;
linecp = xstrdup(line);
for (tk = strtok(linecp, "# \t"), tcount = 0; tk;
tk = strtok(NULL, "# \t"), tcount++) {
Expand All @@ -240,7 +230,7 @@ static int config_action(struct dirtree *node)
break;
}
}
if (!tk) continue;
if (!tk) continue;
// process the tokens[0] contains first word of config line.
if (!strcmp(tokens[0], "alias")) {
struct arg_list *temp;
Expand Down Expand Up @@ -325,7 +315,7 @@ static void find_dep(void)
if (!mod) continue;
if ((mod->flags & MOD_ALOADED) &&
!(toys.optflags & (FLAG_r | FLAG_D))) continue;

mod->flags |= MOD_FNDDEPMOD;
if ((mod->flags & MOD_NDDEPS) && (!mod->dep)) {
TT.nudeps--;
Expand Down Expand Up @@ -441,7 +431,7 @@ static int go_probe(struct module_s *m)
}
if (toys.optflags & FLAG_v) printf("go_prob'ing %s\n", m->name);
if (!(toys.optflags & FLAG_r)) m->dep = llist_rev(m->dep);

while (m->dep) {
struct module_s *m2;
char *fn, *options;
Expand Down Expand Up @@ -532,7 +522,7 @@ void modprobe_main(void)

// Read /proc/modules to get loaded modules.
fs = xfopen("/proc/modules", "r");

while (read_line(fs, &procline) > 0) {
*(strchr(procline, ' ')) = '\0';
get_mod(procline, 1)->flags = MOD_ALOADED;
Expand Down Expand Up @@ -579,11 +569,11 @@ void modprobe_main(void)
do { // Probe all real names for the alias.
char *real = ((struct arg_list*)llist_pop(&module->rnames))->arg;
struct module_s *m2 = get_mod(real, 0);

if (toys.optflags&FLAG_v)
printf("probing alias %s by realname %s\n", module->name, real);
if (!m2) continue;
if (!(m2->flags & MOD_BLACKLIST)
if (!(m2->flags & MOD_BLACKLIST)
&& (!(m2->flags & MOD_ALOADED) || (flags & (FLAG_r | FLAG_D))))
go_probe(m2);
free(real);
Expand Down