static struct buffer parse_table(struct buffer buff, struct invocation *invocation, const char **error) { unsigned i; size_t ident_len; struct argument args[QUERY_MAX_ARGS]; const char *ident_name = NULL; unsigned argi = 0; buff = chomp(buff); buff = expect_char(buff, '{', error); if (*error) goto clean; buff = chomp(buff); while (!peek(buff, "}")) { if (argi >= QUERY_MAX_ARGS) { raise_too_many_arguments(error); goto clean; } if (isalpha((int)buff.data[buff.offset])) { buff = get_ident(buff, &ident_name, &ident_len, error); if (!*error) { args[argi].a.value.type = RDT_STRING; args[argi].a.value.val.string.len = ident_len; args[argi].a.value.val.string.buff = (char*)calloc( ident_len + 1, sizeof(char) ); if (!args[argi].a.value.val.string.buff) goto clean; strncpy( args[argi].a.value.val.string.buff, ident_name, ident_len ); } } else buff = parse_string(buff, &args[argi].a.value, error); if (*error) goto clean; args[argi].type = AT_VALUE; buff = chomp(buff); argi++; buff = expect_char(buff, ':', error); if (*error) goto clean; buff = chomp(buff); if (argi >= QUERY_MAX_ARGS) { raise_too_many_arguments(error); goto clean; } buff = parse_argument(buff, &args[argi], error); if (*error) goto clean; argi++; buff = chomp(buff); buff = expect_char(buff, ',', error); if (*error) { *error = NULL; break; } buff = chomp(buff); } buff = expect_char(buff, '}', error); if (*error) goto clean; invocation->func = all_map; invocation->argc = argi; invocation->argv = (struct argument*) malloc(sizeof(struct argument) * argi); if (!invocation->argv) { raise_enomem(error); goto clean; } memcpy(invocation->argv, args, sizeof(struct argument) * argi); goto success; clean: for (i = 0; i < argi; i++) argument_free(&args[i]); success: return buff; }
int main(int argc, char** argv) { int ifd, len = 0, i = 0, selectret = 0, maxfd, retryselect, pid; char buf[BUF_LEN]; char *configdir; char *pidfilename; char *statusfilename; char *statusbin; char *error_str; char *version_str = PACKAGE_STRING; char *pbuf; char *filename; FILE *pidfile; FILE *statusfile; fd_set set; struct inotify_event *event; struct argument *argument = argument_new(); struct pipe_list *pipe_list_cur; struct stat file_stat; struct watchnode *node; /* alloc pipe list */ pipe_list_head = malloc(sizeof(struct pipe_list)); pipe_list_head->next = NULL; /* set up signals for exiting/reaping */ signal(SIGINT, &handle_quit_signal); signal(SIGTERM, &handle_quit_signal); signal(SIGCHLD, &handle_child_signal); signal(SIGHUP, &handle_hup_signal); /* add command line arguments */ argument_register(argument, "help", "Prints this help text.", 0); argument_register(argument, "version", "Prints version information.", 0); argument_register(argument, "daemon", "Run as a daemon.", 0); argument_register(argument, "verbose", "Turns on debug text.", 0); argument_register(argument, "sync", "Sync mode (for debugging).", 0); argument_register(argument, "log-to-stdout", "Deprecated, use \"--log-to=stdout\" instead", 0); argument_register(argument, "log-to", "Log messages with specified way. " #ifdef USE_SYSLOG "Can be: stdout, file, syslog. \"file\" by default.", 1); #else "Can be: stdout, file. \"file\" by default.", 1); #endif if ((error_str = argument_parse(argument, argc, argv))) { fprintf(stderr, "Error in arguments: %s", error_str); free(error_str); return -1; } if (argument_exists(argument, "help")) { char *help_txt = argument_get_help_text(argument); printf("%s", help_txt); free(help_txt); return 0; } if (argument_exists(argument, "version")) { printf("%s\n", version_str); return 0; } if (argument_exists(argument, "verbose")) { verbose = 1; } if (argument_exists(argument, "daemon") && fork()) return 0; if (argument_exists(argument, "sync")) syncmode = 1; if (argument_exists(argument, "log-to-stdout")) fprintf(stderr, "Warning, this option is deprecated, " \ "please use new syntax: \"--log-to=stdout\".\n"); logtype = LOG_FILE; if (argument_exists(argument, "log-to") && \ (log_arg = argument_get_value(argument, "log-to")) != NULL) { if (strcmp(log_arg, "stdout") == 0) logtype = LOG_STDOUT; #ifdef USE_SYSLOG else if (strcmp(log_arg, "syslog") == 0) logtype = LOG_SYS; #endif else /* logtype already set to 'file' above */ fprintf(stderr, "Warning, selected unknown logging type. " \ "Will use \"--log-to=file\" instead.\n"); } /* get config dir (must free this) */ configdir = get_config_dir(); /* if a config file has not been specified, use default */ if (argument_get_extra(argument)) { configfile = strdup(argument_get_extra(argument)); } else { configfile = malloc (strlen(configdir) + strlen ("/config") + 1); sprintf(configfile, "%s/config", configdir); } argument_free(argument); free(configdir); if (access(configfile, R_OK) != 0) { fprintf(stderr, "error: could not open config file: %s\n", configfile); return -1; } /* create a pid file */ pidfilename = get_pid_filename(); if (stat(pidfilename, &file_stat) == 0) /* pidfile exists */ { pidfile = fopen(pidfilename, "r"); if (fscanf(pidfile, "%d", &pid) == 1) /* pidfile has a pid inside */ { char *binaryname; char *scanformat; if ((binaryname = strrchr(argv[0], '/')) != NULL) { binaryname++; } else { binaryname = argv[0]; } scanformat = malloc(strlen("Name: %") + strlen(binaryname) + strlen("s") + 1); statusfilename = malloc(strlen("/proc/") + 6 + strlen("/status") + 1); sprintf(statusfilename, "/proc/%d/status", pid); if (stat(statusfilename, &file_stat) != 0) /* write pid file if the process no longer exists */ { write_pid_file(pidfilename); } else /* process exists, so check owner and binary name */ { statusfile = fopen(statusfilename, "r"); statusbin = malloc(strlen(binaryname) + 2); /* the binary name may start with "fsniper" but be longer */ sprintf(scanformat, "Name: %%%ds", strlen(binaryname) + 1); fscanf(statusfile, scanformat, statusbin); free(statusfilename); fclose(statusfile); fclose(pidfile); if (strcmp(binaryname, statusbin) == 0 && file_stat.st_uid == getuid()) /* exit if the process is fsniper and is owned by the current user */ { printf("%s: already running instance found with pid %d. exiting.\n", binaryname, pid); exit(1); } else /* the pid file contains an old pid, one that isn't fsniper, or one not owned by the current user */ { write_pid_file(pidfilename); } } } else /* pidfile is invalid */ { fclose(pidfile); write_pid_file(pidfilename); } } else /* the pidfile doesn't exist */ { write_pid_file(pidfilename); } free(pidfilename); /* start up log */ if (!log_open()) { fprintf(stderr, "Error: could not start log.\n"); return -1; } ifd = inotify_init(); if (ifd < 0) { perror("inotify_init"); return -1; } if (verbose) log_write("Parsing config file: %s\n", configfile); config = keyval_parse_file(configfile); if ((error_str = keyval_get_error())) { fprintf(stderr, "%s", error_str); free(error_str); exit(1); } validate_config(config); /* add nodes to the inotify descriptor */ g_watchnode = add_watches(ifd); /* wait for events and then handle them */ while (1) { /* set up fds and max */ FD_ZERO(&set); FD_SET(ifd, &set); maxfd = ifd; for (pipe_list_cur = pipe_list_head->next; pipe_list_cur; pipe_list_cur = pipe_list_cur->next) { FD_SET(pipe_list_cur->pfd[0], &set); if (pipe_list_cur->pfd[0] > maxfd) maxfd = pipe_list_cur->pfd[0]; } retryselect = 1; while (retryselect) { /* use select to get activity on any of the fds */ selectret = select(maxfd + 1, &set, NULL, NULL, NULL); if (selectret == -1) { if (errno == EINTR) retryselect = 1; else handle_quit_signal(-2); } else retryselect = 0; } /* handle any events on the inotify fd */ if (FD_ISSET(ifd, &set)) { len = read(ifd, buf, BUF_LEN); while (i < len) { event = (struct inotify_event *) &buf[i]; if (event->len && (event->mask & IN_CLOSE_WRITE || event->mask & IN_MOVED_TO)) { /* if sync mode, just call handle_exec */ if (syncmode == 1) { handle_event(event, fileno(_logfd)); } else { /* create new pipe_list entry */ for (pipe_list_cur = pipe_list_head; pipe_list_cur->next != NULL; pipe_list_cur = pipe_list_cur->next) {} pipe_list_cur->next = malloc(sizeof(struct pipe_list)); pipe_list_cur->next->next = NULL; /* create pipe */ pipe(pipe_list_cur->next->pfd); if (fork() == 0) { /* child, close 0 */ close(pipe_list_cur->next->pfd[0]); log_close(); signal(SIGINT, &handle_child_quit_signal); signal(SIGTERM, &handle_child_quit_signal); handle_event(event, pipe_list_cur->next->pfd[1]); } else { /* parent, close 1 */ close(pipe_list_cur->next->pfd[1]); } } } else if (event->len && (event->mask & IN_CREATE && event->mask & IN_ISDIR)) { for (node = g_watchnode->next; node; node = node->next) if (node->wd == event->wd) break; if (node) { /* combine the name inotify gives with the full path to the file */ filename = malloc(strlen(node->path) + strlen("/") + strlen(event->name) + 1); sprintf(filename, "%s/%s", node->path, event->name); watch_dir(node, ifd, strdup(filename), node->section); free(filename); } } else if (event->len && (event->mask & IN_DELETE && event->mask & IN_ISDIR)) { for (node = g_watchnode->next; node; node = node->next) if (node->wd == event->wd) break; if (node) { /* combine the name inotify gives with the full path to the file */ filename = malloc(strlen(node->path) + strlen("/") + strlen(event->name) + 1); sprintf(filename, "%s/%s", node->path, event->name); unwatch_dir(filename, ifd); free(filename); } } i += EVENT_SIZE + event->len; } i = 0; } /* now lets see if we have any pipe activity */ pipe_list_cur = pipe_list_head->next; while (pipe_list_cur) { if (FD_ISSET(pipe_list_cur->pfd[0], &set)) { len = read(pipe_list_cur->pfd[0], buf, BUF_LEN); if (len == 0) { close(pipe_list_cur->pfd[0]); /* remove this item from the list */ pipe_list_cur = pipe_list_remove(pipe_list_head, pipe_list_cur); } else { /* print it somewhere */ pbuf = malloc(len + 1); snprintf(pbuf, len, "%s", buf); log_write("%s\n", pbuf); free(pbuf); pipe_list_cur = pipe_list_cur->next; } } else { pipe_list_cur = pipe_list_cur->next; } } } }
static struct buffer parse_method_call(struct buffer buff, struct invocation *invocation, const char **error) { size_t func_name_len; unsigned i; struct argument args[QUERY_MAX_ARGS]; unsigned argi = 0; const char *func_name = NULL; struct registered_func *rf = registered_functions; invocation->func = NULL; buff = get_ident(buff, &func_name, &func_name_len, error); if (*error) goto clean; buff = chomp(buff); buff = expect_char(buff, '(', error); if (*error) goto clean; while (rf->name) { if (strncmp(rf->name, func_name, func_name_len) == 0) { invocation->func = rf->func; break; } rf++; } if (!invocation->func) { raise_unknown_function(buff.offset, func_name, func_name_len, error); goto clean; } buff = chomp(buff); while (!peek(buff, ")")) { if (argi >= QUERY_MAX_ARGS) { raise_too_many_arguments(error); goto clean; } buff = parse_argument(buff, &args[argi], error); if (*error) goto clean; argi++; buff = chomp(buff); buff = expect_char(buff, ',', error); if (*error) { *error = NULL; break; } buff = chomp(buff); } buff = expect_char(buff, ')', error); if (*error) goto clean; invocation->argc = argi; invocation->argv = (struct argument*) malloc(sizeof(struct argument) * argi); if (!invocation->argv) { raise_enomem(error); goto clean; } memcpy(invocation->argv, args, sizeof(struct argument) * argi); goto success; clean: for (i = 0; i < argi; i++) argument_free(&args[i]); success: return buff; }