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
222e10def1SPeter Tribble /*
232e10def1SPeter Tribble * Copyright (c) 2017 Peter Tribble.
242e10def1SPeter Tribble */
252e10def1SPeter Tribble
265c51f124SMoriah Waterland /*
27214c2196SCasper H.S. Dik * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
285c51f124SMoriah Waterland */
295c51f124SMoriah Waterland
305c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
315c51f124SMoriah Waterland /* All Rights Reserved */
325c51f124SMoriah Waterland
335c51f124SMoriah Waterland
345c51f124SMoriah Waterland #include <stdio.h>
355c51f124SMoriah Waterland #include <fcntl.h>
365c51f124SMoriah Waterland #include <sys/types.h>
375c51f124SMoriah Waterland #include <sys/param.h>
385c51f124SMoriah Waterland #include <sys/sysmacros.h>
395c51f124SMoriah Waterland #include <string.h>
405c51f124SMoriah Waterland #include <strings.h>
415c51f124SMoriah Waterland #include <sys/wait.h>
425c51f124SMoriah Waterland #include <sys/stat.h>
435c51f124SMoriah Waterland #include <sys/mman.h>
445c51f124SMoriah Waterland #include <sys/statvfs.h>
455c51f124SMoriah Waterland #include <signal.h>
465c51f124SMoriah Waterland #include <limits.h>
475c51f124SMoriah Waterland #include <errno.h>
485c51f124SMoriah Waterland #include <fcntl.h>
495c51f124SMoriah Waterland #include <stdlib.h>
505c51f124SMoriah Waterland #include <unistd.h>
515c51f124SMoriah Waterland #include <time.h>
525c51f124SMoriah Waterland #include <errno.h>
535c51f124SMoriah Waterland #include <pkglocs.h>
545c51f124SMoriah Waterland #include <locale.h>
555c51f124SMoriah Waterland #include <libintl.h>
565c51f124SMoriah Waterland #include <pkglib.h>
575c51f124SMoriah Waterland #include "libinst.h"
585c51f124SMoriah Waterland #include "libadm.h"
595c51f124SMoriah Waterland
6062224350SCasper H.S. Dik #define LOCKFILE ".pkg.lock.client"
6162224350SCasper H.S. Dik #define LOCKFILESERV ".pkg.lock"
6262224350SCasper H.S. Dik
635c51f124SMoriah Waterland #define LOCKWAIT 10 /* seconds between retries */
645c51f124SMoriah Waterland #define LOCKRETRY 20 /* number of retries for a DB lock */
655c51f124SMoriah Waterland
6662224350SCasper H.S. Dik #define ERR_COMMIT "WARNING: unable to commit contents database update"
675c51f124SMoriah Waterland #define ERR_NOCLOSE "WARNING: unable to close <%s>"
685c51f124SMoriah Waterland #define ERR_NOUNLINK_LATENT "WARNING: unable to unlink latent <%s>"
695c51f124SMoriah Waterland #define ERR_LINK_FAIL "link(%s, %s) failed (errno %d)"
705c51f124SMoriah Waterland #define ERR_NORENAME_CONTENTS "unable to establish contents file <%s> "\
715c51f124SMoriah Waterland "from <%s>"
725c51f124SMoriah Waterland #define ERR_RENAME_FAIL "rename(%s, %s) failed (errno %d)"
735c51f124SMoriah Waterland #define ERR_RESTORE_FAIL "attempt to restore <%s> failed"
745c51f124SMoriah Waterland #define ERR_NOUNLINK "WARNING: unable to unlink <%s>"
755c51f124SMoriah Waterland #define ERR_FCLOSE_FAIL "fclose failed (errno %d)"
765c51f124SMoriah Waterland #define ERR_ERRNO "(errno %d: %s)"
775c51f124SMoriah Waterland #define ERR_NOTMPOPEN "unable to open temporary contents file image"
785c51f124SMoriah Waterland #define ERR_CFBACK "Not enough space to backup <%s>"
795c51f124SMoriah Waterland #define ERR_CREAT_CONT "unable to create contents file <%s>: %s"
805c51f124SMoriah Waterland #define ERR_ACCESS_CONT "unable to access contents file <%s>: %s"
815c51f124SMoriah Waterland #define ERR_CFBACK1 "Need=%llu blocks, Available=%llu blocks " \
825c51f124SMoriah Waterland "(block size=%d)"
835c51f124SMoriah Waterland #define ERR_NOCFILE "unable to locate contents file <%s>"
845c51f124SMoriah Waterland #define ERR_NOROPEN "unable to open <%s> for reading"
855c51f124SMoriah Waterland #define ERR_NOOPEN "unable to open <%s> for writing"
865c51f124SMoriah Waterland #define ERR_NOSTAT "unable to stat contents file <%s>"
875c51f124SMoriah Waterland #define ERR_NOSTATV "statvfs(%s) failed"
885c51f124SMoriah Waterland #define ERR_NOUPD "unable to update contents file"
895c51f124SMoriah Waterland #define ERR_DRCONTCP "unable to copy contents file to <%s>"
905c51f124SMoriah Waterland
915c51f124SMoriah Waterland #define MSG_XWTING "NOTE: Waiting for exclusive access to the package " \
925c51f124SMoriah Waterland "database."
935c51f124SMoriah Waterland #define MSG_NOLOCK "NOTE: Couldn't lock the package database."
945c51f124SMoriah Waterland
955c51f124SMoriah Waterland #define ERR_NOLOCK "Database lock failed."
965c51f124SMoriah Waterland #define ERR_OPLOCK "unable to open lock file <%s>."
975c51f124SMoriah Waterland #define ERR_MKLOCK "unable to create lock file <%s>."
985c51f124SMoriah Waterland #define ERR_LCKREM "unable to lock package database - remote host " \
995c51f124SMoriah Waterland "unavailable."
1005c51f124SMoriah Waterland #define ERR_BADLCK "unable to lock package database - unknown error."
1015c51f124SMoriah Waterland #define ERR_DEADLCK "unable to lock package database - deadlock condition."
1025c51f124SMoriah Waterland #define ERR_TMOUT "unable to lock package database - too many retries."
1035c51f124SMoriah Waterland #define ERR_CFDIR "unable to locate contents file directory"
1045c51f124SMoriah Waterland
1055c51f124SMoriah Waterland static int active_lock;
1065c51f124SMoriah Waterland static int lock_fd; /* fd of LOCKFILE. */
1075c51f124SMoriah Waterland static char *pkgadm_dir;
1085c51f124SMoriah Waterland
10962224350SCasper H.S. Dik int pkgWlock(int verbose);
1105c51f124SMoriah Waterland static int pkgWunlock(void);
1115c51f124SMoriah Waterland
1125c51f124SMoriah Waterland /* forward declarations */
1135c51f124SMoriah Waterland
1145c51f124SMoriah Waterland int relslock(void);
1155c51f124SMoriah Waterland
1165c51f124SMoriah Waterland /*ARGSUSED*/
1175c51f124SMoriah Waterland static void
do_alarm(int n)1185c51f124SMoriah Waterland do_alarm(int n)
1195c51f124SMoriah Waterland {
1205c51f124SMoriah Waterland (void) signal(SIGALRM, SIG_IGN);
1215c51f124SMoriah Waterland (void) signal(SIGALRM, do_alarm);
1225c51f124SMoriah Waterland (void) alarm(LOCKWAIT);
1235c51f124SMoriah Waterland }
1245c51f124SMoriah Waterland
1255c51f124SMoriah Waterland /*
1265c51f124SMoriah Waterland * Point packaging to the appropriate contents file. This is primarily used
1275c51f124SMoriah Waterland * to establish a dryrun contents file. If the malloc() doesn't work, this
1285c51f124SMoriah Waterland * returns 99 (internal error), else 0.
1295c51f124SMoriah Waterland */
1305c51f124SMoriah Waterland int
set_cfdir(char * cfdir)1315c51f124SMoriah Waterland set_cfdir(char *cfdir)
1325c51f124SMoriah Waterland {
1335c51f124SMoriah Waterland char realcf[PATH_MAX];
1345c51f124SMoriah Waterland char tmpcf[PATH_MAX];
1355c51f124SMoriah Waterland int status;
1365c51f124SMoriah Waterland
1375c51f124SMoriah Waterland if (cfdir == NULL) {
1385c51f124SMoriah Waterland pkgadm_dir = get_PKGADM();
1395c51f124SMoriah Waterland return (0);
1405c51f124SMoriah Waterland }
1415c51f124SMoriah Waterland
1425c51f124SMoriah Waterland if ((pkgadm_dir = strdup(cfdir)) == NULL) {
1435c51f124SMoriah Waterland return (99);
1445c51f124SMoriah Waterland }
1455c51f124SMoriah Waterland
1465c51f124SMoriah Waterland (void) snprintf(tmpcf, sizeof (tmpcf), "%s/contents", pkgadm_dir);
1475c51f124SMoriah Waterland
1485c51f124SMoriah Waterland /*
1495c51f124SMoriah Waterland * return if a temporary contents file already exists -
1505c51f124SMoriah Waterland * assume it is from a prior package in this series.
1515c51f124SMoriah Waterland */
1525c51f124SMoriah Waterland
1535c51f124SMoriah Waterland if (access(tmpcf, F_OK) == 0) {
1545c51f124SMoriah Waterland return (0);
1555c51f124SMoriah Waterland }
1565c51f124SMoriah Waterland
1575c51f124SMoriah Waterland /*
1585c51f124SMoriah Waterland * no temporary contents file exists - create one.
1595c51f124SMoriah Waterland */
1605c51f124SMoriah Waterland
1615c51f124SMoriah Waterland (void) snprintf(realcf, sizeof (realcf), "%s/contents", get_PKGADM());
1625c51f124SMoriah Waterland
1635c51f124SMoriah Waterland /*
1645c51f124SMoriah Waterland * If there's a contents file there already, copy it
16562224350SCasper H.S. Dik * over, otherwise initialize one. Make sure that the
16662224350SCasper H.S. Dik * server, if running, flushes the contents file.
1675c51f124SMoriah Waterland */
1685c51f124SMoriah Waterland
16962224350SCasper H.S. Dik (void) pkgsync(NULL, get_PKGADM(), B_FALSE);
17062224350SCasper H.S. Dik
1715c51f124SMoriah Waterland /* create new contents file if one does not already exist */
1725c51f124SMoriah Waterland
1735c51f124SMoriah Waterland if (access(realcf, F_OK) != 0) {
1745c51f124SMoriah Waterland int n;
1755c51f124SMoriah Waterland
1765c51f124SMoriah Waterland n = open(tmpcf, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
1775c51f124SMoriah Waterland if (n < 0) {
1785c51f124SMoriah Waterland progerr(gettext(ERR_CREAT_CONT), tmpcf,
17962224350SCasper H.S. Dik strerror(errno));
1805c51f124SMoriah Waterland return (99);
1815c51f124SMoriah Waterland }
1825c51f124SMoriah Waterland (void) close(n);
1835c51f124SMoriah Waterland } else {
1845c51f124SMoriah Waterland
1855c51f124SMoriah Waterland /* contents file exists, save in pkgadm-dir */
1865c51f124SMoriah Waterland
1875c51f124SMoriah Waterland status = copyf(realcf, tmpcf, (time_t)0);
1885c51f124SMoriah Waterland if (status != 0) {
1895c51f124SMoriah Waterland progerr(gettext(ERR_DRCONTCP), tmpcf);
1905c51f124SMoriah Waterland return (99);
1915c51f124SMoriah Waterland }
1925c51f124SMoriah Waterland }
1935c51f124SMoriah Waterland
1945c51f124SMoriah Waterland return (0);
1955c51f124SMoriah Waterland }
1965c51f124SMoriah Waterland
1975c51f124SMoriah Waterland /*
1985c51f124SMoriah Waterland * This function installs the database lock, opens the contents file for
1995c51f124SMoriah Waterland * reading and creates and opens the temporary contents file for read/write.
2005c51f124SMoriah Waterland * It returns 1 if successful, 0 otherwise.
2015c51f124SMoriah Waterland */
2025c51f124SMoriah Waterland int
ocfile(PKGserver * server,VFP_T ** r_tmpvfp,fsblkcnt_t map_blks)20362224350SCasper H.S. Dik ocfile(PKGserver *server, VFP_T **r_tmpvfp, fsblkcnt_t map_blks)
2045c51f124SMoriah Waterland {
205214c2196SCasper H.S. Dik struct stat64 statb, statl;
2065c51f124SMoriah Waterland struct statvfs64 svfsb;
2075c51f124SMoriah Waterland fsblkcnt_t free_blocks;
2085c51f124SMoriah Waterland fsblkcnt_t need_blocks;
209214c2196SCasper H.S. Dik fsblkcnt_t log_blocks;
2105c51f124SMoriah Waterland VFP_T *tmpvfp = (VFP_T *)NULL;
2115c51f124SMoriah Waterland char contents[PATH_MAX];
212214c2196SCasper H.S. Dik char logfile[PATH_MAX];
21362224350SCasper H.S. Dik off_t cdiff_alloc;
21462224350SCasper H.S. Dik PKGserver newserver;
2155c51f124SMoriah Waterland
2165c51f124SMoriah Waterland /* establish package administration contents directory location */
2175c51f124SMoriah Waterland
2185c51f124SMoriah Waterland if (pkgadm_dir == NULL) {
2195c51f124SMoriah Waterland if (set_cfdir(NULL) != 0) {
2205c51f124SMoriah Waterland progerr(gettext(ERR_CFDIR));
2215c51f124SMoriah Waterland return (0);
2225c51f124SMoriah Waterland }
2235c51f124SMoriah Waterland }
2245c51f124SMoriah Waterland
2255c51f124SMoriah Waterland /* Lock the file for exclusive access */
2265c51f124SMoriah Waterland
2275c51f124SMoriah Waterland if (!pkgWlock(1)) {
2285c51f124SMoriah Waterland progerr(gettext(ERR_NOLOCK));
2295c51f124SMoriah Waterland return (0);
2305c51f124SMoriah Waterland }
2315c51f124SMoriah Waterland
23262224350SCasper H.S. Dik if (*server != NULL) {
23362224350SCasper H.S. Dik vfpTruncate(*r_tmpvfp);
23462224350SCasper H.S. Dik (void) vfpClearModified(*r_tmpvfp);
2355c51f124SMoriah Waterland
23662224350SCasper H.S. Dik return (1);
23762224350SCasper H.S. Dik }
2385c51f124SMoriah Waterland
23962224350SCasper H.S. Dik newserver = pkgopenserver(NULL, pkgadm_dir, B_FALSE);
2405c51f124SMoriah Waterland
24162224350SCasper H.S. Dik /* The error has been reported. */
24262224350SCasper H.S. Dik if (newserver == NULL)
24362224350SCasper H.S. Dik return (0);
2445c51f124SMoriah Waterland
24562224350SCasper H.S. Dik /* reset return VFP/FILE pointers */
2465c51f124SMoriah Waterland
24762224350SCasper H.S. Dik (*r_tmpvfp) = (VFP_T *)NULL;
2485c51f124SMoriah Waterland
24962224350SCasper H.S. Dik /* determine path to the primary contents file */
25062224350SCasper H.S. Dik (void) snprintf(contents, sizeof (contents), "%s/contents", pkgadm_dir);
2515c51f124SMoriah Waterland
2525c51f124SMoriah Waterland /*
2535c51f124SMoriah Waterland * Check and see if there is enough space for the packaging commands
2545c51f124SMoriah Waterland * to back up the contents file, if there is not, then do not allow
2555c51f124SMoriah Waterland * execution to continue by failing the ocfile() call.
2565c51f124SMoriah Waterland */
2575c51f124SMoriah Waterland
2585c51f124SMoriah Waterland /* Get the contents file size */
2595c51f124SMoriah Waterland
26062224350SCasper H.S. Dik if (stat64(contents, &statb) == -1) {
2615c51f124SMoriah Waterland int lerrno = errno;
2625c51f124SMoriah Waterland
26362224350SCasper H.S. Dik progerr(gettext(ERR_NOCFILE), contents);
2645c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
26562224350SCasper H.S. Dik pkgcloseserver(newserver);
2665c51f124SMoriah Waterland return (0);
2675c51f124SMoriah Waterland }
2685c51f124SMoriah Waterland
2695c51f124SMoriah Waterland /* Get the filesystem space */
2705c51f124SMoriah Waterland
27162224350SCasper H.S. Dik if (statvfs64(contents, &svfsb) == -1) {
2725c51f124SMoriah Waterland int lerrno = errno;
2735c51f124SMoriah Waterland
2745c51f124SMoriah Waterland progerr(gettext(ERR_NOSTATV), contents);
2755c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
27662224350SCasper H.S. Dik pkgcloseserver(newserver);
2775c51f124SMoriah Waterland return (0);
2785c51f124SMoriah Waterland }
2795c51f124SMoriah Waterland
2805c51f124SMoriah Waterland free_blocks = (((fsblkcnt_t)svfsb.f_frsize > 0) ?
281214c2196SCasper H.S. Dik howmany(svfsb.f_frsize, DEV_BSIZE) :
282214c2196SCasper H.S. Dik howmany(svfsb.f_bsize, DEV_BSIZE)) * svfsb.f_bfree;
2835c51f124SMoriah Waterland
284214c2196SCasper H.S. Dik /* determine blocks used by the logfile */
285214c2196SCasper H.S. Dik (void) snprintf(logfile, sizeof (logfile), "%s/" PKGLOG, pkgadm_dir);
28662224350SCasper H.S. Dik
287214c2196SCasper H.S. Dik if (stat64(logfile, &statl) == -1)
288214c2196SCasper H.S. Dik log_blocks = 0;
289214c2196SCasper H.S. Dik else
290214c2196SCasper H.S. Dik log_blocks = nblk(statl.st_size, svfsb.f_bsize, svfsb.f_frsize);
2915c51f124SMoriah Waterland
2925c51f124SMoriah Waterland /*
2935c51f124SMoriah Waterland * Calculate the number of blocks we need to be able to operate on
294214c2196SCasper H.S. Dik * the contents file and the log file.
295214c2196SCasper H.S. Dik * When adding a package (map_blks > 0), we add the size of the
296214c2196SCasper H.S. Dik * pkgmap file times 1.5 as the pkgmap is a bit smaller then the
297214c2196SCasper H.S. Dik * lines added to the contents file. That data is written both to
298214c2196SCasper H.S. Dik * the new contents file and the log file (2 * 1.5 * map_blks).
299214c2196SCasper H.S. Dik * The new contents file is limited by the size of the current
300214c2196SCasper H.S. Dik * contents file and the increased log file.
301214c2196SCasper H.S. Dik * If we're removing a package, then the log might grow to the size
302214c2196SCasper H.S. Dik * of the full contents file but then the new contents file would
303214c2196SCasper H.S. Dik * be zero and so we only need to add the size of the contents file.
3045c51f124SMoriah Waterland */
305214c2196SCasper H.S. Dik need_blocks = map_blks * 3 +
306214c2196SCasper H.S. Dik /* Current log file */
307214c2196SCasper H.S. Dik log_blocks +
308214c2196SCasper H.S. Dik /* Current contents file */
309214c2196SCasper H.S. Dik nblk(statb.st_size, svfsb.f_bsize, svfsb.f_frsize);
3105c51f124SMoriah Waterland
3115c51f124SMoriah Waterland if ((need_blocks + 10) > free_blocks) {
3125c51f124SMoriah Waterland progerr(gettext(ERR_CFBACK), contents);
3135c51f124SMoriah Waterland progerr(gettext(ERR_CFBACK1), need_blocks, free_blocks,
314214c2196SCasper H.S. Dik DEV_BSIZE);
31562224350SCasper H.S. Dik pkgcloseserver(newserver);
3165c51f124SMoriah Waterland return (0);
3175c51f124SMoriah Waterland }
3185c51f124SMoriah Waterland
3195c51f124SMoriah Waterland /*
3205c51f124SMoriah Waterland * open the temporary contents file without a path name - this causes
3215c51f124SMoriah Waterland * the "vfp" to be opened on in-memory storage only, the size of which
3225c51f124SMoriah Waterland * is set following a successful return - this causes the temporary
3235c51f124SMoriah Waterland * contents file to be maintained in memory only - if no changes are
3245c51f124SMoriah Waterland * made as the primary contents file is processed, the in memory data
3255c51f124SMoriah Waterland * is discarded and not written to the disk.
3265c51f124SMoriah Waterland */
3275c51f124SMoriah Waterland
3285c51f124SMoriah Waterland if (vfpOpen(&tmpvfp, (char *)NULL, "w", VFP_NONE) != 0) {
3295c51f124SMoriah Waterland int lerrno = errno;
3305c51f124SMoriah Waterland
3315c51f124SMoriah Waterland progerr(gettext(ERR_NOTMPOPEN));
3325c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
33362224350SCasper H.S. Dik pkgcloseserver(newserver);
3345c51f124SMoriah Waterland return (0);
3355c51f124SMoriah Waterland }
3365c51f124SMoriah Waterland
3375c51f124SMoriah Waterland /*
3385c51f124SMoriah Waterland * set size of allocation for temporary contents file - this sets the
3395c51f124SMoriah Waterland * size of the in-memory buffer associated with the open vfp.
34062224350SCasper H.S. Dik * We only store the new and changed entries.
34162224350SCasper H.S. Dik * We allocate memory depending on the size of the pkgmap; it's not
34262224350SCasper H.S. Dik * completely right but <some value + * 1.5 * map_blks * DEV_BSIZE>
34362224350SCasper H.S. Dik * seems fine (an install adds the size if the name of the package.)
3445c51f124SMoriah Waterland */
3455c51f124SMoriah Waterland
34662224350SCasper H.S. Dik cdiff_alloc = map_blks * DEV_BSIZE;
34762224350SCasper H.S. Dik cdiff_alloc += cdiff_alloc/2;
34862224350SCasper H.S. Dik if (cdiff_alloc < 1000000)
34962224350SCasper H.S. Dik cdiff_alloc += 1000000;
35062224350SCasper H.S. Dik
35162224350SCasper H.S. Dik if (vfpSetSize(tmpvfp, cdiff_alloc) != 0) {
3525c51f124SMoriah Waterland int lerrno = errno;
3535c51f124SMoriah Waterland
3545c51f124SMoriah Waterland progerr(gettext(ERR_NOTMPOPEN));
3555c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
3565c51f124SMoriah Waterland (void) vfpClose(&tmpvfp);
35762224350SCasper H.S. Dik pkgcloseserver(newserver);
3585c51f124SMoriah Waterland return (0);
3595c51f124SMoriah Waterland }
3605c51f124SMoriah Waterland
36162224350SCasper H.S. Dik /* set return ->s to open server/vfps */
3625c51f124SMoriah Waterland
3635c51f124SMoriah Waterland (*r_tmpvfp) = tmpvfp;
36462224350SCasper H.S. Dik *server = newserver;
3655c51f124SMoriah Waterland
3665c51f124SMoriah Waterland return (1); /* All OK */
3675c51f124SMoriah Waterland }
3685c51f124SMoriah Waterland
3695c51f124SMoriah Waterland /*
3705c51f124SMoriah Waterland * This is a simple open and lock of the contents file. It doesn't create a
3715c51f124SMoriah Waterland * temporary contents file and it doesn't need to do any space checking.
3725c51f124SMoriah Waterland * Returns 1 for OK and 0 for "didn't do it".
3735c51f124SMoriah Waterland */
3745c51f124SMoriah Waterland int
socfile(PKGserver * server,boolean_t quiet)37562224350SCasper H.S. Dik socfile(PKGserver *server, boolean_t quiet)
3765c51f124SMoriah Waterland {
377*014740deSToomas Soome boolean_t readonly = B_FALSE;
37862224350SCasper H.S. Dik PKGserver newserver;
3795c51f124SMoriah Waterland
3805c51f124SMoriah Waterland if (pkgadm_dir == NULL) {
3815c51f124SMoriah Waterland if (set_cfdir(NULL) != 0) {
3825c51f124SMoriah Waterland progerr(gettext(ERR_CFDIR));
3835c51f124SMoriah Waterland return (0);
3845c51f124SMoriah Waterland }
3855c51f124SMoriah Waterland }
3865c51f124SMoriah Waterland
3875c51f124SMoriah Waterland /*
3885c51f124SMoriah Waterland * Lock the database for exclusive access, but don't make a fuss if
3895c51f124SMoriah Waterland * it fails (user may not be root and the .pkg.lock file may not
3905c51f124SMoriah Waterland * exist yet).
3915c51f124SMoriah Waterland */
3925c51f124SMoriah Waterland
3935c51f124SMoriah Waterland if (!pkgWlock(0)) {
39462224350SCasper H.S. Dik if (!quiet)
39562224350SCasper H.S. Dik logerr(gettext(MSG_NOLOCK));
39662224350SCasper H.S. Dik readonly = B_TRUE;
3975c51f124SMoriah Waterland }
3985c51f124SMoriah Waterland
39962224350SCasper H.S. Dik newserver = pkgopenserver(NULL, pkgadm_dir, readonly);
40062224350SCasper H.S. Dik if (newserver == NULL)
4015c51f124SMoriah Waterland return (0);
4025c51f124SMoriah Waterland
40362224350SCasper H.S. Dik *server = newserver;
4045c51f124SMoriah Waterland return (1);
4055c51f124SMoriah Waterland }
4065c51f124SMoriah Waterland
4075c51f124SMoriah Waterland /*
4085c51f124SMoriah Waterland * Name: swapcfile
4095c51f124SMoriah Waterland * Description: This function closes both the current and temporary contents
4105c51f124SMoriah Waterland * files specified, and conditionally replaces the old transitory
4115c51f124SMoriah Waterland * contents file with the newly updated temporary contents file.
4125c51f124SMoriah Waterland * The "ocfile()" or "socfile()" functions must be called to re-
4135c51f124SMoriah Waterland * open the real contents file for processing.
41462224350SCasper H.S. Dik * Arguments: PKGserver - handle to the package database
4155c51f124SMoriah Waterland * a_cfTmpVfp - (VFP_T **) - [RW, *RW]
41662224350SCasper H.S. Dik * This is the VFP associated which contains all the
41762224350SCasper H.S. Dik * modifications to be written back to the database.
4185c51f124SMoriah Waterland * file that is being written to.
4195c51f124SMoriah Waterland * pkginst - (char) - [RO, *RO]
4205c51f124SMoriah Waterland * This is the name of the package being operated on;
4215c51f124SMoriah Waterland * this is used to write the "last modified by xxx"
4225c51f124SMoriah Waterland * comment at the end of the contents file.
4235c51f124SMoriah Waterland * dbchg - (int) - [RO]
4245c51f124SMoriah Waterland * == 0 - the temporary contents file has NOT been changed
4255c51f124SMoriah Waterland * with respect to the real contents file; do not
4265c51f124SMoriah Waterland * update the real contents file with the contents
4275c51f124SMoriah Waterland * of the temporary contents file.
4285c51f124SMoriah Waterland * != 0 - the temporary contetns file HAS been changed with
4295c51f124SMoriah Waterland * respect to the real contents file; DO update the
4305c51f124SMoriah Waterland * real contents file with the contents of the
4315c51f124SMoriah Waterland * temporary contents file.
4325c51f124SMoriah Waterland * Returns: int == RESULT_OK - successful
4335c51f124SMoriah Waterland * == RESULT_WRN - successful with warnings
4345c51f124SMoriah Waterland * == RESULT_ERR - failed with fatal errors - deserves an
4355c51f124SMoriah Waterland * alarming message and a quit()
4365c51f124SMoriah Waterland * NOTES: If dbchg != 0, the contents file is always updated. If dbchg == 0,
4375c51f124SMoriah Waterland * the contents file is updated IF the data is modified indication
4385c51f124SMoriah Waterland * is set on the contents file associated with a_cfTmpVfp.
4395c51f124SMoriah Waterland */
4405c51f124SMoriah Waterland
4415c51f124SMoriah Waterland int
swapcfile(PKGserver server,VFP_T ** a_cfTmpVfp,char * pkginst,int dbchg)44262224350SCasper H.S. Dik swapcfile(PKGserver server, VFP_T **a_cfTmpVfp, char *pkginst, int dbchg)
4435c51f124SMoriah Waterland {
4445c51f124SMoriah Waterland char *pe;
4455c51f124SMoriah Waterland char *pl;
4465c51f124SMoriah Waterland char *ps;
4475c51f124SMoriah Waterland char line[256];
4485c51f124SMoriah Waterland char timeb[BUFSIZ];
4495c51f124SMoriah Waterland int retval = RESULT_OK;
4505c51f124SMoriah Waterland struct tm *timep;
4515c51f124SMoriah Waterland time_t clock;
4525c51f124SMoriah Waterland
4535c51f124SMoriah Waterland /* normalize pkginst so its never null */
4545c51f124SMoriah Waterland
4555c51f124SMoriah Waterland if (pkginst == (char *)NULL) {
4565c51f124SMoriah Waterland dbchg = 0;
4575c51f124SMoriah Waterland pkginst = "<unknown>";
4585c51f124SMoriah Waterland }
4595c51f124SMoriah Waterland
4605c51f124SMoriah Waterland /*
4615c51f124SMoriah Waterland * If no changes were made to the database, checkpoint the temporary
4625c51f124SMoriah Waterland * contents file - if this fails, then just close the file which causes
4635c51f124SMoriah Waterland * the contents file to be reopened and reread if it is needed again
4645c51f124SMoriah Waterland */
4655c51f124SMoriah Waterland
4665c51f124SMoriah Waterland if ((dbchg == 0) && (vfpGetModified(*a_cfTmpVfp) == 0)) {
4675c51f124SMoriah Waterland (void) pkgWunlock(); /* Free the database lock. */
4685c51f124SMoriah Waterland return (retval);
4695c51f124SMoriah Waterland }
4705c51f124SMoriah Waterland
4715c51f124SMoriah Waterland /*
4725c51f124SMoriah Waterland * changes made to the current temporary contents file -
4735c51f124SMoriah Waterland * remove any trailing comment lines in the temp contents file, then
4745c51f124SMoriah Waterland * append updated modification info records to temp contents file
4755c51f124SMoriah Waterland */
4765c51f124SMoriah Waterland
4775c51f124SMoriah Waterland pe = vfpGetCurrCharPtr(*a_cfTmpVfp); /* last char in contents file */
4785c51f124SMoriah Waterland ps = vfpGetFirstCharPtr(*a_cfTmpVfp); /* 1st char in contents file */
4795c51f124SMoriah Waterland pl = pe; /* last match is last char in contents file */
4805c51f124SMoriah Waterland
4815c51f124SMoriah Waterland /* skip past all trailing newlines and null bytes */
4825c51f124SMoriah Waterland
4835c51f124SMoriah Waterland while ((pe > ps) && ((*pe == '\n') || (*pe == '\0'))) {
4845c51f124SMoriah Waterland pe--;
4855c51f124SMoriah Waterland }
4865c51f124SMoriah Waterland
4875c51f124SMoriah Waterland /* remove trailing comments as long as there are lines in the file */
4885c51f124SMoriah Waterland
4895c51f124SMoriah Waterland while (pe > ps) {
4905c51f124SMoriah Waterland if (*pe != '\n') {
4915c51f124SMoriah Waterland /* curr char is not newline: backup one byte */
4925c51f124SMoriah Waterland pl = pe--;
4935c51f124SMoriah Waterland } else if (*pl != '#') {
4945c51f124SMoriah Waterland /* curr char is newline next char not comment break */
4955c51f124SMoriah Waterland break;
4965c51f124SMoriah Waterland } else {
4975c51f124SMoriah Waterland /* curr char is newline next char is comment - remove */
4985c51f124SMoriah Waterland *pl = '\0';
4995c51f124SMoriah Waterland vfpSetLastCharPtr(*a_cfTmpVfp, pl);
5005c51f124SMoriah Waterland pe--;
5015c51f124SMoriah Waterland }
5025c51f124SMoriah Waterland }
5035c51f124SMoriah Waterland
5045c51f124SMoriah Waterland /* create two update comment lines */
5055c51f124SMoriah Waterland
5065c51f124SMoriah Waterland (void) time(&clock);
5075c51f124SMoriah Waterland timep = localtime(&clock);
5085c51f124SMoriah Waterland
5095c51f124SMoriah Waterland (void) strftime(timeb, sizeof (timeb), "%c\n", timep);
5105c51f124SMoriah Waterland (void) snprintf(line, sizeof (line),
511214c2196SCasper H.S. Dik gettext("# Last modified by %s for %s package\n# %s"),
512214c2196SCasper H.S. Dik get_prog_name(), pkginst, timeb);
5135c51f124SMoriah Waterland vfpPuts(*a_cfTmpVfp, line);
5145c51f124SMoriah Waterland
5155c51f124SMoriah Waterland /* commit temporary contents file bytes to storage */
5165c51f124SMoriah Waterland
51762224350SCasper H.S. Dik if (pkgservercommitfile(*a_cfTmpVfp, server) != 0) {
51862224350SCasper H.S. Dik logerr(gettext(ERR_COMMIT));
5195c51f124SMoriah Waterland vfpClose(a_cfTmpVfp);
52062224350SCasper H.S. Dik pkgcloseserver(server);
5215c51f124SMoriah Waterland (void) pkgWunlock(); /* Free the database lock. */
5225c51f124SMoriah Waterland return (RESULT_ERR);
5235c51f124SMoriah Waterland }
5245c51f124SMoriah Waterland
5255c51f124SMoriah Waterland return (relslock() == 0 ? RESULT_ERR : retval);
5265c51f124SMoriah Waterland }
5275c51f124SMoriah Waterland
5285c51f124SMoriah Waterland /* This function releases the lock on the package database. */
5295c51f124SMoriah Waterland int
relslock(void)5305c51f124SMoriah Waterland relslock(void)
5315c51f124SMoriah Waterland {
5325c51f124SMoriah Waterland /*
5335c51f124SMoriah Waterland * This closes the contents file and releases the lock.
5345c51f124SMoriah Waterland */
5355c51f124SMoriah Waterland if (!pkgWunlock()) {
5365c51f124SMoriah Waterland int lerrno = errno;
5375c51f124SMoriah Waterland
5385c51f124SMoriah Waterland progerr(gettext(ERR_NOUPD));
5395c51f124SMoriah Waterland logerr(gettext(ERR_FCLOSE_FAIL), lerrno);
5405c51f124SMoriah Waterland return (0);
5415c51f124SMoriah Waterland }
5425c51f124SMoriah Waterland return (1);
5435c51f124SMoriah Waterland }
5445c51f124SMoriah Waterland
5455c51f124SMoriah Waterland /*
5465c51f124SMoriah Waterland * This function attempts to lock the package database. It returns 1 on
5475c51f124SMoriah Waterland * success, 0 on failure. The positive logic verbose flag determines whether
5485c51f124SMoriah Waterland * or not the function displays the error message upon failure.
5495c51f124SMoriah Waterland */
55062224350SCasper H.S. Dik int
pkgWlock(int verbose)5512e10def1SPeter Tribble pkgWlock(int verbose)
5522e10def1SPeter Tribble {
5535c51f124SMoriah Waterland int retry_cnt, retval;
5545c51f124SMoriah Waterland char lockpath[PATH_MAX];
5555c51f124SMoriah Waterland
5565c51f124SMoriah Waterland active_lock = 0;
5575c51f124SMoriah Waterland
5585c51f124SMoriah Waterland (void) snprintf(lockpath, sizeof (lockpath),
5592e10def1SPeter Tribble "%s/%s", pkgadm_dir, LOCKFILE);
5605c51f124SMoriah Waterland
5615c51f124SMoriah Waterland retry_cnt = LOCKRETRY;
5625c51f124SMoriah Waterland
5635c51f124SMoriah Waterland /*
5645c51f124SMoriah Waterland * If the lock file is not present, create it. The mode is set to
5655c51f124SMoriah Waterland * allow any process to lock the database, that's because pkgchk may
5665c51f124SMoriah Waterland * be run by a non-root user.
5675c51f124SMoriah Waterland */
5685c51f124SMoriah Waterland if (access(lockpath, F_OK) == -1) {
5695c51f124SMoriah Waterland lock_fd = open(lockpath, O_RDWR|O_CREAT|O_TRUNC|O_EXCL, 0644);
5705c51f124SMoriah Waterland if (lock_fd < 0) {
5715c51f124SMoriah Waterland if (verbose)
5725c51f124SMoriah Waterland progerr(gettext(ERR_MKLOCK), lockpath);
5735c51f124SMoriah Waterland return (0);
5745c51f124SMoriah Waterland } else {
5755c51f124SMoriah Waterland (void) fchmod(lock_fd, 0644); /* force perms. */
5765c51f124SMoriah Waterland }
5775c51f124SMoriah Waterland } else {
5785c51f124SMoriah Waterland if ((lock_fd = open(lockpath, O_RDWR)) == -1) {
5795c51f124SMoriah Waterland if (verbose)
5805c51f124SMoriah Waterland progerr(gettext(ERR_OPLOCK), lockpath);
5815c51f124SMoriah Waterland return (0);
5825c51f124SMoriah Waterland }
5835c51f124SMoriah Waterland }
5845c51f124SMoriah Waterland
5855c51f124SMoriah Waterland (void) signal(SIGALRM, do_alarm);
5865c51f124SMoriah Waterland (void) alarm(LOCKWAIT);
5875c51f124SMoriah Waterland
588*014740deSToomas Soome retval = 0;
5895c51f124SMoriah Waterland do {
5905c51f124SMoriah Waterland if (lockf(lock_fd, F_LOCK, 0)) {
5915c51f124SMoriah Waterland if (errno == EAGAIN || errno == EINTR)
5925c51f124SMoriah Waterland logerr(gettext(MSG_XWTING));
5935c51f124SMoriah Waterland else if (errno == ECOMM) {
5945c51f124SMoriah Waterland logerr(gettext(ERR_LCKREM));
5955c51f124SMoriah Waterland break;
5965c51f124SMoriah Waterland } else if (errno == EBADF) {
5975c51f124SMoriah Waterland logerr(gettext(ERR_BADLCK));
5985c51f124SMoriah Waterland break;
5995c51f124SMoriah Waterland } else if (errno == EDEADLK) {
6005c51f124SMoriah Waterland logerr(gettext(ERR_DEADLCK));
6015c51f124SMoriah Waterland break;
6025c51f124SMoriah Waterland }
6035c51f124SMoriah Waterland } else {
6045c51f124SMoriah Waterland active_lock = 1;
6055c51f124SMoriah Waterland retval = 1;
6065c51f124SMoriah Waterland break;
6075c51f124SMoriah Waterland }
6085c51f124SMoriah Waterland } while (retry_cnt--);
6095c51f124SMoriah Waterland
6105c51f124SMoriah Waterland (void) signal(SIGALRM, SIG_IGN);
6115c51f124SMoriah Waterland
6122e10def1SPeter Tribble if (retval == 0) {
6135c51f124SMoriah Waterland if (retry_cnt == -1) {
6145c51f124SMoriah Waterland logerr(gettext(ERR_TMOUT));
6155c51f124SMoriah Waterland }
6165c51f124SMoriah Waterland
6175c51f124SMoriah Waterland (void) pkgWunlock(); /* close the lockfile. */
6185c51f124SMoriah Waterland }
6195c51f124SMoriah Waterland
6205c51f124SMoriah Waterland return (retval);
6215c51f124SMoriah Waterland }
6225c51f124SMoriah Waterland
6235c51f124SMoriah Waterland /*
6245c51f124SMoriah Waterland * Release the lock on the package database. Returns 1 on success, 0 on
6255c51f124SMoriah Waterland * failure.
6265c51f124SMoriah Waterland */
6275c51f124SMoriah Waterland static int
pkgWunlock(void)6282e10def1SPeter Tribble pkgWunlock(void)
6292e10def1SPeter Tribble {
6305c51f124SMoriah Waterland if (active_lock) {
6315c51f124SMoriah Waterland active_lock = 0;
6325c51f124SMoriah Waterland if (close(lock_fd))
6335c51f124SMoriah Waterland return (0);
6345c51f124SMoriah Waterland else
6355c51f124SMoriah Waterland return (1);
6365c51f124SMoriah Waterland } else
6375c51f124SMoriah Waterland return (1);
6385c51f124SMoriah Waterland }
6395c51f124SMoriah Waterland
6405c51f124SMoriah Waterland /*
6415c51f124SMoriah Waterland * This function verifies that the contents file is in place.
6425c51f124SMoriah Waterland * returns 1 - if it exists
6435c51f124SMoriah Waterland * returns 0 - if it does not exist
6445c51f124SMoriah Waterland */
6455c51f124SMoriah Waterland int
iscfile(void)6465c51f124SMoriah Waterland iscfile(void)
6475c51f124SMoriah Waterland {
6485c51f124SMoriah Waterland char contents[PATH_MAX];
6495c51f124SMoriah Waterland
6505c51f124SMoriah Waterland (void) snprintf(contents, PATH_MAX, "%s/contents", get_PKGADM());
6515c51f124SMoriah Waterland
6525c51f124SMoriah Waterland return (access(contents, F_OK) == 0 ? 1 : 0);
6535c51f124SMoriah Waterland }
6545c51f124SMoriah Waterland
6555c51f124SMoriah Waterland /*
6565c51f124SMoriah Waterland * This function verifies that the contents file is in place. If it is - no
6575c51f124SMoriah Waterland * change. If it isn't - this creates it.
6585c51f124SMoriah Waterland * Returns: == 0 : failure
6595c51f124SMoriah Waterland * != 0 : success
6605c51f124SMoriah Waterland */
6615c51f124SMoriah Waterland
6625c51f124SMoriah Waterland int
vcfile(void)6635c51f124SMoriah Waterland vcfile(void)
6645c51f124SMoriah Waterland {
6655c51f124SMoriah Waterland int lerrno;
6665c51f124SMoriah Waterland int fd;
6675c51f124SMoriah Waterland char contents[PATH_MAX];
6685c51f124SMoriah Waterland
6695c51f124SMoriah Waterland /*
6705c51f124SMoriah Waterland * create full path to contents file
6715c51f124SMoriah Waterland */
6725c51f124SMoriah Waterland
6735c51f124SMoriah Waterland (void) snprintf(contents, sizeof (contents),
674214c2196SCasper H.S. Dik "%s/contents", get_PKGADM());
6755c51f124SMoriah Waterland
6765c51f124SMoriah Waterland /*
6775c51f124SMoriah Waterland * Attempt to create the file - will only be successful
6785c51f124SMoriah Waterland * if the file does not currently exist.
6795c51f124SMoriah Waterland */
6805c51f124SMoriah Waterland
6815c51f124SMoriah Waterland fd = open(contents, O_WRONLY | O_CREAT | O_EXCL, 0644);
6825c51f124SMoriah Waterland if (fd >= 0) {
6835c51f124SMoriah Waterland /*
6845c51f124SMoriah Waterland * Contents file wasn't there, but is now.
6855c51f124SMoriah Waterland */
6865c51f124SMoriah Waterland
6875c51f124SMoriah Waterland echo(gettext("## Software contents file initialized"));
6885c51f124SMoriah Waterland (void) close(fd);
6895c51f124SMoriah Waterland return (1); /* success */
6905c51f124SMoriah Waterland }
6915c51f124SMoriah Waterland
6925c51f124SMoriah Waterland /*
6935c51f124SMoriah Waterland * Could not create the file - it may exist or there may be
6945c51f124SMoriah Waterland * permissions issues - find out and act accordingly.
6955c51f124SMoriah Waterland */
6965c51f124SMoriah Waterland
6975c51f124SMoriah Waterland lerrno = errno;
6985c51f124SMoriah Waterland
6995c51f124SMoriah Waterland /* success if error is 'file exists' */
7005c51f124SMoriah Waterland
7015c51f124SMoriah Waterland if (lerrno == EEXIST) {
7025c51f124SMoriah Waterland return (1); /* success */
7035c51f124SMoriah Waterland }
7045c51f124SMoriah Waterland
7055c51f124SMoriah Waterland /* success if error is 'permission denied' but file exists */
7065c51f124SMoriah Waterland
7075c51f124SMoriah Waterland if (lerrno == EACCES) {
7085c51f124SMoriah Waterland /*
7095c51f124SMoriah Waterland * Because O_CREAT and O_EXCL are specified in open(),
7105c51f124SMoriah Waterland * if the contents file already exists, the open will
7115c51f124SMoriah Waterland * fail with EACCES - determine if this is the case -
7125c51f124SMoriah Waterland * if so return success.
7135c51f124SMoriah Waterland */
7145c51f124SMoriah Waterland
7155c51f124SMoriah Waterland if (access(contents, F_OK) == 0) {
7165c51f124SMoriah Waterland return (1); /* success */
7175c51f124SMoriah Waterland }
7185c51f124SMoriah Waterland
7195c51f124SMoriah Waterland /*
7205c51f124SMoriah Waterland * access() failed - if because of permissions failure this
7215c51f124SMoriah Waterland * means the contents file exists but it cannot be accessed
7225c51f124SMoriah Waterland * or the path to the contents file cannot be accessed - in
7235c51f124SMoriah Waterland * either case the contents file cannot be accessed.
7245c51f124SMoriah Waterland */
7255c51f124SMoriah Waterland
7265c51f124SMoriah Waterland if (errno == EACCES) {
7275c51f124SMoriah Waterland progerr(gettext(ERR_ACCESS_CONT), contents,
728214c2196SCasper H.S. Dik strerror(lerrno));
7295c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
7305c51f124SMoriah Waterland return (0); /* failure */
7315c51f124SMoriah Waterland }
7325c51f124SMoriah Waterland }
7335c51f124SMoriah Waterland
7345c51f124SMoriah Waterland /*
7355c51f124SMoriah Waterland * the contents file does not exist and it cannot be created.
7365c51f124SMoriah Waterland */
7375c51f124SMoriah Waterland
7385c51f124SMoriah Waterland progerr(gettext(ERR_CREAT_CONT), contents, strerror(lerrno));
7395c51f124SMoriah Waterland logerr(gettext(ERR_ERRNO), lerrno, strerror(lerrno));
7405c51f124SMoriah Waterland return (0); /* failure */
7415c51f124SMoriah Waterland }
742