gint main (gint argc, gchar *argv[]) { g_autoptr(GPtrArray) new_argv = NULL; g_autofree gchar *path = NULL; g_autofree gchar *env_param = NULL; g_autofree gchar *fwd_param = NULL; const gchar *fuse_commfd_env; gint fuse_commfd = -1; gint exit_status; fuse_commfd_env = g_getenv ("_FUSE_COMMFD"); if (!parse_fd (fuse_commfd_env, &fuse_commfd)) return EXIT_FAILURE; env_param = g_strdup_printf ("--env=_FUSE_COMMFD=%s", fuse_commfd_env); fwd_param = g_strdup_printf ("--forward-fd=%d", fuse_commfd); if (!(path = g_find_program_in_path ("flatpak-spawn"))) return EXIT_FAILURE; new_argv = g_ptr_array_new (); g_ptr_array_add (new_argv, path); g_ptr_array_add (new_argv, (gchar *)"--clear-env"); g_ptr_array_add (new_argv, (gchar *)"--watch-bus"); g_ptr_array_add (new_argv, (gchar *)"--host"); g_ptr_array_add (new_argv, env_param); g_ptr_array_add (new_argv, fwd_param); g_ptr_array_add (new_argv, (gchar *)"fusermount"); for (guint i = 1; i < argc; i++) g_ptr_array_add (new_argv, argv[i]); g_ptr_array_add (new_argv, NULL); g_spawn_sync (NULL, (gchar **)new_argv->pdata, NULL, (G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_CHILD_INHERITS_STDIN), child_setup_func, NULL, NULL, NULL, &exit_status, NULL); return exit_status; }
int XML_Parser::parse_file(const char* path) { // Open file: int fd = open(path, O_RDONLY); if (fd < 0) { raise("open() failed"); return -1; } // Parse file descriptor: return parse_fd(fd); }
int fdwalk(void (*func)(void *, int), void *opaque) { DIR *dir = opendir("/proc/self/fd"); if (!dir) { return -1; } int opendirfd = dirfd(dir); int parse_errors = 0; struct dirent *ent; // Have to clear errno to distinguish readdir() completion from failure. while (errno = 0, (ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } // We avoid atoi or strtol because those are part of libc and they involve // locale stuff, which is probably not safe from a post-fork context in a // multi-threaded app. int fd = parse_fd(ent->d_name); if (fd < 0) { parse_errors = 1; continue; } if (fd != opendirfd) { (*func)(opaque, fd); } } int saved_errno = errno; if (closedir(dir) < 0) { if (!saved_errno) { // Return the closedir error. return -1; } // Else ignore it because we have a more relevant error to return. } if (saved_errno) { errno = saved_errno; return -1; } else if (parse_errors) { errno = EBADF; return -1; } else { return 0; } }
void LogAnalyzer::parse_path(const char *path) { create_vehicle_from_commandline_arguments(); if (_vehicle == NULL) { _vehicle = new AnalyzerVehicle::Base(); } bool do_stdin = false; log_format_t log_format = log_format_none; if (!strcmp(path, "-")) { do_stdin = true; } else if (ends_with(path, ".BIN") || ends_with(path, ".bin")) { log_format = log_format_df; } else if (ends_with(path, ".log") || ends_with(path, ".LOG")) { log_format = log_format_log; } else if (ends_with(path, ".tlog") || ends_with(path, ".TLOG")) { log_format = log_format_tlog; } if (force_format() != log_format_none) { log_format = force_format(); } if (log_format == log_format_none) { if (do_stdin) { ::fprintf(stderr, "You asked to parse stdin but did not force a format type\n"); } else { ::fprintf(stderr, "Unable to determine log type from filename (%s); try -i?\n", path); } usage(); exit(1); } switch (log_format) { case log_format_tlog: prep_for_tlog(); break; case log_format_df: prep_for_df(); break; case log_format_log: prep_for_log(); break; default: abort(); } int fd; if (streq(path, "-")) { // fd = fileno(stdin); // doesn't work on Cygwin fd = 0; } else { fd = xopen(path, O_RDONLY); } if (output_style == Analyze::OUTPUT_BRIEF) { printf("%25s: ", path); } parse_fd(reader, fd); if (!streq(path, "-")) { close(fd); } if (output_style == Analyze::OUTPUT_BRIEF) { printf("\n"); } switch (log_format) { case log_format_tlog: cleanup_after_tlog(); break; case log_format_df: cleanup_after_df(); break; case log_format_log: cleanup_after_log(); break; default: abort(); } delete _vehicle; _vehicle = NULL; }