diff options
author | sam <sam@FreeBSD.org> | 2004-12-11 02:33:33 +0000 |
---|---|---|
committer | sam <sam@FreeBSD.org> | 2004-12-11 02:33:33 +0000 |
commit | 476c9d3a010c9ec2db658c943141a6cee92cd794 (patch) | |
tree | 39a3278eb0acc8da2b932e61895f82cd40238e25 /sbin | |
parent | 35ee62eea56fd3e170bea33daf977aa394350181 (diff) | |
download | FreeBSD-src-476c9d3a010c9ec2db658c943141a6cee92cd794.zip FreeBSD-src-476c9d3a010c9ec2db658c943141a6cee92cd794.tar.gz |
add a callback mechanism for code that wants to defer committing changes
until all the command line args have been processed
Reviewed by: ambrisko
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/ifconfig/ifconfig.c | 30 | ||||
-rw-r--r-- | sbin/ifconfig/ifconfig.h | 3 |
2 files changed, 33 insertions, 0 deletions
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 3725276..5fc2362 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -446,6 +446,27 @@ cmd_lookup(const char *name) #undef N } +struct callback { + callback_func *cb_func; + void *cb_arg; + struct callback *cb_next; +}; +static struct callback *callbacks = NULL; + +void +callback_register(callback_func *func, void *arg) +{ + struct callback *cb; + + cb = malloc(sizeof(struct callback)); + if (cb == NULL) + errx(1, "unable to allocate memory for callback"); + cb->cb_func = func; + cb->cb_arg = arg; + cb->cb_next = callbacks; + callbacks = cb; +} + /* specially-handled comamnds */ static void setifaddr(const char *, int, int, const struct afswtch *); static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); @@ -457,6 +478,7 @@ static const struct cmd setifdstaddr_cmd = static int ifconfig(int argc, char *const *argv, const struct afswtch *afp) { + struct callback *cb; int s; if (afp == NULL) @@ -541,6 +563,14 @@ ifconfig(int argc, char *const *argv, const struct afswtch *afp) if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) Perror("ioctl (SIOCAIFADDR)"); } + + /* + * Do deferred callbacks registered while processing + * command-line arguments. + */ + for (cb = callbacks; cb != NULL; cb = cb->cb_next) + cb->cb_func(s, cb->cb_arg); + close(s); return(0); } diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h index aa09ff8..3de635c 100644 --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -56,6 +56,9 @@ struct cmd { }; void cmd_register(struct cmd *); +typedef void callback_func(int s, void *); +void callback_register(callback_func *, void *); + /* * Macros for declaring command functions and initializing entries. */ |