xref: /illumos-gate/usr/src/cmd/svr4pkg/pkgtrans/main.c (revision d1aea6f139360e9e7f1504facb24f8521047b15c)
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 char	*optarg;
63 	extern int	optind;
64 	char		*homedir = NULL;
65 	int		ret, len;
66 
67 	(void) setlocale(LC_ALL, "");
68 
69 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
70 #define	TEXT_DOMAIN "SYS_TEST"
71 #endif
72 	(void) textdomain(TEXT_DOMAIN);
73 
74 	(void) set_prog_name(argv[0]);
75 
76 	while ((c = getopt(argc, argv, "snio?")) != EOF) {
77 		switch (c) {
78 		case 'n':
79 			options |= PT_RENAME;
80 			break;
81 
82 		case 'i':
83 			options |= PT_INFO_ONLY;
84 			break;
85 
86 		case 'o':
87 			options |= PT_OVERWRITE;
88 			break;
89 
90 		case 's':
91 			options |= PT_ODTSTREAM;
92 			break;
93 
94 		default:
95 			usage();
96 			return (1);
97 		}
98 	}
99 	func = signal(SIGINT, trap);
100 	if (func != SIG_DFL)
101 		(void) signal(SIGINT, func);
102 	(void) signal(SIGHUP, trap);
103 	(void) signal(SIGQUIT, trap);
104 	(void) signal(SIGTERM, trap);
105 	(void) signal(SIGPIPE, trap);
106 	(void) signal(SIGPWR, trap);
107 
108 	if ((argc-optind) < 2) {
109 		usage();
110 		return (1);
111 	}
112 
113 	ret = pkgtrans(flex_device(argv[optind], 1),
114 	    flex_device(argv[optind+1], 1), &argv[optind+2], options);
115 
116 	quit(ret);
117 	/*NOTREACHED*/
118 }
119 
120 void
121 quit(int retcode)
122 {
123 	(void) signal(SIGINT, SIG_IGN);
124 	(void) signal(SIGHUP, SIG_IGN);
125 	(void) ds_close(1);
126 	(void) pkghead(NULL);
127 	exit(retcode);
128 }
129 
130 static void
131 trap(int signo)
132 {
133 	(void) signal(SIGINT, SIG_IGN);
134 	(void) signal(SIGHUP, SIG_IGN);
135 
136 	if (signo == SIGINT) {
137 		progerr(gettext("aborted at user request.\n"));
138 		quit(3);
139 	}
140 	progerr(gettext("aborted by signal %d\n"), signo);
141 	quit(1);
142 }
143 
144 static void
145 usage(void)
146 {
147 	(void) fprintf(stderr,
148 	    gettext("usage: %s [-ions] srcdev dstdev [pkg [pkg...]]\n"),
149 	    get_prog_name());
150 }
151