summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/replarp.c66
1 files changed, 58 insertions, 8 deletions
diff --git a/src/replarp.c b/src/replarp.c
index 9e72d9b..a502a49 100644
--- a/src/replarp.c
+++ b/src/replarp.c
@@ -33,6 +33,7 @@
#include <sys/time.h>
#include <sys/ioctl.h>
#include <locale.h>
+#include <getopt.h>
#include <sys/socket.h>
#include <arpa/inet.h>
@@ -101,8 +102,8 @@ static void get_hwaddr();
static void debug_print_hwaddr(const char*, const uint8_t*, uint8_t);
#endif
static void main_loop();
-int main(int,char**);
static void log_f(int, const char*, ...);
+int main(int,char**);
#if !DEBUG
static enum {
@@ -357,24 +358,73 @@ static void main_loop()
int main(int argc, char **argv)
{
+ int opt;
+ char *pidfile = NULL;
+
perr = stderr;
debug_printf("Built with debug code.\n");
- daemonize();
+ while ((opt = getopt(argc, argv, "hfp:")) != -1) {
+ switch (opt) {
+ case 'p':
+ pidfile = optarg;
+ break;
+ default:
+ fprintf(perr, "Usage: %s [<options>] <ifname> <ip> [...]\n",
+ argv[0]);
+ fprintf(perr,
+ "Options:\n"
+ " -p <pidfile> Write PID to <pidfile>\n");
+ exit(EXIT_FAILURE);
+ }
+ }
- if (argc < 3) {
- fprintf(perr, "Usage: %s <ifname> <ip> [...]\n", argv[0]);
+ argv += optind;
+ argc -= optind;
+
+ if (argc < 2) {
+ fprintf(perr, "Wrong usage. Try -h for help.\n");
exit(EXIT_FAILURE);
}
- ifname = argv[1];
- addr_count = argc - 2;
+ daemonize();
+
+ if (pidfile) {
+ int pidfd = open(pidfile, O_RDWR | O_CREAT | O_EXCL,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ FILE *pidf;
+
+ if (pidfd < 0) {
+ if (errno == EEXIST)
+ fprintf(perr,
+ "Pidfile already exists. If you are sure there is no "
+ "instance of replarp already running, you should delete "
+ "it.\n");
+ else
+ fprintf(perr, "Error creating pidfile: %s\n",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (!(pidf = fdopen(pidfd, "w"))) {
+ fprintf(perr, "Error creating file from pidfd: %s\n",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ fprintf(pidf, "%jd\n", (intmax_t)getpid());
+ fclose(pidf);
+ }
+
+ ifname = argv[0];
+
+ addr_count = argc - 1;
addresses = malloc(addr_count*sizeof(struct in_addr));
for (int i = 0; i < addr_count; i++) {
- if (inet_pton(AF_INET, argv[2 + i], &addresses[i]) < 1) {
+ if (inet_pton(AF_INET, argv[1 + i], &addresses[i]) < 1) {
fprintf(perr,
"An error occured while parsing the IP `%s'.\n",
- argv[2 + i]);
+ argv[1 + i]);
exit(EXIT_FAILURE);
}
}