Adds pid file support.

This commit is contained in:
Andrea Mazzoleni 2013-06-17 23:50:26 +02:00
parent 875081f530
commit bc2a837953
8 changed files with 149 additions and 14 deletions

View file

@ -84,7 +84,7 @@ void usage(void)
#define OPT_TEST_SKIP_SEQUENTIAL 263
#define OPT_TEST_FORCE_MURMUR3 264
#define OPT_TEST_FORCE_SPOOKY2 265
#define OPT_TEST_SKIP_PID 266
#if HAVE_GETOPT_LONG
struct option long_options[] = {
@ -139,6 +139,9 @@ struct option long_options[] = {
/* Force Spooky2 hash */
{ "test-force-spooky2", 0, 0, OPT_TEST_FORCE_SPOOKY2 },
/* Skip the use of pid file */
{ "test-skip-pid", 0, 0, OPT_TEST_SKIP_PID },
{ 0, 0, 0, 0 }
};
#endif
@ -185,6 +188,7 @@ int main(int argc, char* argv[])
int test_skip_device;
int test_force_murmur3;
int test_force_spooky2;
int test_skip_pid;
const char* conf;
struct snapraid_state state;
int operation;
@ -198,6 +202,7 @@ int main(int argc, char* argv[])
const char* command;
const char* import;
const char* log;
int pid;
os_init();
@ -220,6 +225,7 @@ int main(int argc, char* argv[])
test_skip_device = 0;
test_force_murmur3 = 0;
test_force_spooky2 = 0;
test_skip_pid = 0;
blockstart = 0;
blockcount = 0;
tommy_list_init(&filterlist_file);
@ -227,6 +233,7 @@ int main(int argc, char* argv[])
filter_missing = 0;
import = 0;
log = 0;
pid = 0;
opterr = 0;
while ((c =
@ -347,6 +354,9 @@ int main(int argc, char* argv[])
case OPT_TEST_FORCE_SPOOKY2 :
test_force_spooky2 = 1;
break;
case OPT_TEST_SKIP_PID :
test_skip_pid = 1;
break;
default:
fprintf(stderr, "Unknown option '%c'\n", (char)c);
exit(EXIT_FAILURE);
@ -427,6 +437,30 @@ int main(int argc, char* argv[])
raid_init();
if (!test_skip_self)
selftest(gui);
state_init(&state);
/* read the configuration file */
state_config(&state, conf, command, verbose, gui, force_zero, force_empty, force_uuid, find_by_name, test_expect_unrecoverable, test_expect_recoverable, test_skip_sign, test_skip_fallocate, test_skip_sequential, test_skip_device, test_force_murmur3, test_force_spooky2);
#if HAVE_PIDFILE
/* create the pid file */
if (!test_skip_pid) {
pid = pid_lock(state.pidfile);
if (pid == -1) {
if (errno != EWOULDBLOCK) {
fprintf(stderr, "Error creating the pid file '%s'. %s.\n", state.pidfile, strerror(errno));
} else {
fprintf(stderr, "The pid file '%s' is already locked!\n", state.pidfile);
fprintf(stderr, "SnapRAID is already in use!\n");
}
exit(EXIT_FAILURE);
}
}
#endif
/* open the log file */
if (log == 0)
log = "2";
@ -442,13 +476,6 @@ int main(int argc, char* argv[])
}
}
if (!test_skip_self)
selftest(gui);
state_init(&state);
state_config(&state, conf, command, verbose, gui, force_zero, force_empty, force_uuid, find_by_name, test_expect_unrecoverable, test_expect_recoverable, test_skip_sign, test_skip_fallocate, test_skip_sequential, test_skip_device, test_force_murmur3, test_force_spooky2);
if (operation == OPERATION_DIFF) {
state_read(&state);
@ -545,10 +572,6 @@ int main(int argc, char* argv[])
}
}
state_done(&state);
tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
/* close log file */
if (stdlog != stdout && stdlog != stderr) {
if (fclose(stdlog) != 0) {
@ -557,6 +580,19 @@ int main(int argc, char* argv[])
}
}
#if HAVE_PIDFILE
if (!test_skip_pid) {
if (pid_unlock(pid, state.pidfile) == -1) {
fprintf(stderr, "Error closing the pid file '%s'. %s.\n", state.pidfile, strerror(errno));
exit(EXIT_FAILURE);
}
}
#endif
state_done(&state);
tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
os_done();
return EXIT_SUCCESS;