15c51f124SMoriah Waterland /* 25c51f124SMoriah Waterland * CDDL HEADER START 35c51f124SMoriah Waterland * 45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the 55c51f124SMoriah Waterland * Common Development and Distribution License (the "License"). 65c51f124SMoriah Waterland * You may not use this file except in compliance with the License. 75c51f124SMoriah Waterland * 85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing. 105c51f124SMoriah Waterland * See the License for the specific language governing permissions 115c51f124SMoriah Waterland * and limitations under the License. 125c51f124SMoriah Waterland * 135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each 145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the 165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying 175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner] 185c51f124SMoriah Waterland * 195c51f124SMoriah Waterland * CDDL HEADER END 205c51f124SMoriah Waterland */ 215c51f124SMoriah Waterland 225c51f124SMoriah Waterland /* 235c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 245c51f124SMoriah Waterland * Use is subject to license terms. 255c51f124SMoriah Waterland */ 265c51f124SMoriah Waterland 275c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 285c51f124SMoriah Waterland /* All Rights Reserved */ 295c51f124SMoriah Waterland 305c51f124SMoriah Waterland 315c51f124SMoriah Waterland 325c51f124SMoriah Waterland #include <stdio.h> 335c51f124SMoriah Waterland #include <errno.h> 345c51f124SMoriah Waterland #include <string.h> 355c51f124SMoriah Waterland #include <stdlib.h> 365c51f124SMoriah Waterland #include <unistd.h> 375c51f124SMoriah Waterland #include <pkgdev.h> 385c51f124SMoriah Waterland #include <pkginfo.h> 395c51f124SMoriah Waterland #include <sys/types.h> 405c51f124SMoriah Waterland #include <devmgmt.h> 415c51f124SMoriah Waterland #include <sys/mount.h> 425c51f124SMoriah Waterland #include "pkglib.h" 435c51f124SMoriah Waterland #include "pkglibmsgs.h" 445c51f124SMoriah Waterland #include "pkglocale.h" 455c51f124SMoriah Waterland 465c51f124SMoriah Waterland /* libadm.a */ 475c51f124SMoriah Waterland extern int getvol(char *device, char *label, int options, char *prompt); 485c51f124SMoriah Waterland 495c51f124SMoriah Waterland #define CMDSIZ 256 505c51f124SMoriah Waterland 515c51f124SMoriah Waterland int 525c51f124SMoriah Waterland pkgmount(struct pkgdev *devp, char *pkg, int part, int nparts, int getvolflg) 535c51f124SMoriah Waterland { 545c51f124SMoriah Waterland int n; 555c51f124SMoriah Waterland char *pt, prompt[64], cmd[CMDSIZ]; 565c51f124SMoriah Waterland FILE *pp; 575c51f124SMoriah Waterland 585c51f124SMoriah Waterland if (getuid()) { 595c51f124SMoriah Waterland progerr(pkg_gt(ERR_NOTROOT)); 605c51f124SMoriah Waterland return (99); 615c51f124SMoriah Waterland } 625c51f124SMoriah Waterland 635c51f124SMoriah Waterland if (part && nparts) { 645c51f124SMoriah Waterland if (pkg) { 65*4656d474SGarrett D'Amore (void) snprintf(prompt, sizeof (prompt), 66*4656d474SGarrett D'Amore pkg_gt(LABEL0), part, nparts, pkg); 675c51f124SMoriah Waterland } else { 68*4656d474SGarrett D'Amore (void) snprintf(prompt, sizeof (prompt), 69*4656d474SGarrett D'Amore pkg_gt(LABEL1), part, nparts); 705c51f124SMoriah Waterland } 715c51f124SMoriah Waterland } else if (pkg) 72*4656d474SGarrett D'Amore (void) snprintf(prompt, sizeof (prompt), pkg_gt(LABEL2), pkg); 735c51f124SMoriah Waterland else 74*4656d474SGarrett D'Amore (void) snprintf(prompt, sizeof (prompt), pkg_gt(LABEL3)); 755c51f124SMoriah Waterland 765c51f124SMoriah Waterland n = 0; 775c51f124SMoriah Waterland for (;;) { 785c51f124SMoriah Waterland if (!getvolflg && n) 795c51f124SMoriah Waterland /* 805c51f124SMoriah Waterland * Return to caller if not prompting 815c51f124SMoriah Waterland * and error was encountered. 825c51f124SMoriah Waterland */ 835c51f124SMoriah Waterland return (-1); 845c51f124SMoriah Waterland if (getvolflg && (n = getvol(devp->bdevice, NULL, 855c51f124SMoriah Waterland (devp->rdonly ? 0 : DM_FORMFS|DM_WLABEL), prompt))) { 865c51f124SMoriah Waterland if (n == 3) 875c51f124SMoriah Waterland return (3); 885c51f124SMoriah Waterland if (n == 2) 895c51f124SMoriah Waterland progerr(pkg_gt("unknown device <%s>"), 905c51f124SMoriah Waterland devp->bdevice); 915c51f124SMoriah Waterland else 925c51f124SMoriah Waterland progerr( 935c51f124SMoriah Waterland pkg_gt("unable to obtain package volume")); 945c51f124SMoriah Waterland return (99); 955c51f124SMoriah Waterland } 965c51f124SMoriah Waterland 975c51f124SMoriah Waterland if (devp->fstyp == NULL) { 98*4656d474SGarrett D'Amore (void) snprintf(cmd, sizeof (cmd), 99*4656d474SGarrett D'Amore "%s %s", FSTYP, devp->bdevice); 1005c51f124SMoriah Waterland if ((pp = epopen(cmd, "r")) == NULL) { 1015c51f124SMoriah Waterland rpterr(); 1025c51f124SMoriah Waterland logerr(pkg_gt(ERR_FSTYP), devp->bdevice); 1035c51f124SMoriah Waterland n = -1; 1045c51f124SMoriah Waterland continue; 1055c51f124SMoriah Waterland } 1065c51f124SMoriah Waterland cmd[0] = '\0'; 1075c51f124SMoriah Waterland if (fgets(cmd, CMDSIZ, pp) == NULL) { 1085c51f124SMoriah Waterland logerr(pkg_gt(ERR_FSTYP), devp->bdevice); 1095c51f124SMoriah Waterland (void) pclose(pp); 1105c51f124SMoriah Waterland n = -1; 1115c51f124SMoriah Waterland continue; 1125c51f124SMoriah Waterland } 1135c51f124SMoriah Waterland if (epclose(pp)) { 1145c51f124SMoriah Waterland rpterr(); 1155c51f124SMoriah Waterland logerr(pkg_gt(ERR_FSTYP), devp->bdevice); 1165c51f124SMoriah Waterland n = -1; 1175c51f124SMoriah Waterland continue; 1185c51f124SMoriah Waterland } 1195c51f124SMoriah Waterland if (pt = strpbrk(cmd, " \t\n")) 1205c51f124SMoriah Waterland *pt = '\0'; 1215c51f124SMoriah Waterland if (cmd[0] == '\0') { 1225c51f124SMoriah Waterland logerr(pkg_gt(ERR_FSTYP), devp->bdevice); 1235c51f124SMoriah Waterland n = -1; 1245c51f124SMoriah Waterland continue; 1255c51f124SMoriah Waterland } 1265c51f124SMoriah Waterland devp->fstyp = strdup(cmd); 1275c51f124SMoriah Waterland } 1285c51f124SMoriah Waterland 1295c51f124SMoriah Waterland if (devp->rdonly) { 1305c51f124SMoriah Waterland n = pkgexecl(NULL, NULL, NULL, NULL, MOUNT, "-r", "-F", 1315c51f124SMoriah Waterland devp->fstyp, devp->bdevice, devp->mount, NULL); 1325c51f124SMoriah Waterland } else { 1335c51f124SMoriah Waterland n = pkgexecl(NULL, NULL, NULL, NULL, MOUNT, "-F", 1345c51f124SMoriah Waterland devp->fstyp, devp->bdevice, devp->mount, NULL); 1355c51f124SMoriah Waterland } 1365c51f124SMoriah Waterland if (n) { 1375c51f124SMoriah Waterland progerr(pkg_gt("mount of %s failed"), devp->bdevice); 1385c51f124SMoriah Waterland continue; 1395c51f124SMoriah Waterland } 1405c51f124SMoriah Waterland devp->mntflg++; 1415c51f124SMoriah Waterland break; 1425c51f124SMoriah Waterland } 1435c51f124SMoriah Waterland return (0); 1445c51f124SMoriah Waterland } 1455c51f124SMoriah Waterland 1465c51f124SMoriah Waterland int 1475c51f124SMoriah Waterland pkgumount(struct pkgdev *devp) 1485c51f124SMoriah Waterland { 1495c51f124SMoriah Waterland int n = 1; 1505c51f124SMoriah Waterland int retry = 10; 1515c51f124SMoriah Waterland 1525c51f124SMoriah Waterland if (!devp->mntflg) 1535c51f124SMoriah Waterland return (0); 1545c51f124SMoriah Waterland 1555c51f124SMoriah Waterland while (n != 0 && retry-- > 0) { 1565c51f124SMoriah Waterland n = pkgexecl(NULL, NULL, NULL, NULL, UMOUNT, devp->bdevice, 1575c51f124SMoriah Waterland NULL); 1585c51f124SMoriah Waterland if (n != 0) { 1595c51f124SMoriah Waterland progerr(pkg_gt("retrying umount of %s"), 1605c51f124SMoriah Waterland devp->bdevice); 161*4656d474SGarrett D'Amore (void) sleep(5); 1625c51f124SMoriah Waterland } 1635c51f124SMoriah Waterland } 1645c51f124SMoriah Waterland if (n == 0) 1655c51f124SMoriah Waterland devp->mntflg = 0; 1665c51f124SMoriah Waterland return (n); 1675c51f124SMoriah Waterland } 168