diff --git a/src/common.c b/src/common.c index 9b32f84..22b6d38 100644 --- a/src/common.c +++ b/src/common.c @@ -82,6 +82,17 @@ int setgroups(int count, int *groups) } #endif + +void +check_superuser(void (*usage_fn)(void)) +{ + if (geteuid() != 0) { + warnx("Run as root and you'll be happy.\n"); + usage_fn(); + /* NOTREACHED */ + } +} + int open_dns(int localport, in_addr_t listen_ip) { diff --git a/src/common.h b/src/common.h index edc8e82..06d57db 100644 --- a/src/common.h +++ b/src/common.h @@ -58,6 +58,7 @@ struct query { int fromlen; }; +void check_superuser(void (*usage_fn)(void)); int open_dns(int, in_addr_t); void close_dns(int); diff --git a/src/iodine.c b/src/iodine.c index 76d1e36..67a9bff 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -945,11 +945,7 @@ main(int argc, char **argv) } } - if (geteuid() != 0) { - warnx("Run as root and you'll be happy.\n"); - usage(); - /* NOTREACHED */ - } + check_superuser(usage); argc -= optind; argv += optind; diff --git a/src/iodined.c b/src/iodined.c index a1dc298..3295d44 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -1002,10 +1002,7 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (geteuid() != 0) { - warnx("Run as root and you'll be happy.\n"); - usage(); - } + check_superuser(usage); if (argc != 2) usage();