summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2018-08-08 22:53:03 -0400
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2018-08-08 22:53:03 -0400
commit75b4f9e76ed827bbe10a12f87eb34c744bf829b4 (patch)
tree813f4715d540d381f4fc1e050e29bb5575a4a8c4
parentc076dcdfcf8e69b26e44c8a71297769743eb0f4e (diff)
downloadrandom-seed-75b4f9e76ed827bbe10a12f87eb34c744bf829b4.tar.xz
random-seed-75b4f9e76ed827bbe10a12f87eb34c744bf829b4.zip
random-seed.c cleanups, add default seed path
-rw-r--r--configure.ac9
-rw-r--r--random-seed.c60
2 files changed, 48 insertions, 21 deletions
diff --git a/configure.ac b/configure.ac
index a624294..51397a3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -37,4 +37,13 @@ AS_IF([test -n "$machine_id"],
]
)
+AC_ARG_WITH([default-seed-path],
+ AC_HELP_STRING(--with-default-seed-path, [default seed path if no command line argument [/var/lib/random-seed]]),
+ [default_seed_path=$withval],
+ [default_seed_path=/var/lib/random-seed])
+AC_DEFINE_UNQUOTED(DEFAULT_SEED_PATH, "$default_seed_path", [default seed path])
+# assume people will not specify default_seed_path=/
+default_seed_path_dir=${default_seed_path%/*}
+AC_SUBST(default_seed_path_dir)
+
AC_OUTPUT(Makefile)
diff --git a/random-seed.c b/random-seed.c
index 385d4f5..338a385 100644
--- a/random-seed.c
+++ b/random-seed.c
@@ -22,6 +22,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdnoreturn.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@@ -38,6 +39,7 @@
/* 3 hours */
#define DAEMONIZE_SLEEP_TIME (3*60*60)
+/* random pool is always the same size, so use a fixed size array */
struct rand_pool_info_ {
int entropy_count;
int buf_size;
@@ -322,7 +324,6 @@ static bool save(const char *seed_path, unsigned char *random_buf) {
seed_path_tmp = strdup(seed_path);
- // TODO: rewrite when AT_REPLACE gets added... eventually...
seed_dir_fd = open(mydirname(seed_path_tmp), O_RDONLY | O_DIRECTORY);
if (seed_dir_fd == -1) {
perror("error opening seed directory");
@@ -437,26 +438,12 @@ static void sighandler(int signum) {
}
}
-int main(int argc, char *argv[]) {
- const char *seed_path = argv[2];
+noreturn static void run(const char *mode, const char *seed_path) {
FILE *seed_file;
unsigned char random_buf[RAND_POOL_SIZE];
int exit_status = 0;
- if (argc == 2 && (streq(argv[1], "-h") || streq(argv[1], "--help"))) {
- usage();
- exit(0);
- }
-
- if (argc != 3) {
- fprintf(stderr, "invalid argument count, expected 2\n");
- usage();
- exit(2);
- }
-
- umask(0077);
-
- if (streq(argv[1], "load")) {
+ if (streq(mode, "load")) {
bool refresh_seed = true;
if (streq(seed_path, "-")) {
@@ -487,9 +474,9 @@ int main(int argc, char *argv[]) {
}
}
exit(exit_status);
- } else if (streq(argv[1], "save")) {
+ } else if (streq(mode, "save")) {
exit(!save(seed_path, NULL));
- } else if (streq(argv[1], "daemonize")) {
+ } else if (streq(mode, "daemonize")) {
if (streq(seed_path, "-")) {
fputs("error: seed_path cannot be - for daemonize\n", stderr);
exit(2);
@@ -499,7 +486,8 @@ int main(int argc, char *argv[]) {
perror("error opening seed file");
exit(3);
}
- load(seed_file);
+ if (!load(seed_file))
+ fputs("warning: failed to load initial entropy\n", stderr);
struct sigaction sa;
sa.sa_handler = sighandler;
@@ -524,7 +512,7 @@ int main(int argc, char *argv[]) {
exit(exit_status);
sleep(DAEMONIZE_SLEEP_TIME);
}
- } else if (streq(argv[1], "daemonise")) {
+ } else if (streq(mode, "daemonise")) {
fputs("invalid mode (did you mean `daemonize'?)\n", stderr);
exit(2);
} else {
@@ -532,3 +520,33 @@ int main(int argc, char *argv[]) {
exit(2);
}
}
+
+int main(int argc, char *argv[]) {
+ char *mode, *seed_path;
+
+ switch (argc) {
+ case 2:
+ if (streq(argv[1], "-h") || streq(argv[1], "--help")) {
+ usage();
+ exit(0);
+ }
+ if (streq(argv[1], "-V") || streq(argv[1], "--version")) {
+ printf("random-seed %s\n", PACKAGE_VERSION);
+ exit(0);
+ }
+ mode = argv[1];
+ seed_path = DEFAULT_SEED_PATH;
+ break;
+ case 3:
+ mode = argv[1];
+ seed_path = argv[2];
+ break;
+ default:
+ fprintf(stderr, "error: invalid arguments\n");
+ usage();
+ exit(2);
+ }
+
+ umask(0);
+ run(mode, seed_path);
+}