fsnotifier: meaningful exit codes

This commit is contained in:
Roman Shevchenko
2013-06-17 20:00:27 +04:00
parent 5358d44522
commit df14345e77
3 changed files with 25 additions and 19 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -36,7 +36,7 @@
#define LOG_ENV_ERROR "error"
#define LOG_ENV_OFF "off"
#define VERSION "20130617.1827"
#define VERSION "20130617.1935"
#define VERSION_MSG "fsnotifier " VERSION "\n"
#define USAGE_MSG \
@@ -73,8 +73,8 @@ static bool self_test = false;
static void init_log();
static void run_self_test();
static void main_loop();
static bool read_input();
static bool main_loop();
static int read_input();
static bool update_roots(array* new_roots);
static void unregister_roots();
static bool register_roots(array* new_roots, array* unwatchable, array* mounts);
@@ -116,12 +116,15 @@ int main(int argc, char** argv) {
setvbuf(stdin, NULL, _IONBF, 0);
int rv = 0;
roots = array_create(20);
if (init_inotify() && roots != NULL) {
if (roots != NULL && init_inotify()) {
set_inotify_callback(&inotify_callback);
if (!self_test) {
main_loop();
if (!main_loop()) {
rv = 3;
}
}
else {
run_self_test();
@@ -131,14 +134,15 @@ int main(int argc, char** argv) {
}
else {
output("GIVEUP\n");
rv = 2;
}
close_inotify();
array_delete(roots);
userlog(LOG_INFO, "finished");
userlog(LOG_INFO, "finished (%d)", rv);
closelog();
return 0;
return rv;
}
@@ -216,7 +220,7 @@ static void run_self_test() {
}
static void main_loop() {
static bool main_loop() {
int input_fd = fileno(stdin), inotify_fd = get_inotify_fd();
int nfds = (inotify_fd > input_fd ? inotify_fd : input_fd) + 1;
fd_set rfds;
@@ -233,14 +237,16 @@ static void main_loop() {
if (select(nfds, &rfds, NULL, NULL, &timeout) < 0) {
if (errno != EINTR) {
userlog(LOG_ERR, "select: %s", strerror(errno));
break;
return false;
}
}
else if (FD_ISSET(input_fd, &rfds)) {
if (!read_input()) break;
int result = read_input();
if (result == 0) return true;
else if (result != ERR_CONTINUE) return false;
}
else if (FD_ISSET(inotify_fd, &rfds)) {
if (!process_inotify_input()) break;
if (!process_inotify_input()) return false;
}
else {
check_missing_roots();
@@ -249,24 +255,24 @@ static void main_loop() {
}
static bool read_input() {
static int read_input() {
char* line = read_line(stdin);
userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));
if (line == NULL || strcmp(line, "EXIT") == 0) {
userlog(LOG_INFO, "exiting: %s", line);
return false;
return 0;
}
if (strcmp(line, "ROOTS") == 0) {
array* new_roots = array_create(20);
CHECK_NULL(new_roots, false);
CHECK_NULL(new_roots, ERR_ABORT);
while (1) {
line = read_line(stdin);
userlog(LOG_DEBUG, "input: %s", (line ? line : "<null>"));
if (line == NULL || strlen(line) == 0) {
return false;
return 0;
}
else if (strcmp(line, "#") == 0) {
break;
@@ -274,15 +280,15 @@ static bool read_input() {
else {
int l = strlen(line);
if (l > 1 && line[l-1] == '/') line[l-1] = '\0';
CHECK_NULL(array_push(new_roots, strdup(line)), false);
CHECK_NULL(array_push(new_roots, strdup(line)), ERR_ABORT);
}
}
return update_roots(new_roots);
return update_roots(new_roots) ? ERR_CONTINUE : ERR_ABORT;
}
userlog(LOG_INFO, "unrecognised command: %s", line);
return true;
userlog(LOG_WARNING, "unrecognised command: %s", line);
return ERR_CONTINUE;
}