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