ex_t histogram(bfpath *bfp) { ex_t rc; uint count; void *dsh, *dbe; dsv_t val; rhistogram_t hist; dbe = ds_init(bfp); if (dbe == NULL) return EX_ERROR; dsh = ds_open(dbe, bfp, DS_READ); if (dsh == NULL) return EX_ERROR; if (DST_OK != ds_txn_begin(dsh)) { ds_close(dsh); ds_cleanup(dbe); fprintf(stderr, "cannot begin transaction!\n"); return EX_ERROR; } ds_get_msgcounts(dsh, &val); mgood = val.goodcount; mbad = val.spamcount; memset(&hist, 0, sizeof(hist)); rc = ds_foreach(dsh, ds_histogram_hook, &hist); if (DST_OK != ds_txn_commit(dsh)) { ds_close(dsh); ds_cleanup(dbe); fprintf(stderr, "cannot commit transaction!\n"); return EX_ERROR; } ds_close(dsh); ds_cleanup(dbe); count = print_histogram(&hist); if (verbose > 0) { printf("hapaxes: ham %7u, spam %7u\n", ham_hapax, spam_hapax); printf(" pure: ham %7u, spam %7u\n", ham_only, spam_only); } else { printf("hapaxes: ham %7u (%5.2f%%), spam %7u (%5.2f%%)\n", ham_hapax, PCT(ham_hapax), spam_hapax, PCT(spam_hapax)); printf(" pure: ham %7u (%5.2f%%), spam %7u (%5.2f%%)\n", ham_only, PCT(ham_only), spam_only, PCT(spam_only)); } return rc; }
static void cleanup(void) { (void) chdir("/"); if (tmpdir) { (void) rrmdir(tmpdir); free(tmpdir); tmpdir = NULL; } if (tmppath) { /* remove any previous tmppath stuff */ (void) rrmdir(tmppath); free(tmppath); tmppath = NULL; } if (tmpsymdir) { /* remove temp symbolic links made for signed pkg */ (void) rrmdir(tmpsymdir); free(tmpsymdir); tmpsymdir = NULL; } if (srcdev.mount && !ids_name) (void) pkgumount(&srcdev); if (dstdev.mount && !ods_name) (void) pkgumount(&dstdev); (void) ds_close(1); }
void quit(int retcode) { (void) signal(SIGINT, SIG_IGN); (void) signal(SIGHUP, SIG_IGN); (void) ds_close(1); (void) pkghead(NULL); exit(retcode); }
void quit(int retcode) { PKG_ERR *err; err = pkgerr_new(); (void) signal(SIGINT, SIG_IGN); (void) signal(SIGHUP, SIG_IGN); (void) ds_close(1); (void) pkghead(NULL); if (keystore != NULL) { (void) close_keystore(err, keystore, NULL); pkgerr_free(err); } exit(retcode); }
main() { int count; int fd; int measurements = 10000; char buffer[SECTOR]; FILE *fout = fopen("ploppy.txt", "w"); float time1; float time2; float outtime; struct timeval tv; for (count = 0; count < SECTOR; count++) { buffer[count] = 'X'; } fd = ds_open("/dev/disksim", O_WRONLY); for (count = 0; count < measurements; count++) { ds_lseek(fd, 0, SEEK_CUR); ds_write(fd, buffer, SECTOR); ds_gettimeofday(&tv); time1 = (float) tv.tv_sec*1000 + (float) tv.tv_usec/1000; ds_lseek(fd, SECTOR, SEEK_CUR); ds_write(fd, buffer, SECTOR); ds_gettimeofday(&tv); time2 = (float) tv.tv_sec*1000 + (float) tv.tv_usec/1000; outtime = time2 - time1; fprintf(fout, "%d ", count); fprintf(fout, "%f\n", outtime); } ds_close(fd); fclose(fout); }
static int wdsheader(struct dm_buf *hdr, char *src, char *device, char **pkg, PKCS7 *sig) { FILE *fp; char path[PATH_MAX], tmp_entry[ENTRY_MAX], tmp_file[L_tmpnam+1]; char srcpath[PATH_MAX]; int i, n; int list_fd; int block_cnt; int len; char cwd[MAXPATHLEN + 1]; boolean_t making_sig = B_FALSE; making_sig = (sig != NULL) ? B_TRUE : B_FALSE; (void) ds_close(0); if (dstdev.pathname) ds_fd = creat(device, 0644); else ds_fd = open(device, 1); if (ds_fd < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), device, errno); return (1); } if (ds_ginit(device) < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), device, errno); (void) ds_close(0); return (1); } /* * The loop below assures compatibility with tapes that don't * have a block size (e.g.: Exabyte) by forcing EOR at the end * of each 512 bytes. */ for (block_cnt = 0; block_cnt < hdr->allocation; block_cnt += BLK_SIZE) { (void) write(ds_fd, (hdr->text_buffer + block_cnt), BLK_SIZE); } /* * write the first cpio() archive to the datastream * which should contain the pkginfo & pkgmap files * for all packages */ (void) tmpnam(tmp_file); /* temporary file name */ if ((list_fd = open(tmp_file, O_RDWR | O_CREAT, 0644)) == -1) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), tmp_file); return (1); } /* * Create a cpio-compatible list of the requisite files in * the temporary file. */ if (!making_sig) { for (i = 0; pkg[i]; i++) { register ssize_t entry_size; /* * Copy pkginfo and pkgmap filenames into the * temporary string allowing for the first line * as a special case. */ entry_size = sprintf(tmp_entry, (i == 0) ? "%s/%s\n%s/%s" : "\n%s/%s\n%s/%s", pkg[i], PKGINFO, pkg[i], PKGMAP); if (write(list_fd, tmp_entry, entry_size) != entry_size) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), tmp_file); (void) close(list_fd); ecleanup(); return (1); } } } else { register ssize_t entry_size; /* * if we're making a signature, we must make a * temporary area full of symlinks to the requisite * files, plus an extra entry for the signature, so * that cpio will put all files and signature in the * same archive in a single invocation of cpio. */ tmpsymdir = xstrdup(tmpnam(NULL)); if (mkdir(tmpsymdir, S_IRWXU)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_MKDIR), tmpsymdir); return (1); } /* generate the signature */ if (((len = snprintf(path, PATH_MAX, "%s/%s", tmpsymdir, SIGNATURE_FILENAME)) >= PATH_MAX) || len < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), tmpsymdir); cleanup(); return (1); } if ((fp = fopen(path, "w")) == NULL) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), path); cleanup(); return (1); } (void) PEM_write_PKCS7(fp, sig); (void) fclose(fp); for (i = 0; pkg[i]; i++) { (void) snprintf(path, sizeof (path), "%s/%s", tmpsymdir, pkg[i]); if (mkdir(path, 0755)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_MKDIR), path); cleanup(); return (1); } (void) snprintf(path, sizeof (path), "%s/%s/%s", tmpsymdir, pkg[i], PKGINFO); (void) snprintf(srcpath, sizeof (srcpath), "%s/%s/%s", src, pkg[i], PKGINFO); if (symlink(srcpath, path) != 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_SYMLINK), path, srcpath); cleanup(); return (1); } (void) snprintf(path, sizeof (path), "%s/%s/%s", tmpsymdir, pkg[i], PKGMAP); (void) snprintf(srcpath, sizeof (srcpath), "%s/%s/%s", src, pkg[i], PKGMAP); if (symlink(srcpath, path) != 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_SYMLINK), path, srcpath); cleanup(); return (1); } /* * Copy pkginfo and pkgmap filenames into the * temporary string allowing for the first line * as a special case. */ entry_size = snprintf(tmp_entry, sizeof (tmp_entry), (i == 0) ? "%s/%s\n%s/%s" : "\n%s/%s\n%s/%s", pkg[i], PKGINFO, pkg[i], PKGMAP); if (write(list_fd, tmp_entry, entry_size) != entry_size) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), tmp_file); (void) close(list_fd); ecleanup(); cleanup(); return (1); } } /* add signature to list of files */ entry_size = snprintf(tmp_entry, sizeof (tmp_entry), "\n%s", SIGNATURE_FILENAME); if (write(list_fd, tmp_entry, entry_size) != entry_size) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOTMPFIL), tmp_file); (void) close(list_fd); ecleanup(); cleanup(); return (1); } } (void) lseek(list_fd, 0, SEEK_SET); if (!making_sig) { (void) snprintf(tmp_entry, sizeof (tmp_entry), "%s -ocD -C %d", CPIOPROC, (int)BLK_SIZE); } else { /* * when making a signature, we must make sure to follow * symlinks during the cpio so that we don't archive * the links themselves */ (void) snprintf(tmp_entry, sizeof (tmp_entry), "%s -ocDL -C %d", CPIOPROC, (int)BLK_SIZE); } if (making_sig) { /* save cwd and change to symlink dir for cpio invocation */ if (getcwd(cwd, MAXPATHLEN + 1) == NULL) { logerr(pkg_gt(ERR_GETWD)); progerr(pkg_gt(ERR_TRANSFER)); cleanup(); return (1); } if (chdir(tmpsymdir)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), tmpsymdir); cleanup(); return (1); } } if (n = esystem(tmp_entry, list_fd, ds_fd)) { rpterr(); progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CMDFAIL), tmp_entry, n); (void) close(list_fd); (void) unlink(tmp_file); cleanup(); return (1); } (void) close(list_fd); (void) unlink(tmp_file); if (making_sig) { /* change to back to src dir for subsequent operations */ if (chdir(cwd)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), cwd); cleanup(); return (1); } } return (0); }
static int pkgxfer(char *srcinst, int options) { int r; struct pkginfo info; FILE *fp, *pp; char *pt, *src, *dst; char dstdir[PATH_MAX], temp[PATH_MAX], srcdir[PATH_MAX], cmd[CMDSIZE], pkgname[NON_ABI_NAMELNGTH]; int i, n, part, nparts, maxpartsize, curpartcnt, iscomp; char volnos[128], tmpvol[128]; struct statvfs64 svfsb; longlong_t free_blocks; struct stat srcstat; info.pkginst = NULL; /* required initialization */ /* * when this routine is entered, the first part of * the package to transfer is already available in * the directory indicated by 'src' --- unless the * source device is a datstream, in which case only * the pkginfo and pkgmap files are available in 'src' */ src = srcdev.dirname; dst = dstdev.dirname; if (!(options & PT_SILENT)) (void) fprintf(stderr, pkg_gt(MSG_TRANSFER), srcinst); (void) strlcpy(dstinst, srcinst, sizeof (dstinst)); if (!(options & PT_ODTSTREAM)) { /* destination is a (possibly mounted) directory */ (void) snprintf(dstdir, sizeof (dstdir), "%s/%s", dst, dstinst); /* * need to check destination directory to assure * that we will not be duplicating a package which * already resides there (though we are allowed to * overwrite the same version) */ pkgdir = src; if (fpkginfo(&info, srcinst)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOEXISTS), srcinst); (void) fpkginfo(&info, NULL); return (1); } pkgdir = dst; (void) strlcpy(temp, srcinst, sizeof (temp)); if (pt = strchr(temp, '.')) *pt = '\0'; (void) strlcat(temp, ".*", sizeof (temp)); if (pt = fpkginst(temp, info.arch, info.version)) { /* * the same instance already exists, although * its pkgid might be different */ if (options & PT_OVERWRITE) { (void) strlcpy(dstinst, pt, sizeof (dstinst)); (void) snprintf(dstdir, sizeof (dstdir), "%s/%s", dst, dstinst); } else { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_DUPVERS), srcinst); (void) fpkginfo(&info, NULL); (void) fpkginst(NULL); return (2); } } else if (options & PT_RENAME) { /* * find next available instance by appending numbers * to the package abbreviation until the instance * does not exist in the destination directory */ if (pt = strchr(temp, '.')) *pt = '\0'; for (i = 2; (access(dstdir, 0) == 0); i++) { (void) snprintf(dstinst, sizeof (dstinst), "%s.%d", temp, i); (void) snprintf(dstdir, sizeof (dstdir), "%s/%s", dst, dstinst); } } else if (options & PT_OVERWRITE) { /* * we're allowed to overwrite, but there seems * to be no valid package to overwrite, and we are * not allowed to rename the destination, so act * as if we weren't given permission to overwrite * --- this keeps us from removing a destination * instance which is named the same as the source * instance, but really reflects a different pkg! */ options &= (~PT_OVERWRITE); } (void) fpkginfo(&info, NULL); (void) fpkginst(NULL); if (ckoverwrite(dst, dstinst, options)) return (2); if (isdir(dstdir) && mkdir(dstdir, 0755)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_MKDIR), dstdir); return (1); } (void) snprintf(srcdir, sizeof (srcdir), "%s/%s", src, srcinst); if (stat(srcdir, &srcstat) != -1) { if (chmod(dstdir, (srcstat.st_mode & S_IAMB)) == -1) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHMODDIR), dstdir); return (1); } } else { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_STATDIR), srcdir); return (1); } } if (!(options & PT_SILENT) && strcmp(dstinst, srcinst)) (void) fprintf(stderr, pkg_gt(MSG_RENAME), dstinst); (void) snprintf(srcdir, sizeof (srcdir), "%s/%s", src, srcinst); if (chdir(srcdir)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), srcdir); return (1); } if (ids_name) { /* unpack the datatstream into a directory */ /* * transfer pkginfo & pkgmap first */ (void) snprintf(cmd, sizeof (cmd), "%s -pudm %s", CPIOPROC, dstdir); if ((pp = epopen(cmd, "w")) == NULL) { rpterr(); progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_POPEN), cmd, errno); return (1); } (void) fprintf(pp, "%s\n%s\n", PKGINFO, PKGMAP); (void) sighold(SIGINT); (void) sighold(SIGHUP); r = epclose(pp); (void) sigrelse(SIGINT); (void) sigrelse(SIGHUP); if (r != 0) { rpterr(); progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_PCLOSE), cmd, errno); return (1); } if (options & PT_INFO_ONLY) return (0); /* don't transfer objects */ if (chdir(dstdir)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), dstdir); return (1); } /* * for each part of the package, use cpio() to * unpack the archive into the destination directory */ nparts = ds_findpkg(srcdev.cdevice, srcinst); if (nparts < 0) { progerr(pkg_gt(ERR_TRANSFER)); return (1); } for (part = 1; part <= nparts; /* void */) { if (ds_getpkg(srcdev.cdevice, part, dstdir)) { progerr(pkg_gt(ERR_TRANSFER)); return (1); } part++; if (dstdev.mount) { (void) chdir("/"); if (pkgumount(&dstdev)) return (1); if (part <= nparts) { if (n = pkgmount(&dstdev, NULL, part+1, nparts, 1)) return (n); if (ckoverwrite(dst, dstinst, options)) return (1); if (isdir(dstdir) && mkdir(dstdir, 0755)) { progerr( pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_MKDIR), dstdir); return (1); } /* * since volume is removable, each part * must contain a duplicate of the * pkginfo file to properly identify the * volume */ if (chdir(srcdir)) { progerr( pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), srcdir); return (1); } if ((pp = epopen(cmd, "w")) == NULL) { rpterr(); progerr( pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_POPEN), cmd, errno); return (1); } (void) fprintf(pp, "pkginfo"); (void) sighold(SIGINT); (void) sighold(SIGHUP); r = epclose(pp); (void) sigrelse(SIGINT); (void) sigrelse(SIGHUP); if (r != 0) { rpterr(); progerr( pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_PCLOSE), cmd, errno); return (1); } if (chdir(dstdir)) { progerr( pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CHDIR), dstdir); return (1); } } } } return (0); } if ((fp = fopen(PKGMAP, "r")) == NULL) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOPKGMAP), srcinst); return (1); } nparts = 1; if (!rd_map_size(fp, &nparts, &maxpartsize, &compressedsize)) return (1); else (void) fclose(fp); if (srcdev.mount) { if (ckvolseq(srcdir, 1, nparts)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_SEQUENCE)); return (1); } } /* write each part of this package */ if (options & PT_ODTSTREAM) { char line[128]; (void) mgets(line, 128); curpartcnt = -1; /* LINTED E_SEC_SCANF_UNBOUNDED_COPY */ if (sscanf(line, "%s %d %d %[ 0-9]", pkgname, &nparts, &maxpartsize, volnos) == 4) { (void) sscanf(volnos, "%d %[ 0-9]", &curpartcnt, tmpvol); (void) strlcpy(volnos, tmpvol, sizeof (volnos)); } } for (part = 1; part <= nparts; /* void */) { if (curpartcnt == 0 && (options & PT_ODTSTREAM)) { char prompt[128]; int index; ds_volno++; (void) ds_close(0); (void) sprintf(prompt, pkg_gt("Insert %%v %d of %d into %%p"), ds_volno, ds_volcnt); if (n = getvol(ods_name, NULL, DM_FORMAT, prompt)) return (n); if ((ds_fd = open(dstdev.cdevice, O_WRONLY)) < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), dstdev.cdevice, errno); return (1); } if (ds_ginit(dstdev.cdevice) < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), dstdev.cdevice, errno); (void) ds_close(0); return (1); } (void) sscanf(volnos, "%d %[ 0-9]", &index, tmpvol); (void) strlcpy(volnos, tmpvol, sizeof (volnos)); curpartcnt += index; } if (options & PT_INFO_ONLY) nparts = 0; if (part == 1) { (void) snprintf(cmd, sizeof (cmd), "find %s %s", PKGINFO, PKGMAP); if (nparts && (isdir(INSTALL) == 0)) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, INSTALL, sizeof (cmd)); } } else (void) snprintf(cmd, sizeof (cmd), "find %s", PKGINFO); if (nparts > 1) { (void) snprintf(temp, sizeof (temp), "%s.%d", RELOC, part); if (iscpio(temp, &iscomp) || isdir(temp) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, temp, sizeof (cmd)); } (void) snprintf(temp, sizeof (temp), "%s.%d", ROOT, part); if (iscpio(temp, &iscomp) || isdir(temp) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, temp, sizeof (cmd)); } (void) snprintf(temp, sizeof (temp), "%s.%d", ARCHIVE, part); if (isdir(temp) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, temp, sizeof (cmd)); } } else if (nparts) { for (i = 0; reloc_names[i] != NULL; i++) { if (iscpio(reloc_names[i], &iscomp) || isdir(reloc_names[i]) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, reloc_names[i], sizeof (cmd)); } } for (i = 0; root_names[i] != NULL; i++) { if (iscpio(root_names[i], &iscomp) || isdir(root_names[i]) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, root_names[i], sizeof (cmd)); } } if (isdir(ARCHIVE) == 0) { (void) strlcat(cmd, " ", sizeof (cmd)); (void) strlcat(cmd, ARCHIVE, sizeof (cmd)); } } if (options & PT_ODTSTREAM) { (void) snprintf(cmd + strlen(cmd), sizeof (cmd) - strlen(cmd), " -print | %s -ocD -C %d", CPIOPROC, (int)BLK_SIZE); } else { if (statvfs64(dstdir, &svfsb) == -1) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_STATVFS), dstdir, errno); return (1); } free_blocks = (((long)svfsb.f_frsize > 0) ? howmany(svfsb.f_frsize, DEV_BSIZE) : howmany(svfsb.f_bsize, DEV_BSIZE)) * svfsb.f_bavail; if ((has_comp_size ? compressedsize : maxpartsize) > free_blocks) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_NOSPACE), has_comp_size ? (long)compressedsize : (long)maxpartsize, free_blocks); return (1); } (void) snprintf(cmd + strlen(cmd), sizeof (cmd) - strlen(cmd), " -print | %s -pdum %s", CPIOPROC, dstdir); } n = esystem(cmd, -1, (options & PT_ODTSTREAM) ? ds_fd : -1); if (n) { rpterr(); progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CMDFAIL), cmd, n); return (1); } part++; if (srcdev.mount && (nparts > 1)) { /* unmount current source volume */ (void) chdir("/"); if (pkgumount(&srcdev)) return (1); /* loop until volume is mounted successfully */ while (part <= nparts) { /* read only */ n = pkgmount(&srcdev, NULL, part, nparts, 1); if (n) return (n); if (chdir(srcdir)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_CORRUPT)); (void) chdir("/"); (void) pkgumount(&srcdev); continue; } if (ckvolseq(srcdir, part, nparts)) { (void) chdir("/"); (void) pkgumount(&srcdev); continue; } break; } } if (!(options & PT_ODTSTREAM) && dstdev.mount) { /* unmount current volume */ if (pkgumount(&dstdev)) return (1); /* loop until next volume is mounted successfully */ while (part <= nparts) { /* writable */ n = pkgmount(&dstdev, NULL, part, nparts, 1); if (n) return (n); if (ckoverwrite(dst, dstinst, options)) continue; if (isdir(dstdir) && mkdir(dstdir, 0755)) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_MKDIR), dstdir); continue; } break; } } if ((options & PT_ODTSTREAM) && part <= nparts) { if (curpartcnt >= 0 && part > curpartcnt) { char prompt[128]; int index; ds_volno++; if (ds_close(0)) return (1); (void) sprintf(prompt, pkg_gt("Insert %%v %d of %d into %%p"), ds_volno, ds_volcnt); if (n = getvol(ods_name, NULL, DM_FORMAT, prompt)) return (n); if ((ds_fd = open(dstdev.cdevice, 1)) < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), dstdev.cdevice, errno); return (1); } if (ds_ginit(dstdev.cdevice) < 0) { progerr(pkg_gt(ERR_TRANSFER)); logerr(pkg_gt(MSG_OPEN), dstdev.cdevice, errno); (void) ds_close(0); return (1); } (void) sscanf(volnos, "%d %[ 0-9]", &index, tmpvol); (void) strlcpy(volnos, tmpvol, sizeof (volnos)); curpartcnt += index; } } } return (0); }
void quit(int retcode) { /* disable interrupts */ (void) signal(SIGINT, SIG_IGN); (void) signal(SIGHUP, SIG_IGN); /* process return code if not quit(99) */ if (retcode != 99) { if ((retcode % 10) == 0) { if (failflag) { retcode += 1; } else if (warnflag) { retcode += 2; } } if (ireboot) { retcode = (retcode % 10) + 20; } if (dreboot) { retcode = (retcode % 10) + 10; } } /* if set remove dstream temporary directory */ if (dstreamTempDir != (char *)NULL) { echoDebug(DBG_REMOVING_DSTREAM_TMPDIR, dstreamTempDir); (void) rrmdir(dstreamTempDir); dstreamTempDir = (char *)NULL; } /* If we're in dryrun mode, write out the dryrun file(s). */ if (in_dryrun_mode()) { char exit_msg[200]; set_dr_info(EXITCODE, retcode); if (failflag || warnflag) { set_dr_exitmsg(msgtext); } else { /* LINTED variable format specified */ (void) snprintf(exit_msg, sizeof (exit_msg), qreason(1, retcode, installStarted, includeZonename), (pkginst ? pkginst : "unknown"), zoneName); set_dr_exitmsg(exit_msg); } write_dryrun_file(extlist); ptext(stderr, MSG_DRYRUN_DONE); ptext(stderr, MSG_NOCHANGE); if (tmpdir[0] != NULL) (void) rrmdir(tmpdir); } else { /* fix bug #1082589 that deletes root file */ if (tmpdir[0] != NULL) { (void) rrmdir(tmpdir); } /* send mail to appropriate user list */ mailmsg(retcode); /* display message about this installation */ quitmsg(retcode); } /* * In the event that this quit() was called prior to completion of * the task, do an unlockinst() just in case. */ unlockinst(); /* Unmount anything that's our responsibility. */ (void) unmount_client(); /* * No need to umount device since calling process * was responsible for original mount */ if (!updatingExistingPackage) { if (!installStarted && pkgloc[0]) { /* * install not yet started; if package install * location is defined, remove the package. */ echoDebug(DBG_QUIT_REMOVING_PKGDIR, pkgloc); (void) chdir("/"); if (pkgloc[0]) { (void) rrmdir(pkgloc); } } } else { if (!installStarted) { /* * If we haven't started, but have already done * the <PKGINST>/install directory rename, then * remove the new <PKGINST>/install directory * and rename <PKGINST>/install.save back to * <PKGINST>/install. */ if (pkgloc_sav[0] && !access(pkgloc_sav, F_OK)) { if (pkgloc[0] && !access(pkgloc, F_OK)) (void) rrmdir(pkgloc); if (rename(pkgloc_sav, pkgloc) == -1) { progerr(ERR_PACKAGEBINREN, pkgloc_sav, pkgloc); } } } else { if (pkgloc_sav[0] && !access(pkgloc_sav, F_OK)) { echoDebug(DBG_QUIT_REMOVING_PKGSAV, pkgloc_sav); (void) rrmdir(pkgloc_sav); } } } /* * pkginst can be null if an administration setting doesn't all * the package to be installed. Make sure pkginst exeists before * updating the DB */ if (dparts > 0) ds_skiptoend(pkgdev.cdevice); (void) ds_close(1); /* Free the filesystem table. */ fs_tab_free(); /* Free the package information lists. */ pinfo_free(); /* Free all stragglers. */ bl_free(BL_ALL); (void) pathdup(NULL); /* Free regfiles. */ regfiles_free(); /* final exit debugging message */ echoDebug(DBG_EXIT_WITH_CODE, retcode); exit(retcode); /*NOTREACHED*/ }
int pkgexecv(char *filein, char *fileout, char *uname, char *gname, char *arg[]) { int exit_no; int n; int status; pid_t pid; pid_t waitstat; struct group *grp; struct passwd *pwp; struct sigaction nact; struct sigaction oact; void (*funcSighup)(int); void (*funcSigint)(int); /* flush standard i/o before creating new process */ (void) fflush(stdout); (void) fflush(stderr); /* * hold SIGINT/SIGHUP signals and reset signal received counter; * after the vfork() the parent and child need to setup their respective * interrupt handling and release the hold on the signals */ (void) sighold(SIGINT); (void) sighold(SIGHUP); sig_received = 0; /* * create new process to execute command in; * vfork() is being used to avoid duplicating the parents * memory space - this means that the child process may * not modify any of the parents memory including the * standard i/o descriptors - all the child can do is * adjust interrupts and open files as a prelude to a * call to exec(). */ pid = vfork(); if (pid < 0) { /* * ************************************************************* * fork failed! * ************************************************************* */ progerr(pkg_gt(ERR_FORK_FAILED), errno, strerror(errno)); /* release hold on signals */ (void) sigrelse(SIGHUP); (void) sigrelse(SIGINT); return (-1); } if (pid > 0) { /* * ************************************************************* * This is the forking (parent) process * ************************************************************* */ /* close datastream if any portion read */ if (ds_curpartcnt >= 0) { if (ds_close(0) != 0) { /* kill child process */ (void) sigsend(P_PID, pid, SIGKILL); /* release hold on signals */ (void) sigrelse(SIGHUP); (void) sigrelse(SIGINT); return (-1); } } /* * setup signal handlers for SIGINT and SIGHUP and release hold */ /* hook SIGINT to sig_trap() */ nact.sa_handler = sig_trap; nact.sa_flags = SA_RESTART; (void) sigemptyset(&nact.sa_mask); if (sigaction(SIGINT, &nact, &oact) < 0) { funcSigint = SIG_DFL; } else { funcSigint = oact.sa_handler; } /* hook SIGHUP to sig_trap() */ nact.sa_handler = sig_trap; nact.sa_flags = SA_RESTART; (void) sigemptyset(&nact.sa_mask); if (sigaction(SIGHUP, &nact, &oact) < 0) { funcSighup = SIG_DFL; } else { funcSighup = oact.sa_handler; } /* release hold on signals */ (void) sigrelse(SIGHUP); (void) sigrelse(SIGINT); /* * wait for the process to exit, reap child exit status */ for (;;) { status = 0; waitstat = waitpid(pid, (int *)&status, 0); if (waitstat < 0) { /* waitpid returned error */ if (errno == EAGAIN) { /* try again */ continue; } if (errno == EINTR) { continue; } /* error from waitpid: bail */ break; } else if (waitstat == pid) { /* child exit status available */ break; } } /* * reset signal handlers */ /* reset SIGINT */ nact.sa_handler = funcSigint; nact.sa_flags = SA_RESTART; (void) sigemptyset(&nact.sa_mask); (void) sigaction(SIGINT, &nact, (struct sigaction *)NULL); /* reset SIGHUP */ nact.sa_handler = funcSighup; nact.sa_flags = SA_RESTART; (void) sigemptyset(&nact.sa_mask); (void) sigaction(SIGHUP, &nact, (struct sigaction *)NULL); /* error if child process does not match */ if (waitstat != pid) { progerr(pkg_gt(ERR_WAIT_FAILED), pid, waitstat, status, errno, strerror(errno)); return (-1); } /* * determine final exit code: * - if signal received, then return interrupted (3) * - if child exit status is available, return exit child status * - otherwise return error (-1) */ if (sig_received != 0) { exit_no = 3; /* interrupted */ } else if (WIFEXITED(status)) { exit_no = WEXITSTATUS(status); } else { exit_no = -1; /* exec() or other process error */ } return (exit_no); } /* * ********************************************************************* * This is the forked (child) process * ********************************************************************* */ /* reset all signals to default */ for (n = 0; n < NSIG; n++) { (void) sigset(n, SIG_DFL); } /* release hold on signals held by parent before fork() */ (void) sigrelse(SIGHUP); (void) sigrelse(SIGINT); /* * The caller wants to have stdin connected to filein. */ if (filein && *filein) { /* * If input is supposed to be connected to /dev/tty */ if (strncmp(filein, "/dev/tty", 8) == 0) { /* * If stdin is connected to a tty device. */ if (isatty(STDIN_FILENO)) { /* * Reopen it to /dev/tty. */ n = open(filein, O_RDONLY); if (n >= 0) { (void) dup2(n, STDIN_FILENO); } } } else { /* * If we did not want to be connected to /dev/tty, we * connect input to the requested file no questions. */ n = open(filein, O_RDONLY); if (n >= 0) { (void) dup2(n, STDIN_FILENO); } } } /* * The caller wants to have stdout and stderr connected to fileout. * If "fileout" is "/dev/tty" then reconnect stdout to "/dev/tty" * only if /dev/tty is not already associated with "a tty". */ if (fileout && *fileout) { /* * If output is supposed to be connected to /dev/tty */ if (strncmp(fileout, "/dev/tty", 8) == 0) { /* * If stdout is connected to a tty device. */ if (isatty(STDOUT_FILENO)) { /* * Reopen it to /dev/tty if /dev/tty available. */ n = open(fileout, O_WRONLY); if (n >= 0) { /* * /dev/tty is available - close the * current standard output stream, and * reopen it on /dev/tty */ (void) dup2(n, STDOUT_FILENO); } } /* * not connected to tty device - probably redirect to * file - preserve existing output device */ } else { /* * If we did not want to be connected to /dev/tty, we * connect output to the requested file no questions. */ /* LINTED O_CREAT without O_EXCL specified in call to */ n = open(fileout, O_WRONLY|O_CREAT|O_APPEND, 0666); if (n >= 0) { (void) dup2(n, STDOUT_FILENO); } } /* * Dup stderr from stdout. */ (void) dup2(STDOUT_FILENO, STDERR_FILENO); } /* * do NOT close all file descriptors except stdio * file descriptors are passed in to some subcommands * (see dstream:ds_getinfo() and dstream:ds_putinfo()) */ /* set group/user i.d. if requested */ if (gname && *gname && (grp = cgrnam(gname)) != NULL) { if (setgid(grp->gr_gid) == -1) { progerr(pkg_gt(ERR_SETGID), grp->gr_gid); } } if (uname && *uname && (pwp = cpwnam(uname)) != NULL) { if (setuid(pwp->pw_uid) == -1) { progerr(pkg_gt(ERR_SETUID), pwp->pw_uid); } } /* execute target executable */ (void) execve(arg[0], arg, environ); progerr(pkg_gt(ERR_EX_FAIL), arg[0], errno); _exit(99); /*NOTREACHED*/ }
static ex_t display_words(bfpath *bfp, int argc, char **argv, bool show_probability) { byte buf[BUFSIZE]; buff_t *buff = buff_new(buf, 0, BUFSIZE); const byte *word; const char *path = bfp->filepath; const char *head_format = !show_probability ? "%-30s %6s %6s\n" : "%-30s %6s %6s %6s\n"; const char *data_format = !show_probability ? "%-30s %6lu %6lu\n" : "%-30s %6lu %6lu %f\n"; void *dsh = NULL; /* initialize to silence bogus gcc warning */ void *dbe; int rv = 0; ex_t ec = EX_OK; dsv_t msgcnts; /* protect against broken stat(2) that succeeds for empty names */ if (path == NULL || *path == '\0') { fprintf(stderr, "Expecting non-empty directory or file name.\n"); return EX_ERROR; } dbe = ds_init(bfp); dsh = ds_open(dbe, bfp, DS_READ);; if (dsh == NULL) /* print error, cleanup, and exit */ ds_open_failure(bfp, dbe); if (DST_OK != ds_txn_begin(dsh)) { ds_close(dsh); ds_cleanup(dbe); fprintf(stderr, "Cannot begin transaction.\n"); return EX_ERROR; } if (show_probability) { ds_get_msgcounts(dsh, &msgcnts); robs = ROBS; robx = ROBX; } fprintf(fpo, head_format, "", "spam", "good", " Fisher"); while (argc >= 0) { dsv_t val; word_t *token; int rc; unsigned long spam_count; unsigned long good_count; double rob_prob = 0.0; if (argc == 0) { if (get_token(buff, stdin) != 0) break; token = &buff->t; } else { word = (const byte *) *argv++; if (--argc == 0) argc = -1; token = word_news((const char *)word); } rc = ds_read(dsh, token, &val); switch (rc) { case 0: spam_count = val.spamcount; good_count = val.goodcount; if (!show_probability) fprintf(fpo, data_format, token->u.text, spam_count, good_count); else { rob_prob = calc_prob(good_count, spam_count, msgcnts.goodcount, msgcnts.spamcount); fprintf(fpo, data_format, token->u.text, spam_count, good_count, rob_prob); } break; case 1: break; default: fprintf(stderr, "Cannot read from database.\n"); ec = EX_ERROR; goto finish; } if (token != &buff->t) word_free(token); } finish: if (DST_OK != rv ? ds_txn_abort(dsh) : ds_txn_commit(dsh)) { fprintf(stderr, "Cannot %s transaction.\n", rv ? "abort" : "commit"); ec = EX_ERROR; } ds_close(dsh); ds_cleanup(dbe); buff_free(buff); return ec; }
static int load_wordlist(bfpath *bfp) { void *dsh; byte buf[BUFSIZE]; byte *p; int rv = 0; size_t len; int load_count = 0; unsigned long line = 0; unsigned long count[IX_SIZE], date; YYYYMMDD today_save = today; void *dbe = ds_init(bfp); dsh = ds_open(dbe, bfp, (dbmode_t)(DS_WRITE | DS_LOAD)); if (dsh == NULL) /* print error, cleanup, and exit */ ds_open_failure(bfp, dbe); memset(buf, '\0', BUFSIZE); if (DST_OK != ds_txn_begin(dsh)) exit(EX_ERROR); for (;;) { dsv_t data; word_t *token; if (fgets((char *)buf, BUFSIZE, fpin) == NULL) { if (ferror(fpin)) { perror(progname); rv = 2; } break; } line++; len = strlen((char *)buf); /* too short. */ if (len < 4) continue; p = spanword(buf); len = strlen((const char *)buf); if (max_token_len != 0 && len > max_token_len) continue; /* too long - discard */ spamcount = (uint) atoi((const char *)p); if ((int) spamcount < 0) spamcount = 0; p = spanword(p); goodcount = (uint) atoi((const char *)p); if ((int) goodcount < 0) goodcount = 0; p = spanword(p); date = (uint) atoi((const char *)p); p = spanword(p); if (*p != '\0') { fprintf(stderr, "%s: Unexpected input [%s] on line %lu. " "Expecting whitespace before count.\n", progname, buf, line); rv = 1; break; } if (date == 0) /* date as YYYYMMDD */ date = today_save; if (replace_nonascii_characters) do_replace_nonascii_characters(buf, len); token = word_new(buf, len); data.goodcount = goodcount; data.spamcount = spamcount; data.date = date; if (is_count((const char *)buf) && !(maintain && discard_token(token, &data))) { load_count += 1; /* Slower, but allows multiple lists to be concatenated */ set_date(date); switch (ds_read(dsh, token, &data)) { case 0: case 1: break; default: rv = 1; } data.spamcount += spamcount; data.goodcount += goodcount; if (ds_write(dsh, token, &data)) rv = 1; } word_free(token); } if (rv) { fprintf(stderr, "read or write error, aborting.\n"); ds_txn_abort(dsh); } else { switch (ds_txn_commit(dsh)) { case DST_FAILURE: case DST_TEMPFAIL: fprintf(stderr, "commit failed\n"); exit(EX_ERROR); case DST_OK: break; } } ds_close(dsh); ds_cleanup(dbe); if (verbose) fprintf(dbgout, "%d tokens loaded\n", load_count); return rv; }