Beispiel #1
0
static void remove_file(FILE *p)
{
	int i;

	if (debug < 2) return;

	mrlog("remove_file(%p)", p);
	for (i = 0; i < 100; i++)
		if (files[i].p == p) break;
	if (i == 100) {
		mrlog("Can't find file, exiting");
		dump_files();
		mrlog("Continuing even though I can't find file %p", p);
	}
	mrlog("Removing file %p (%s) from slot %d", p, files[i].cl, i);
	dump_files();
	files[i].p = NULL;
}
Beispiel #2
0
static void store_file(FILE *p, char *cl)
{
	int i;

	if (debug < 2) return;
	mrlog("store_file(%p, %s)", p, cl);

	for (i = 0; i < 100; i++)
		if (files[i].p == NULL) break;
	if (i == 100) {
		mrlog("No empty files slot, exiting");
		dump_files();
		mrexit("Out of file slots", EXIT_FAILURE);
	}
	mrlog("Storing file %p (%s) in slot %d", p, cl, i);
	dump_files();
	files[i].p = p;
	strlcpy(files[i].cl, cl, 20);
}
Beispiel #3
0
void get_config_files( void )
/***************************/
{
    int                 drive_name;
#ifdef __OS2__
    SEL                 gbl_seg;
    SEL                 lcl_seg;
    PGINFOSEG           gbl;

    DosGetInfoSeg( &gbl_seg, &lcl_seg );
    gbl = MK_FP( gbl_seg, 0 );
    techoutput( "OS/2 Version %d.%d%c\n", gbl->uchMajorVersion,
                gbl->uchMinorVersion, gbl->chRevisionLetter);
    drive_name = gbl->bootdrive + 'A' - 1;
#else
    union REGS          regs;
    char                *comspec;

    regs.h.ah = 0x30;
    intdos( &regs, &regs );
    techoutput( "DOS Version %d.%d\n", regs.h.al, regs.h.ah );
    /*   Get Drive that user booted off of   */
    if( regs.h.al >= 0x04 ) {
        regs.w.ax = 0x3305;
        intdos( &regs, &regs );
        drive_name = regs.h.dl + 'A' - 1;
    } else {
        comspec = getenv( "COMSPEC" );
        if( comspec != NULL ) {
            drive_name = toupper( *comspec );
        } else {
            return;
        }
    }
#endif
    dump_files( drive_name );
}
/* calls skip to gate calling dump_from_fd recursively
 * in the specified directory. dump_from_fd defaults to
 * dump_file_from_fd above when set to NULL. skip defaults
 * to false when set to NULL. dump_from_fd will always be
 * called with title NULL.
 */
int dump_files(const char *title, const char *dir,
        bool (*skip)(const char *path),
        int (*dump_from_fd)(const char *title, const char *path, int fd)) {
    DIR *dirp;
    struct dirent *d;
    char *newpath = NULL;
    char *slash = "/";
    int fd, retval = 0;

    if (title) {
        printf("------ %s (%s) ------\n", title, dir);
    }

    if (dir[strlen(dir) - 1] == '/') {
        ++slash;
    }
    dirp = opendir(dir);
    if (dirp == NULL) {
        retval = -errno;
        fprintf(stderr, "%s: %s\n", dir, strerror(errno));
        return retval;
    }

    if (!dump_from_fd) {
        dump_from_fd = dump_file_from_fd;
    }
    for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
        if ((d->d_name[0] == '.')
         && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
          || (d->d_name[1] == '\0'))) {
            continue;
        }
        asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
                 (d->d_type == DT_DIR) ? "/" : "");
        if (!newpath) {
            retval = -errno;
            continue;
        }
        if (skip && (*skip)(newpath)) {
            continue;
        }
        if (d->d_type == DT_DIR) {
            int ret = dump_files(NULL, newpath, skip, dump_from_fd);
            if (ret < 0) {
                retval = ret;
            }
            continue;
        }
        fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
        if (fd < 0) {
            retval = fd;
            printf("*** %s: %s\n", newpath, strerror(errno));
            continue;
        }
        (*dump_from_fd)(NULL, newpath, fd);
    }
    closedir(dirp);
    if (title) {
        printf("\n");
    }
    return retval;
}
static gboolean
dump_files (GFile *dir,
            struct archive *archive,
            GCancellable *cancellable,
            char *parent,
            GError **error)
{
  g_autoptr(GFileEnumerator) fe = NULL;
  gboolean ret = TRUE;
  GFileType type;

  fe = g_file_enumerate_children (dir,
                                  "standard::name,standard::type,standard::is-symlink,standard::symlink-target,unix::mode,time::*",
                                  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, error);
  if (fe == NULL)
    return FALSE;


  while (TRUE)
    {
      g_autoptr(GFileInfo) info = g_file_enumerator_next_file (fe, cancellable, error);
      g_autofree char *path = NULL;
      g_autoptr(GFile) child = NULL;
      guint32 mode;
      g_autoptr(archive_entry_t) entry = archive_entry_new2 (archive);

      if (!info)
        {
          if (error && *error != NULL)
            ret = FALSE;
          break;
        }

      type = g_file_info_get_file_type (info);
      mode = g_file_info_get_attribute_uint32 (info, "unix::mode");
      path = g_build_filename (parent, g_file_info_get_name (info), NULL);
      child = g_file_enumerator_get_child (fe, info);

      archive_entry_set_pathname (entry, path);
      archive_entry_set_uid(entry, 0);
      archive_entry_set_gid(entry, 0);
      archive_entry_set_perm(entry, mode & 0777);
      archive_entry_set_mtime(entry, 0, 0);

      switch (type)
        {
        case G_FILE_TYPE_SYMBOLIC_LINK:
          archive_entry_set_filetype (entry, AE_IFLNK);
          archive_entry_set_symlink (entry, g_file_info_get_symlink_target (info));
          break;

        case G_FILE_TYPE_REGULAR:
          archive_entry_set_filetype (entry, AE_IFREG);
          archive_entry_set_size(entry, g_file_info_get_size (info));
          break;

        case G_FILE_TYPE_DIRECTORY:
          archive_entry_set_filetype (entry, AE_IFDIR);
          break;

        default:
          g_error ("Unhandled type %d\n", type);
          break;
        }

      if (archive_write_header (archive, entry) < ARCHIVE_OK)
        return xdg_app_fail (error, "Can't write tar header");

      if (type == G_FILE_TYPE_REGULAR)
        {
          if (!dump_data (child, archive, cancellable, error))
            return FALSE;
        }

      if (archive_write_finish_entry (archive) < ARCHIVE_OK)
        return xdg_app_fail (error, "Can't finish tar entry");

      if (type == G_FILE_TYPE_DIRECTORY)
        {
          if (!dump_files (child, archive, cancellable, path, error))
            return FALSE;
        }
    }

  return ret;
}
static gboolean
dump_runtime (GFile *root, GCancellable *cancellable, GError **error)
{
  int i;

  g_autoptr(write_archive_t) archive = NULL;
  g_autoptr(GFile) files = g_file_get_child (root, "files");

  archive = archive_write_new ();
  if (archive == NULL)
    return xdg_app_fail (error, "Can't allocate archive");

  if (archive_write_set_format_gnutar (archive) < ARCHIVE_OK)
    return xdg_app_fail (error, "Can't set tar format");

  if (archive_write_open_FILE (archive, stdout) < ARCHIVE_OK)
    return xdg_app_fail (error, "can't open stdout");

  for (i = 0; i < G_N_ELEMENTS(extra_dirs); i++)
    {
      g_autoptr(archive_entry_t) entry = archive_entry_new2 (archive);

      archive_entry_set_pathname (entry, extra_dirs[i]);
      archive_entry_set_uid(entry, 0);
      archive_entry_set_gid(entry, 0);
      archive_entry_set_perm(entry, 0755);
      archive_entry_set_mtime(entry, 0, 0);
      archive_entry_set_filetype (entry, AE_IFDIR);

      if (archive_write_header (archive, entry) < ARCHIVE_OK)
        return xdg_app_fail (error, "Can't write tar header");
    }

  for (i = 0; i < G_N_ELEMENTS(extra_symlinks); i++)
    {
      g_autoptr(archive_entry_t) entry = NULL;

      if (g_str_has_prefix (extra_symlinks[i].target, "usr/"))
        {
          g_autoptr(GFile) dest = g_file_resolve_relative_path (files, extra_symlinks[i].target + 4);

          if (!g_file_query_exists (dest, cancellable))
            continue;
        }

      entry = archive_entry_new2 (archive);

      archive_entry_set_pathname (entry, extra_symlinks[i].path);
      archive_entry_set_uid(entry, 0);
      archive_entry_set_gid(entry, 0);
      archive_entry_set_perm(entry, 0755);
      archive_entry_set_mtime(entry, 0, 0);
      archive_entry_set_filetype (entry, AE_IFLNK);
      archive_entry_set_symlink (entry, extra_symlinks[i].target);

      if (archive_write_header (archive, entry) < ARCHIVE_OK)
        return xdg_app_fail (error, "Can't write tar header");
    }

  if (!dump_files (files, archive, cancellable, "usr", error))
    return FALSE;

  if (archive_write_close (archive) < ARCHIVE_OK)
    return xdg_app_fail (error, "can't close archive");

  return TRUE;
}
Beispiel #7
0
int main(int argc, char **argv)
{
	int i;
	char *p;

	startup_log("main()");
	dirsep = '\\';
	GetModuleFileName(NULL, cfgdir, sizeof cfgdir);
	startup_log("cfgdir = '%s'", cfgdir);
	p = strrchr(cfgdir, dirsep);
	if (p) *p = '\0';
	cfgfile[0] = '\0';
	snprcat(cfgfile, sizeof cfgfile,
		"%s%c%s", cfgdir, dirsep, "mrbig.cfg");
	startup_log("cfgfile = '%s'", cfgfile);
	startup_log("SystemRoot = '%s'", getenv("SystemRoot"));

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-c")) {
			i++;
			if (argv[i] == NULL) {
				fprintf(stderr, "No cfg file\n");
				return EXIT_FAILURE;
			}
		} else if (!strcmp(argv[i], "-d")) {
			debug++;
		} else if (!strcmp(argv[i], "-m")) {
			debug_memory = 1;
		} else if (!strncmp(argv[i], "-i", 2)) {
			if (argv[i][2] == '\0') {
				install_service("MrBig", "Mr Big Monitoring Agent");
			} else {
				install_service(argv[i]+2, argv[i]+2);
			}
			return 0;
		} else if (!strncmp(argv[i], "-u", 2)) {
			if (argv[i][2] == '\0') {
				delete_service("MrBig");
			} else {
				delete_service(argv[i]+2);
			}
			return 0;
		} else if (!strcmp(argv[i], "-t")) {
			standalone = 1;
		} else {
			fprintf(stderr, "Bogus option '%s'\n", argv[i]);
			usage();
		}
	}

	if (standalone) {
		mrbig();
		return 0;
	}

	startup_log("We want to become a service");
	service_main(argc, argv);

	dump_chunks();
	check_chunks("just before exit");
	dump_files();

	return 0;
}