xref: /titanic_52/usr/src/cmd/svr4pkg/libinst/open_package_datastream.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland 
28*5c51f124SMoriah Waterland #include <stdio.h>
29*5c51f124SMoriah Waterland #include <time.h>
30*5c51f124SMoriah Waterland #include <wait.h>
31*5c51f124SMoriah Waterland #include <stdlib.h>
32*5c51f124SMoriah Waterland #include <unistd.h>
33*5c51f124SMoriah Waterland #include <ulimit.h>
34*5c51f124SMoriah Waterland #include <sys/stat.h>
35*5c51f124SMoriah Waterland #include <sys/statvfs.h>
36*5c51f124SMoriah Waterland #include <assert.h>
37*5c51f124SMoriah Waterland #include <fcntl.h>
38*5c51f124SMoriah Waterland #include <errno.h>
39*5c51f124SMoriah Waterland #include <ctype.h>
40*5c51f124SMoriah Waterland #include <dirent.h>
41*5c51f124SMoriah Waterland #include <string.h>
42*5c51f124SMoriah Waterland #include <signal.h>
43*5c51f124SMoriah Waterland #include <locale.h>
44*5c51f124SMoriah Waterland #include <libintl.h>
45*5c51f124SMoriah Waterland #include <pkgstrct.h>
46*5c51f124SMoriah Waterland #include <pkginfo.h>
47*5c51f124SMoriah Waterland #include <pkgdev.h>
48*5c51f124SMoriah Waterland #include <pkglocs.h>
49*5c51f124SMoriah Waterland #include <pwd.h>
50*5c51f124SMoriah Waterland #include <pkglib.h>
51*5c51f124SMoriah Waterland #include <libinst.h>
52*5c51f124SMoriah Waterland #include <libadm.h>
53*5c51f124SMoriah Waterland #include <messages.h>
54*5c51f124SMoriah Waterland 
55*5c51f124SMoriah Waterland /*
56*5c51f124SMoriah Waterland  * *****************************************************************************
57*5c51f124SMoriah Waterland  * global external (public) functions
58*5c51f124SMoriah Waterland  * *****************************************************************************
59*5c51f124SMoriah Waterland  */
60*5c51f124SMoriah Waterland 
61*5c51f124SMoriah Waterland /*
62*5c51f124SMoriah Waterland  * open package datastream
63*5c51f124SMoriah Waterland  * Arguments:	a_argc - (int) - [RO, *RO]
64*5c51f124SMoriah Waterland  *			- number of arguments available in a_argv
65*5c51f124SMoriah Waterland  *		a_argv - (char **) - [RO, *RO]
66*5c51f124SMoriah Waterland  *			- arguments representing package names to add
67*5c51f124SMoriah Waterland  *		a_spoolDir - (char *) - [RO, *RO]
68*5c51f124SMoriah Waterland  *			- directory to write the package (spool) into
69*5c51f124SMoriah Waterland  *			- if == (char *)NULL then install the packages
70*5c51f124SMoriah Waterland  *			- if != (char *)NULL then write packages into directory
71*5c51f124SMoriah Waterland  *		a_device - (char *) - [RO, *RO]
72*5c51f124SMoriah Waterland  *			- device to read packages from when spooling
73*5c51f124SMoriah Waterland  *			- ignored if a_spoolDir == (char *)NULL
74*5c51f124SMoriah Waterland  *		r_repeat - (int *) - [RO, *RW]
75*5c51f124SMoriah Waterland  *			- set == 0 if no further package names in argc/argv
76*5c51f124SMoriah Waterland  *			- set != 0 IF there are package names in argc/argv
77*5c51f124SMoriah Waterland  *			- if == (int *)NULL - not set
78*5c51f124SMoriah Waterland  *		r_idsName - (char **) - [RW, *RW]
79*5c51f124SMoriah Waterland  *			- set to the name of package input data stream device
80*5c51f124SMoriah Waterland  *			- if == (char *)NULL - no input data stream; that is,
81*5c51f124SMoriah Waterland  *			-- the packages are in a directory and not in a stream
82*5c51f124SMoriah Waterland  *			- if != (char *)NULL - this is the device/file that
83*5c51f124SMoriah Waterland  *			-- is the datastream that contains the packages to add
84*5c51f124SMoriah Waterland  *		a_pkgdev - (struct pkgdev *) - [RO, *RW]
85*5c51f124SMoriah Waterland  *			- pkgdev structure containing package device to open
86*5c51f124SMoriah Waterland  * Returns:	B_TRUE - datastream opened successfully
87*5c51f124SMoriah Waterland  *		B_FALSE - datastream failed to open
88*5c51f124SMoriah Waterland  */
89*5c51f124SMoriah Waterland 
90*5c51f124SMoriah Waterland boolean_t
91*5c51f124SMoriah Waterland open_package_datastream(int a_argc, char **a_argv, char *a_spoolDir,
92*5c51f124SMoriah Waterland 	char *a_device, int *r_repeat, char **r_idsName, char *a_tmpdir,
93*5c51f124SMoriah Waterland 	struct pkgdev *a_pkgdev, int a_optind)
94*5c51f124SMoriah Waterland {
95*5c51f124SMoriah Waterland 	int	n;
96*5c51f124SMoriah Waterland 
97*5c51f124SMoriah Waterland 	/* entry assertions */
98*5c51f124SMoriah Waterland 
99*5c51f124SMoriah Waterland 	assert(a_argv != (char **)NULL);
100*5c51f124SMoriah Waterland 	assert(r_idsName != (char **)NULL);
101*5c51f124SMoriah Waterland 	assert(a_tmpdir != (char *)NULL);
102*5c51f124SMoriah Waterland 	assert(a_pkgdev != (struct pkgdev *)NULL);
103*5c51f124SMoriah Waterland 
104*5c51f124SMoriah Waterland 	/* entry debug information */
105*5c51f124SMoriah Waterland 
106*5c51f124SMoriah Waterland 	echoDebug(DBG_ODS_ENTRY);
107*5c51f124SMoriah Waterland 	echoDebug(DBG_ODS_ARGS,
108*5c51f124SMoriah Waterland 			a_pkgdev->bdevice ? a_pkgdev->bdevice : "?",
109*5c51f124SMoriah Waterland 			a_pkgdev->cdevice ? a_pkgdev->cdevice : "?",
110*5c51f124SMoriah Waterland 			a_pkgdev->pathname ? a_pkgdev->pathname : "?",
111*5c51f124SMoriah Waterland 			a_argc, a_device ? a_device : "?");
112*5c51f124SMoriah Waterland 
113*5c51f124SMoriah Waterland 	/* reset possible return values to defaults */
114*5c51f124SMoriah Waterland 
115*5c51f124SMoriah Waterland 	*r_idsName = (char *)NULL;
116*5c51f124SMoriah Waterland 	if (r_repeat != (int *)NULL) {
117*5c51f124SMoriah Waterland 		*r_repeat = 0;
118*5c51f124SMoriah Waterland 	}
119*5c51f124SMoriah Waterland 
120*5c51f124SMoriah Waterland 	/*
121*5c51f124SMoriah Waterland 	 * Determine how to access the package source "device":
122*5c51f124SMoriah Waterland 	 * - if a block device is associated with the source:
123*5c51f124SMoriah Waterland 	 * -- make sure the next "volume" is mounted and ready.
124*5c51f124SMoriah Waterland 	 * -- input data stream is associated character device
125*5c51f124SMoriah Waterland 	 * - if char device but no block device associated with device:
126*5c51f124SMoriah Waterland 	 * -- input data stream is associated character device
127*5c51f124SMoriah Waterland 	 * - else if a path is associated with device:
128*5c51f124SMoriah Waterland 	 * -- input data stream is associated path
129*5c51f124SMoriah Waterland 	 */
130*5c51f124SMoriah Waterland 
131*5c51f124SMoriah Waterland 	if (a_pkgdev->bdevice != (char *)NULL) {
132*5c51f124SMoriah Waterland 		/* package source is block device */
133*5c51f124SMoriah Waterland 
134*5c51f124SMoriah Waterland 		/*
135*5c51f124SMoriah Waterland 		 * _getvol verifies that the specified device is accessible and
136*5c51f124SMoriah Waterland 		 * that a volume of the appropriate medium has been inserted.
137*5c51f124SMoriah Waterland 		 * _getvol is in libadm.h - delivered by ON as part of SUNWcsl
138*5c51f124SMoriah Waterland 		 * is somewhat analagous to getvol(1M) - args are:
139*5c51f124SMoriah Waterland 		 *  - char *device
140*5c51f124SMoriah Waterland 		 *  - char *label
141*5c51f124SMoriah Waterland 		 *  - int options
142*5c51f124SMoriah Waterland 		 *  - char *prompt
143*5c51f124SMoriah Waterland 		 *  - char *norewind - no rewind device (NULL to use device)
144*5c51f124SMoriah Waterland 		 * Returns:
145*5c51f124SMoriah Waterland 		 *	0 - okay, label matches
146*5c51f124SMoriah Waterland 		 *	1 - device not accessable
147*5c51f124SMoriah Waterland 		 *	2 - unknown device (devattr failed)
148*5c51f124SMoriah Waterland 		 *	3 - user selected quit
149*5c51f124SMoriah Waterland 		 *	4 - label does not match
150*5c51f124SMoriah Waterland 		 */
151*5c51f124SMoriah Waterland 
152*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_BDEV, a_pkgdev->bdevice);
153*5c51f124SMoriah Waterland 
154*5c51f124SMoriah Waterland 		n = _getvol(a_pkgdev->bdevice, NULL, 0L,
155*5c51f124SMoriah Waterland 				MSG_INSERT_VOL, a_pkgdev->norewind);
156*5c51f124SMoriah Waterland 
157*5c51f124SMoriah Waterland 		switch (n) {
158*5c51f124SMoriah Waterland 		case 0:	/* volume open, label matches */
159*5c51f124SMoriah Waterland 			if (ds_readbuf(a_pkgdev->cdevice)) {
160*5c51f124SMoriah Waterland 				(*r_idsName) = a_pkgdev->cdevice;
161*5c51f124SMoriah Waterland 			}
162*5c51f124SMoriah Waterland 			break;
163*5c51f124SMoriah Waterland 		case 3:	/* user selected quit */
164*5c51f124SMoriah Waterland 			quit(3);
165*5c51f124SMoriah Waterland 			/* NOTREACHED */
166*5c51f124SMoriah Waterland 		case 2:	/* unknown device (devattr failed) */
167*5c51f124SMoriah Waterland 			progerr(ERR_UNKNOWN_DEV, a_pkgdev->name);
168*5c51f124SMoriah Waterland 			quit(99);
169*5c51f124SMoriah Waterland 			/* NOTREACHED */
170*5c51f124SMoriah Waterland 		default:	/* device not accessable */
171*5c51f124SMoriah Waterland 			progerr(ERR_PKGVOL);
172*5c51f124SMoriah Waterland 			logerr(LOG_GETVOL_RET, n);
173*5c51f124SMoriah Waterland 			quit(99);
174*5c51f124SMoriah Waterland 			/* NOTREACHED */
175*5c51f124SMoriah Waterland 		}
176*5c51f124SMoriah Waterland 	} else if (a_pkgdev->cdevice != (char *)NULL) {
177*5c51f124SMoriah Waterland 		/* package source is character device */
178*5c51f124SMoriah Waterland 
179*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_CDEV, a_pkgdev->cdevice);
180*5c51f124SMoriah Waterland 
181*5c51f124SMoriah Waterland 		(*r_idsName) = a_pkgdev->cdevice;
182*5c51f124SMoriah Waterland 	} else if (a_pkgdev->pathname != (char *)NULL) {
183*5c51f124SMoriah Waterland 		/* package source is path name to file */
184*5c51f124SMoriah Waterland 
185*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_ISFILE, a_pkgdev->pathname);
186*5c51f124SMoriah Waterland 
187*5c51f124SMoriah Waterland 		(*r_idsName) = a_pkgdev->pathname;
188*5c51f124SMoriah Waterland 	} else {
189*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_UNK);
190*5c51f124SMoriah Waterland 	}
191*5c51f124SMoriah Waterland 
192*5c51f124SMoriah Waterland 	/*
193*5c51f124SMoriah Waterland 	 * If writing the packages into a spool directory instead of
194*5c51f124SMoriah Waterland 	 * installing the packages, invoke pkgtrans to perform the
195*5c51f124SMoriah Waterland 	 * conversion and exit.
196*5c51f124SMoriah Waterland 	 */
197*5c51f124SMoriah Waterland 
198*5c51f124SMoriah Waterland 	if (a_spoolDir) {
199*5c51f124SMoriah Waterland 		return (B_TRUE);
200*5c51f124SMoriah Waterland 	}
201*5c51f124SMoriah Waterland 
202*5c51f124SMoriah Waterland 	/* create temp dir for op if input data stream specified */
203*5c51f124SMoriah Waterland 
204*5c51f124SMoriah Waterland 	if (*r_idsName) {
205*5c51f124SMoriah Waterland 		/*
206*5c51f124SMoriah Waterland 		 * initialize datastream,
207*5c51f124SMoriah Waterland 		 * dirname is set to directory where package is unstreamed
208*5c51f124SMoriah Waterland 		 */
209*5c51f124SMoriah Waterland 		if (setup_temporary_directory(&a_pkgdev->dirname, a_tmpdir,
210*5c51f124SMoriah Waterland 			"dstream") == B_FALSE) {
211*5c51f124SMoriah Waterland 			progerr(ERR_STREAMDIR, strerror(errno));
212*5c51f124SMoriah Waterland 			quit(99);
213*5c51f124SMoriah Waterland 			/* NOTREACHED */
214*5c51f124SMoriah Waterland 		}
215*5c51f124SMoriah Waterland 	}
216*5c51f124SMoriah Waterland 
217*5c51f124SMoriah Waterland 	if (r_repeat != (int *)NULL) {
218*5c51f124SMoriah Waterland 		*r_repeat = (a_optind >= a_argc);
219*5c51f124SMoriah Waterland 	}
220*5c51f124SMoriah Waterland 
221*5c51f124SMoriah Waterland 	/*
222*5c51f124SMoriah Waterland 	 * mount source device (e.g. floppy) if no input data stream
223*5c51f124SMoriah Waterland 	 * specified, and the package source device is mountable. If
224*5c51f124SMoriah Waterland 	 * the pkgmount fails, go back and try to mount the package
225*5c51f124SMoriah Waterland 	 * source again. When a package is split up into multiple
226*5c51f124SMoriah Waterland 	 * volumes (such as floppies), it might be possible to go back
227*5c51f124SMoriah Waterland 	 * and insert a different copy of the required volume/floppy
228*5c51f124SMoriah Waterland 	 * if the current one cannot be mounted. Otherwise this could
229*5c51f124SMoriah Waterland 	 * have just called quit() if the mount failed...
230*5c51f124SMoriah Waterland 	 */
231*5c51f124SMoriah Waterland 
232*5c51f124SMoriah Waterland 	if (((*r_idsName) == (char *)NULL) && a_pkgdev->mount) {
233*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_MOUNTING, *r_idsName,
234*5c51f124SMoriah Waterland 							a_pkgdev->mount);
235*5c51f124SMoriah Waterland 		a_pkgdev->rdonly++;
236*5c51f124SMoriah Waterland 		n = pkgmount(a_pkgdev, NULL, 0, 0, 0);
237*5c51f124SMoriah Waterland 		if (n != 0) {
238*5c51f124SMoriah Waterland 			/* pkgmount failed */
239*5c51f124SMoriah Waterland 			return (B_FALSE);
240*5c51f124SMoriah Waterland 		}
241*5c51f124SMoriah Waterland 	}
242*5c51f124SMoriah Waterland 
243*5c51f124SMoriah Waterland 	/*
244*5c51f124SMoriah Waterland 	 * open and initialize input data stream if specified
245*5c51f124SMoriah Waterland 	 */
246*5c51f124SMoriah Waterland 
247*5c51f124SMoriah Waterland 	if ((*r_idsName) != (char *)NULL) {
248*5c51f124SMoriah Waterland 		echoDebug(DBG_ODS_DATASTREAM_INIT, *r_idsName);
249*5c51f124SMoriah Waterland 
250*5c51f124SMoriah Waterland 		/* use character device to force rewind of datastream */
251*5c51f124SMoriah Waterland 		if ((a_pkgdev->cdevice != (char *)NULL) &&
252*5c51f124SMoriah Waterland 			(a_pkgdev->bdevice == (char *)NULL)) {
253*5c51f124SMoriah Waterland 			n = _getvol(a_pkgdev->name, NULL, 0L, NULL,
254*5c51f124SMoriah Waterland 					a_pkgdev->norewind);
255*5c51f124SMoriah Waterland 
256*5c51f124SMoriah Waterland 			switch (n) {
257*5c51f124SMoriah Waterland 			case 0:	/* volume open, label matches */
258*5c51f124SMoriah Waterland 				break;
259*5c51f124SMoriah Waterland 			case 3:	/* user selected quit */
260*5c51f124SMoriah Waterland 				quit(3);
261*5c51f124SMoriah Waterland 				/* NOTREACHED */
262*5c51f124SMoriah Waterland 			case 2:	/* unknown device (devattr failed) */
263*5c51f124SMoriah Waterland 				progerr(ERR_UNKNOWN_DEV, a_pkgdev->name);
264*5c51f124SMoriah Waterland 				quit(99);
265*5c51f124SMoriah Waterland 				/* NOTREACHED */
266*5c51f124SMoriah Waterland 			default:
267*5c51f124SMoriah Waterland 				progerr(ERR_PKGVOL);
268*5c51f124SMoriah Waterland 				logerr(LOG_GETVOL_RET, n);
269*5c51f124SMoriah Waterland 				quit(99);
270*5c51f124SMoriah Waterland 				/* NOTREACHED */
271*5c51f124SMoriah Waterland 			}
272*5c51f124SMoriah Waterland 		}
273*5c51f124SMoriah Waterland 
274*5c51f124SMoriah Waterland 		if (chdir(a_pkgdev->dirname)) {
275*5c51f124SMoriah Waterland 			progerr(ERR_CHDIR, a_pkgdev->dirname);
276*5c51f124SMoriah Waterland 			quit(99);
277*5c51f124SMoriah Waterland 			/* NOTREACHED */
278*5c51f124SMoriah Waterland 		}
279*5c51f124SMoriah Waterland 
280*5c51f124SMoriah Waterland 		/*
281*5c51f124SMoriah Waterland 		 * initialize datastream for subsequent installation;
282*5c51f124SMoriah Waterland 		 * read the source device;
283*5c51f124SMoriah Waterland 		 * aquire the header data and check it for validity;
284*5c51f124SMoriah Waterland 		 * creates subdirectories in package stream directory
285*5c51f124SMoriah Waterland 		 * (a_pkgdev->dirname) for each package and retrieves each
286*5c51f124SMoriah Waterland 		 * packages pkginfo and pkgmap files
287*5c51f124SMoriah Waterland 		 */
288*5c51f124SMoriah Waterland 
289*5c51f124SMoriah Waterland 		if (ds_init(*r_idsName, &a_argv[a_optind],
290*5c51f124SMoriah Waterland 						a_pkgdev->norewind)) {
291*5c51f124SMoriah Waterland 			progerr(ERR_DSINIT, *r_idsName);
292*5c51f124SMoriah Waterland 			quit(99);
293*5c51f124SMoriah Waterland 			/* NOTREACHED */
294*5c51f124SMoriah Waterland 		}
295*5c51f124SMoriah Waterland 	}
296*5c51f124SMoriah Waterland 
297*5c51f124SMoriah Waterland 	return (B_TRUE);
298*5c51f124SMoriah Waterland }
299