Пример #1
0
int
main(int argc, char **argv)
{
    struct stat sbuf;
    char *file, *flags = NULL;
    int c, err = 0, pid = 0;

    __pmSetProgname(argv[0]);
    while ((c = getopt(argc, argv, "f:p:")) != EOF) {
        switch (c) {
        case 'f':
            flags = optarg;
            break;
        case 'p':
            pid = atoi(optarg);
            break;
        default:
            err++;
        }
    }

    if (err || argc != optind + 1)
        usage();

    file = argv[optind];

    c = open(file, O_RDWR, 0644);
    if (c < 0) {
        fprintf(stderr, "Cannot open %s for writing: %s\n",
                file, strerror(errno));
        exit(1);
    }
    fstat(c, &sbuf);
    addr = __pmMemoryMap(c, sbuf.st_size, 1);
    close(c);

    if (flags)
        write_flags(flags);

    if (pid)
        write_pid(pid);

    __pmMemoryUnmap(addr, sbuf.st_size);
    exit(0);
}
Пример #2
0
Файл: help.c Проект: Aconex/pcp
/*
 * open the help text files and return a handle on success
 */
int
pmdaOpenHelp(char *fname)
{
    char	pathname[MAXPATHLEN];
    int		sts, size;
    help_idx_t	hdr;
    help_t	*hp;
    struct stat	sbuf;

    for (sts = 0; sts < numhelp; sts++) {
	if (tab[sts].dir_fd == -1)
	    break;
    }
    if (sts == numhelp) {
	sts = numhelp++;
	tab = (help_t *)realloc(tab, numhelp * sizeof(tab[0]));
	if (tab == NULL) {
	    __pmNoMem("pmdaOpenHelp", numhelp * sizeof(tab[0]), PM_RECOV_ERR);
	    numhelp = 0;
	    return -oserror();
	}
    }
    hp = &tab[sts];
    memset(hp, 0, sizeof(*hp));
    hp->dir_fd = -1;
    hp->pag_fd = -1;

    snprintf(pathname, sizeof(pathname), "%s.dir", fname);
    hp->dir_fd = open(pathname, O_RDONLY);
    if (hp->dir_fd < 0) {
	sts = -oserror();
	goto failed;
    }

    if (read(hp->dir_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
	sts = -EINVAL;
	goto failed;
    }

    if (hdr.pmid != 0x50635068 ||
	(hdr.off_oneline & 0xffff0000) != 0x31320000) {
	sts = -EINVAL;
	goto failed;
    }

    hp->numidx = hdr.off_text;
    size = (hp->numidx + 1) * sizeof(help_idx_t);

    hp->index = (help_idx_t *)__pmMemoryMap(hp->dir_fd, size, 0);
    if (hp->index == NULL) {
	sts = -oserror();
	goto failed;
    }

    snprintf(pathname, sizeof(pathname), "%s.pag", fname);
    hp->pag_fd = open(pathname, O_RDONLY);
    if (hp->pag_fd < 0) {
	sts = -oserror();
	goto failed;
    }
    if (fstat(hp->pag_fd, &sbuf) < 0) {
	sts = -oserror();
	goto failed;
    }
    hp->textlen = (int)sbuf.st_size;
    hp->text = (char *)__pmMemoryMap(hp->pag_fd, hp->textlen, 0);
    if (hp->text == NULL) {
	sts = -oserror();
	goto failed;
    }
    return numhelp - 1;

failed:
    pmdaCloseHelp(numhelp-1);
    return sts;
}