xref: /illumos-gate/usr/src/cmd/svr4pkg/pkgtrans/main.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2017 Peter Tribble.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
33 
34 /*
35  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
36  */
37 
38 #include <locale.h>
39 #include <libintl.h>
40 #include <stdio.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <pkgtrans.h>
46 #include <pkglib.h>
47 #include <pkglocs.h>
48 #include <libadm.h>
49 #include <libinst.h>
50 #include <messages.h>
51 
52 static int	options;
53 
54 static void	usage(void);
55 static void	trap(int signo);
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	int	c;
61 	void	(*func)();
62 	extern int	optind;
63 	int		ret;
64 
65 	(void) setlocale(LC_ALL, "");
66 
67 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
68 #define	TEXT_DOMAIN "SYS_TEST"
69 #endif
70 	(void) textdomain(TEXT_DOMAIN);
71 
72 	(void) set_prog_name(argv[0]);
73 
74 	while ((c = getopt(argc, argv, "snio?")) != EOF) {
75 		switch (c) {
76 		case 'n':
77 			options |= PT_RENAME;
78 			break;
79 
80 		case 'i':
81 			options |= PT_INFO_ONLY;
82 			break;
83 
84 		case 'o':
85 			options |= PT_OVERWRITE;
86 			break;
87 
88 		case 's':
89 			options |= PT_ODTSTREAM;
90 			break;
91 
92 		default:
93 			usage();
94 			return (1);
95 		}
96 	}
97 	func = signal(SIGINT, trap);
98 	if (func != SIG_DFL)
99 		(void) signal(SIGINT, func);
100 	(void) signal(SIGHUP, trap);
101 	(void) signal(SIGQUIT, trap);
102 	(void) signal(SIGTERM, trap);
103 	(void) signal(SIGPIPE, trap);
104 	(void) signal(SIGPWR, trap);
105 
106 	if ((argc-optind) < 2) {
107 		usage();
108 		return (1);
109 	}
110 
111 	ret = pkgtrans(flex_device(argv[optind], 1),
112 	    flex_device(argv[optind+1], 1), &argv[optind+2], options);
113 
114 	quit(ret);
115 	/*NOTREACHED*/
116 }
117 
118 void
119 quit(int retcode)
120 {
121 	(void) signal(SIGINT, SIG_IGN);
122 	(void) signal(SIGHUP, SIG_IGN);
123 	(void) ds_close(1);
124 	(void) pkghead(NULL);
125 	exit(retcode);
126 }
127 
128 static void
129 trap(int signo)
130 {
131 	(void) signal(SIGINT, SIG_IGN);
132 	(void) signal(SIGHUP, SIG_IGN);
133 
134 	if (signo == SIGINT) {
135 		progerr(gettext("aborted at user request.\n"));
136 		quit(3);
137 	}
138 	progerr(gettext("aborted by signal %d\n"), signo);
139 	quit(1);
140 }
141 
142 static void
143 usage(void)
144 {
145 	(void) fprintf(stderr,
146 	    gettext("usage: %s [-ions] srcdev dstdev [pkg [pkg...]]\n"),
147 	    get_prog_name());
148 }
149