1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM *
4*7836SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM *
8*7836SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM * and limitations under the License.
12*7836SJohn.Forte@Sun.COM *
13*7836SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM *
19*7836SJohn.Forte@Sun.COM * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23*7836SJohn.Forte@Sun.COM * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM */
25*7836SJohn.Forte@Sun.COM
26*7836SJohn.Forte@Sun.COM #include <sys/types.h>
27*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
28*7836SJohn.Forte@Sun.COM #include <sys/mkdev.h>
29*7836SJohn.Forte@Sun.COM #include <sys/param.h>
30*7836SJohn.Forte@Sun.COM #include <fcntl.h>
31*7836SJohn.Forte@Sun.COM #include <stdarg.h>
32*7836SJohn.Forte@Sun.COM #include <stdlib.h>
33*7836SJohn.Forte@Sun.COM #include <strings.h>
34*7836SJohn.Forte@Sun.COM #include <errno.h>
35*7836SJohn.Forte@Sun.COM #include <stdio.h>
36*7836SJohn.Forte@Sun.COM #include <locale.h>
37*7836SJohn.Forte@Sun.COM #include <unistd.h>
38*7836SJohn.Forte@Sun.COM #include <libgen.h>
39*7836SJohn.Forte@Sun.COM #include <nsctl.h>
40*7836SJohn.Forte@Sun.COM
41*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_s.h>
42*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_s_u.h>
43*7836SJohn.Forte@Sun.COM #include <sys/unistat/spcs_errors.h>
44*7836SJohn.Forte@Sun.COM
45*7836SJohn.Forte@Sun.COM #include <sys/nsctl/sv.h>
46*7836SJohn.Forte@Sun.COM #include <sys/nsctl/sv_impl.h>
47*7836SJohn.Forte@Sun.COM
48*7836SJohn.Forte@Sun.COM #include <sys/nsctl/cfg.h>
49*7836SJohn.Forte@Sun.COM
50*7836SJohn.Forte@Sun.COM
51*7836SJohn.Forte@Sun.COM static int sv_max_devices;
52*7836SJohn.Forte@Sun.COM
53*7836SJohn.Forte@Sun.COM
54*7836SJohn.Forte@Sun.COM /*
55*7836SJohn.Forte@Sun.COM * Pathnames.
56*7836SJohn.Forte@Sun.COM */
57*7836SJohn.Forte@Sun.COM
58*7836SJohn.Forte@Sun.COM static const caddr_t sv_rpath = SV_DEVICE;
59*7836SJohn.Forte@Sun.COM
60*7836SJohn.Forte@Sun.COM /*
61*7836SJohn.Forte@Sun.COM * Functions.
62*7836SJohn.Forte@Sun.COM */
63*7836SJohn.Forte@Sun.COM
64*7836SJohn.Forte@Sun.COM static void resume_dev(int, sv_name_t *);
65*7836SJohn.Forte@Sun.COM static void suspend_dev(int, const caddr_t);
66*7836SJohn.Forte@Sun.COM static int read_libcfg(sv_name_t svn[]);
67*7836SJohn.Forte@Sun.COM static void resume_sv();
68*7836SJohn.Forte@Sun.COM static void suspend_sv();
69*7836SJohn.Forte@Sun.COM static void prepare_unload_sv();
70*7836SJohn.Forte@Sun.COM
71*7836SJohn.Forte@Sun.COM
72*7836SJohn.Forte@Sun.COM /*
73*7836SJohn.Forte@Sun.COM * support for the special cluster tag "local" to be used with -C in a
74*7836SJohn.Forte@Sun.COM * cluster for local volumes.
75*7836SJohn.Forte@Sun.COM */
76*7836SJohn.Forte@Sun.COM
77*7836SJohn.Forte@Sun.COM #define SV_LOCAL_TAG "local"
78*7836SJohn.Forte@Sun.COM
79*7836SJohn.Forte@Sun.COM static caddr_t program;
80*7836SJohn.Forte@Sun.COM static caddr_t cfg_cluster_tag;
81*7836SJohn.Forte@Sun.COM
82*7836SJohn.Forte@Sun.COM
83*7836SJohn.Forte@Sun.COM static void
usage(void)84*7836SJohn.Forte@Sun.COM usage(void)
85*7836SJohn.Forte@Sun.COM {
86*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, gettext("usage:\n"));
87*7836SJohn.Forte@Sun.COM
88*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, gettext(
89*7836SJohn.Forte@Sun.COM "\t%s -h help\n"), program);
90*7836SJohn.Forte@Sun.COM
91*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, gettext(
92*7836SJohn.Forte@Sun.COM "\t%s [-C tag] -r resume all sv devices\n"), program);
93*7836SJohn.Forte@Sun.COM
94*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, gettext(
95*7836SJohn.Forte@Sun.COM "\t%s [-C tag] -s suspend all sv devices\n"), program);
96*7836SJohn.Forte@Sun.COM
97*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, gettext(
98*7836SJohn.Forte@Sun.COM "\t%s -u prepare for sv unload\n"), program);
99*7836SJohn.Forte@Sun.COM }
100*7836SJohn.Forte@Sun.COM
101*7836SJohn.Forte@Sun.COM
102*7836SJohn.Forte@Sun.COM static void
message(caddr_t prefix,spcs_s_info_t * status,caddr_t string,va_list ap)103*7836SJohn.Forte@Sun.COM message(caddr_t prefix, spcs_s_info_t *status, caddr_t string, va_list ap)
104*7836SJohn.Forte@Sun.COM {
105*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, "%s: %s: ", program, prefix);
106*7836SJohn.Forte@Sun.COM (void) vfprintf(stderr, string, ap);
107*7836SJohn.Forte@Sun.COM (void) fprintf(stderr, "\n");
108*7836SJohn.Forte@Sun.COM
109*7836SJohn.Forte@Sun.COM if (status) {
110*7836SJohn.Forte@Sun.COM spcs_s_report(*status, stderr);
111*7836SJohn.Forte@Sun.COM spcs_s_ufree(status);
112*7836SJohn.Forte@Sun.COM }
113*7836SJohn.Forte@Sun.COM }
114*7836SJohn.Forte@Sun.COM
115*7836SJohn.Forte@Sun.COM
116*7836SJohn.Forte@Sun.COM static void
error(spcs_s_info_t * status,caddr_t string,...)117*7836SJohn.Forte@Sun.COM error(spcs_s_info_t *status, caddr_t string, ...)
118*7836SJohn.Forte@Sun.COM {
119*7836SJohn.Forte@Sun.COM va_list ap;
120*7836SJohn.Forte@Sun.COM va_start(ap, string);
121*7836SJohn.Forte@Sun.COM
122*7836SJohn.Forte@Sun.COM message(gettext("error"), status, string, ap);
123*7836SJohn.Forte@Sun.COM
124*7836SJohn.Forte@Sun.COM va_end(ap);
125*7836SJohn.Forte@Sun.COM exit(1);
126*7836SJohn.Forte@Sun.COM }
127*7836SJohn.Forte@Sun.COM
128*7836SJohn.Forte@Sun.COM
129*7836SJohn.Forte@Sun.COM static void
warn(spcs_s_info_t * status,caddr_t string,...)130*7836SJohn.Forte@Sun.COM warn(spcs_s_info_t *status, caddr_t string, ...)
131*7836SJohn.Forte@Sun.COM {
132*7836SJohn.Forte@Sun.COM va_list ap;
133*7836SJohn.Forte@Sun.COM va_start(ap, string);
134*7836SJohn.Forte@Sun.COM
135*7836SJohn.Forte@Sun.COM message(gettext("warning"), status, string, ap);
136*7836SJohn.Forte@Sun.COM
137*7836SJohn.Forte@Sun.COM va_end(ap);
138*7836SJohn.Forte@Sun.COM }
139*7836SJohn.Forte@Sun.COM
140*7836SJohn.Forte@Sun.COM
141*7836SJohn.Forte@Sun.COM static void
sv_get_maxdevs(void)142*7836SJohn.Forte@Sun.COM sv_get_maxdevs(void)
143*7836SJohn.Forte@Sun.COM {
144*7836SJohn.Forte@Sun.COM sv_name_t svn[1];
145*7836SJohn.Forte@Sun.COM sv_list_t svl;
146*7836SJohn.Forte@Sun.COM int fd;
147*7836SJohn.Forte@Sun.COM
148*7836SJohn.Forte@Sun.COM if (sv_max_devices > 0)
149*7836SJohn.Forte@Sun.COM return;
150*7836SJohn.Forte@Sun.COM
151*7836SJohn.Forte@Sun.COM fd = open(sv_rpath, O_RDONLY);
152*7836SJohn.Forte@Sun.COM if (fd < 0)
153*7836SJohn.Forte@Sun.COM error(NULL, gettext("unable to open %s: %s"),
154*7836SJohn.Forte@Sun.COM sv_rpath, strerror(errno));
155*7836SJohn.Forte@Sun.COM
156*7836SJohn.Forte@Sun.COM bzero(&svl, sizeof (svl));
157*7836SJohn.Forte@Sun.COM bzero(&svn[0], sizeof (svn));
158*7836SJohn.Forte@Sun.COM
159*7836SJohn.Forte@Sun.COM svl.svl_names = &svn[0];
160*7836SJohn.Forte@Sun.COM svl.svl_error = spcs_s_ucreate();
161*7836SJohn.Forte@Sun.COM
162*7836SJohn.Forte@Sun.COM if (ioctl(fd, SVIOC_LIST, &svl) < 0)
163*7836SJohn.Forte@Sun.COM error(&svl.svl_error, gettext("unable to get max devs"));
164*7836SJohn.Forte@Sun.COM
165*7836SJohn.Forte@Sun.COM spcs_s_ufree(&svl.svl_error);
166*7836SJohn.Forte@Sun.COM sv_max_devices = svl.svl_maxdevs;
167*7836SJohn.Forte@Sun.COM
168*7836SJohn.Forte@Sun.COM (void) close(fd);
169*7836SJohn.Forte@Sun.COM }
170*7836SJohn.Forte@Sun.COM
171*7836SJohn.Forte@Sun.COM
172*7836SJohn.Forte@Sun.COM static sv_name_t *
sv_alloc_svnames(void)173*7836SJohn.Forte@Sun.COM sv_alloc_svnames(void)
174*7836SJohn.Forte@Sun.COM {
175*7836SJohn.Forte@Sun.COM sv_name_t *svn = NULL;
176*7836SJohn.Forte@Sun.COM
177*7836SJohn.Forte@Sun.COM sv_get_maxdevs();
178*7836SJohn.Forte@Sun.COM
179*7836SJohn.Forte@Sun.COM svn = calloc(sv_max_devices, sizeof (*svn));
180*7836SJohn.Forte@Sun.COM if (svn == NULL) {
181*7836SJohn.Forte@Sun.COM error(NULL, "unable to allocate %ld bytes of memory",
182*7836SJohn.Forte@Sun.COM sv_max_devices * sizeof (*svn));
183*7836SJohn.Forte@Sun.COM }
184*7836SJohn.Forte@Sun.COM
185*7836SJohn.Forte@Sun.COM return (svn);
186*7836SJohn.Forte@Sun.COM }
187*7836SJohn.Forte@Sun.COM
188*7836SJohn.Forte@Sun.COM int
main(int argc,char * argv[])189*7836SJohn.Forte@Sun.COM main(int argc, char *argv[])
190*7836SJohn.Forte@Sun.COM {
191*7836SJohn.Forte@Sun.COM extern int optind;
192*7836SJohn.Forte@Sun.COM extern char *optarg;
193*7836SJohn.Forte@Sun.COM int Cflag, resume, suspend, unload;
194*7836SJohn.Forte@Sun.COM int opt;
195*7836SJohn.Forte@Sun.COM
196*7836SJohn.Forte@Sun.COM (void) setlocale(LC_ALL, "");
197*7836SJohn.Forte@Sun.COM (void) textdomain("svboot");
198*7836SJohn.Forte@Sun.COM
199*7836SJohn.Forte@Sun.COM program = strdup(basename(argv[0]));
200*7836SJohn.Forte@Sun.COM
201*7836SJohn.Forte@Sun.COM Cflag = unload = resume = suspend = 0;
202*7836SJohn.Forte@Sun.COM
203*7836SJohn.Forte@Sun.COM while ((opt = getopt(argc, argv, "C:hrsu")) != EOF) {
204*7836SJohn.Forte@Sun.COM switch (opt) {
205*7836SJohn.Forte@Sun.COM
206*7836SJohn.Forte@Sun.COM case 'C':
207*7836SJohn.Forte@Sun.COM if (Cflag) {
208*7836SJohn.Forte@Sun.COM warn(NULL,
209*7836SJohn.Forte@Sun.COM gettext("-C specified multiple times"));
210*7836SJohn.Forte@Sun.COM usage();
211*7836SJohn.Forte@Sun.COM exit(2);
212*7836SJohn.Forte@Sun.COM /* NOTREACHED */
213*7836SJohn.Forte@Sun.COM }
214*7836SJohn.Forte@Sun.COM
215*7836SJohn.Forte@Sun.COM Cflag++;
216*7836SJohn.Forte@Sun.COM cfg_cluster_tag = optarg;
217*7836SJohn.Forte@Sun.COM break;
218*7836SJohn.Forte@Sun.COM
219*7836SJohn.Forte@Sun.COM case 'r':
220*7836SJohn.Forte@Sun.COM resume++;
221*7836SJohn.Forte@Sun.COM break;
222*7836SJohn.Forte@Sun.COM
223*7836SJohn.Forte@Sun.COM case 's':
224*7836SJohn.Forte@Sun.COM suspend++;
225*7836SJohn.Forte@Sun.COM break;
226*7836SJohn.Forte@Sun.COM
227*7836SJohn.Forte@Sun.COM case 'u':
228*7836SJohn.Forte@Sun.COM unload++;
229*7836SJohn.Forte@Sun.COM break;
230*7836SJohn.Forte@Sun.COM
231*7836SJohn.Forte@Sun.COM case 'h':
232*7836SJohn.Forte@Sun.COM usage();
233*7836SJohn.Forte@Sun.COM exit(0);
234*7836SJohn.Forte@Sun.COM
235*7836SJohn.Forte@Sun.COM case '?': /* FALLTHRU */
236*7836SJohn.Forte@Sun.COM
237*7836SJohn.Forte@Sun.COM default:
238*7836SJohn.Forte@Sun.COM usage();
239*7836SJohn.Forte@Sun.COM exit(2);
240*7836SJohn.Forte@Sun.COM /* NOTREACHED */
241*7836SJohn.Forte@Sun.COM }
242*7836SJohn.Forte@Sun.COM }
243*7836SJohn.Forte@Sun.COM
244*7836SJohn.Forte@Sun.COM
245*7836SJohn.Forte@Sun.COM /*
246*7836SJohn.Forte@Sun.COM * Usage checks
247*7836SJohn.Forte@Sun.COM */
248*7836SJohn.Forte@Sun.COM
249*7836SJohn.Forte@Sun.COM if ((resume + suspend + unload) > 1) {
250*7836SJohn.Forte@Sun.COM warn(NULL, gettext("-r , -s and -u are mutually exclusive"));
251*7836SJohn.Forte@Sun.COM usage();
252*7836SJohn.Forte@Sun.COM exit(2);
253*7836SJohn.Forte@Sun.COM }
254*7836SJohn.Forte@Sun.COM
255*7836SJohn.Forte@Sun.COM if (!resume && !suspend && !unload) {
256*7836SJohn.Forte@Sun.COM warn(NULL, gettext("option required"));
257*7836SJohn.Forte@Sun.COM usage();
258*7836SJohn.Forte@Sun.COM exit(2);
259*7836SJohn.Forte@Sun.COM }
260*7836SJohn.Forte@Sun.COM
261*7836SJohn.Forte@Sun.COM if (optind != argc) {
262*7836SJohn.Forte@Sun.COM usage();
263*7836SJohn.Forte@Sun.COM exit(2);
264*7836SJohn.Forte@Sun.COM }
265*7836SJohn.Forte@Sun.COM
266*7836SJohn.Forte@Sun.COM
267*7836SJohn.Forte@Sun.COM /*
268*7836SJohn.Forte@Sun.COM * Check for the special (local) cluster tag
269*7836SJohn.Forte@Sun.COM */
270*7836SJohn.Forte@Sun.COM
271*7836SJohn.Forte@Sun.COM if (cfg_cluster_tag != NULL &&
272*7836SJohn.Forte@Sun.COM strcmp(cfg_cluster_tag, SV_LOCAL_TAG) == 0)
273*7836SJohn.Forte@Sun.COM cfg_cluster_tag = "-";
274*7836SJohn.Forte@Sun.COM
275*7836SJohn.Forte@Sun.COM /*
276*7836SJohn.Forte@Sun.COM * Process commands
277*7836SJohn.Forte@Sun.COM */
278*7836SJohn.Forte@Sun.COM
279*7836SJohn.Forte@Sun.COM if (resume)
280*7836SJohn.Forte@Sun.COM resume_sv();
281*7836SJohn.Forte@Sun.COM else if (suspend)
282*7836SJohn.Forte@Sun.COM suspend_sv();
283*7836SJohn.Forte@Sun.COM else if (unload)
284*7836SJohn.Forte@Sun.COM prepare_unload_sv();
285*7836SJohn.Forte@Sun.COM
286*7836SJohn.Forte@Sun.COM return (0);
287*7836SJohn.Forte@Sun.COM }
288*7836SJohn.Forte@Sun.COM
289*7836SJohn.Forte@Sun.COM
290*7836SJohn.Forte@Sun.COM static void
resume_sv()291*7836SJohn.Forte@Sun.COM resume_sv()
292*7836SJohn.Forte@Sun.COM {
293*7836SJohn.Forte@Sun.COM int index;
294*7836SJohn.Forte@Sun.COM sv_name_t *svn;
295*7836SJohn.Forte@Sun.COM int cnt;
296*7836SJohn.Forte@Sun.COM int fd;
297*7836SJohn.Forte@Sun.COM
298*7836SJohn.Forte@Sun.COM svn = sv_alloc_svnames();
299*7836SJohn.Forte@Sun.COM
300*7836SJohn.Forte@Sun.COM index = read_libcfg(svn);
301*7836SJohn.Forte@Sun.COM
302*7836SJohn.Forte@Sun.COM fd = open(sv_rpath, O_RDONLY);
303*7836SJohn.Forte@Sun.COM if (fd < 0) {
304*7836SJohn.Forte@Sun.COM warn(NULL, gettext("unable to open %s: %s"),
305*7836SJohn.Forte@Sun.COM svn->svn_path, strerror(errno));
306*7836SJohn.Forte@Sun.COM return;
307*7836SJohn.Forte@Sun.COM }
308*7836SJohn.Forte@Sun.COM
309*7836SJohn.Forte@Sun.COM for (cnt = 0; cnt < index; cnt++) {
310*7836SJohn.Forte@Sun.COM
311*7836SJohn.Forte@Sun.COM /*
312*7836SJohn.Forte@Sun.COM * Check for more data.
313*7836SJohn.Forte@Sun.COM */
314*7836SJohn.Forte@Sun.COM if (svn[cnt].svn_path[0] == '\0') {
315*7836SJohn.Forte@Sun.COM /*
316*7836SJohn.Forte@Sun.COM * This was set when reading sv.conf. After the last
317*7836SJohn.Forte@Sun.COM * line svn_path was set to \0, so we are finished.
318*7836SJohn.Forte@Sun.COM * We shouldn't get here, but put this in just in
319*7836SJohn.Forte@Sun.COM * case.
320*7836SJohn.Forte@Sun.COM */
321*7836SJohn.Forte@Sun.COM break;
322*7836SJohn.Forte@Sun.COM }
323*7836SJohn.Forte@Sun.COM resume_dev(fd, &svn[cnt]);
324*7836SJohn.Forte@Sun.COM }
325*7836SJohn.Forte@Sun.COM (void) close(fd);
326*7836SJohn.Forte@Sun.COM }
327*7836SJohn.Forte@Sun.COM
328*7836SJohn.Forte@Sun.COM
329*7836SJohn.Forte@Sun.COM static void
resume_dev(int fd,sv_name_t * svn)330*7836SJohn.Forte@Sun.COM resume_dev(int fd, sv_name_t *svn)
331*7836SJohn.Forte@Sun.COM {
332*7836SJohn.Forte@Sun.COM struct stat stb;
333*7836SJohn.Forte@Sun.COM sv_conf_t svc;
334*7836SJohn.Forte@Sun.COM
335*7836SJohn.Forte@Sun.COM bzero(&svc, sizeof (svc));
336*7836SJohn.Forte@Sun.COM
337*7836SJohn.Forte@Sun.COM if (stat(svn->svn_path, &stb) != 0) {
338*7836SJohn.Forte@Sun.COM warn(NULL, gettext("unable to access %s: %s"),
339*7836SJohn.Forte@Sun.COM svn->svn_path, strerror(errno));
340*7836SJohn.Forte@Sun.COM return;
341*7836SJohn.Forte@Sun.COM }
342*7836SJohn.Forte@Sun.COM
343*7836SJohn.Forte@Sun.COM svc.svc_major = major(stb.st_rdev);
344*7836SJohn.Forte@Sun.COM svc.svc_minor = minor(stb.st_rdev);
345*7836SJohn.Forte@Sun.COM (void) strncpy(svc.svc_path, svn->svn_path, sizeof (svc.svc_path));
346*7836SJohn.Forte@Sun.COM
347*7836SJohn.Forte@Sun.COM svc.svc_flag = svn->svn_mode;
348*7836SJohn.Forte@Sun.COM svc.svc_error = spcs_s_ucreate();
349*7836SJohn.Forte@Sun.COM
350*7836SJohn.Forte@Sun.COM if (ioctl(fd, SVIOC_ENABLE, &svc) < 0) {
351*7836SJohn.Forte@Sun.COM spcs_log("sv", &svc.svc_error,
352*7836SJohn.Forte@Sun.COM gettext("%s: unable to resume %s"),
353*7836SJohn.Forte@Sun.COM program, svn->svn_path);
354*7836SJohn.Forte@Sun.COM
355*7836SJohn.Forte@Sun.COM warn(&svc.svc_error, gettext("unable to resume %s"),
356*7836SJohn.Forte@Sun.COM svn->svn_path);
357*7836SJohn.Forte@Sun.COM return;
358*7836SJohn.Forte@Sun.COM }
359*7836SJohn.Forte@Sun.COM
360*7836SJohn.Forte@Sun.COM spcs_log("sv", NULL, gettext("%s: resume %s"),
361*7836SJohn.Forte@Sun.COM program, svn->svn_path);
362*7836SJohn.Forte@Sun.COM
363*7836SJohn.Forte@Sun.COM spcs_s_ufree(&svc.svc_error);
364*7836SJohn.Forte@Sun.COM }
365*7836SJohn.Forte@Sun.COM
366*7836SJohn.Forte@Sun.COM
367*7836SJohn.Forte@Sun.COM /*
368*7836SJohn.Forte@Sun.COM * This routine parses the config file and
369*7836SJohn.Forte@Sun.COM * stores the data in the svn array. The return value is the number
370*7836SJohn.Forte@Sun.COM * of entries read from conf_file. If an error occurs the error()
371*7836SJohn.Forte@Sun.COM * routine is called (which exits the program).
372*7836SJohn.Forte@Sun.COM */
373*7836SJohn.Forte@Sun.COM static int
read_libcfg(sv_name_t svn[])374*7836SJohn.Forte@Sun.COM read_libcfg(sv_name_t svn[])
375*7836SJohn.Forte@Sun.COM {
376*7836SJohn.Forte@Sun.COM char rdev[CFG_MAX_BUF];
377*7836SJohn.Forte@Sun.COM char key[CFG_MAX_KEY];
378*7836SJohn.Forte@Sun.COM struct stat stb;
379*7836SJohn.Forte@Sun.COM int i;
380*7836SJohn.Forte@Sun.COM int setnumber;
381*7836SJohn.Forte@Sun.COM int index = 0; /* Current location in svn array */
382*7836SJohn.Forte@Sun.COM sv_name_t *cur_svn; /* Pointer to svn[index] */
383*7836SJohn.Forte@Sun.COM CFGFILE *cfg;
384*7836SJohn.Forte@Sun.COM
385*7836SJohn.Forte@Sun.COM if ((cfg = cfg_open("")) == NULL) {
386*7836SJohn.Forte@Sun.COM error(NULL, gettext("Error opening config: %s"),
387*7836SJohn.Forte@Sun.COM strerror(errno));
388*7836SJohn.Forte@Sun.COM }
389*7836SJohn.Forte@Sun.COM
390*7836SJohn.Forte@Sun.COM cfg_resource(cfg, cfg_cluster_tag);
391*7836SJohn.Forte@Sun.COM if (!cfg_lock(cfg, CFG_RDLOCK)) {
392*7836SJohn.Forte@Sun.COM error(NULL, gettext("Error locking config: %s"),
393*7836SJohn.Forte@Sun.COM strerror(errno));
394*7836SJohn.Forte@Sun.COM }
395*7836SJohn.Forte@Sun.COM
396*7836SJohn.Forte@Sun.COM for (i = 0; /*CSTYLED*/; i++) {
397*7836SJohn.Forte@Sun.COM setnumber = i + 1;
398*7836SJohn.Forte@Sun.COM
399*7836SJohn.Forte@Sun.COM bzero(rdev, CFG_MAX_BUF);
400*7836SJohn.Forte@Sun.COM (void) snprintf(key, sizeof (key), "sv.set%d.vol", setnumber);
401*7836SJohn.Forte@Sun.COM if (cfg_get_cstring(cfg, key, rdev, sizeof (rdev)) < 0)
402*7836SJohn.Forte@Sun.COM break;
403*7836SJohn.Forte@Sun.COM
404*7836SJohn.Forte@Sun.COM /* Check to see if the raw device is present */
405*7836SJohn.Forte@Sun.COM if (stat(rdev, &stb) != 0) {
406*7836SJohn.Forte@Sun.COM warn(NULL, gettext("unable to access %s: %s"),
407*7836SJohn.Forte@Sun.COM rdev, strerror(errno));
408*7836SJohn.Forte@Sun.COM continue;
409*7836SJohn.Forte@Sun.COM }
410*7836SJohn.Forte@Sun.COM
411*7836SJohn.Forte@Sun.COM if (!S_ISCHR(stb.st_mode)) {
412*7836SJohn.Forte@Sun.COM warn(NULL, gettext("%s is not a character device"),
413*7836SJohn.Forte@Sun.COM rdev);
414*7836SJohn.Forte@Sun.COM continue;
415*7836SJohn.Forte@Sun.COM }
416*7836SJohn.Forte@Sun.COM
417*7836SJohn.Forte@Sun.COM cur_svn = &svn[index]; /* For easier reading below */
418*7836SJohn.Forte@Sun.COM
419*7836SJohn.Forte@Sun.COM if (strlen(rdev) >= sizeof (cur_svn->svn_path)) {
420*7836SJohn.Forte@Sun.COM warn(NULL, gettext(
421*7836SJohn.Forte@Sun.COM "raw device name (%s) longer than %d characters"),
422*7836SJohn.Forte@Sun.COM rdev,
423*7836SJohn.Forte@Sun.COM (sizeof (cur_svn->svn_path) - 1));
424*7836SJohn.Forte@Sun.COM continue;
425*7836SJohn.Forte@Sun.COM }
426*7836SJohn.Forte@Sun.COM
427*7836SJohn.Forte@Sun.COM (void) strcpy(cur_svn->svn_path, rdev);
428*7836SJohn.Forte@Sun.COM cur_svn->svn_mode = (NSC_DEVICE | NSC_CACHE);
429*7836SJohn.Forte@Sun.COM
430*7836SJohn.Forte@Sun.COM index++;
431*7836SJohn.Forte@Sun.COM }
432*7836SJohn.Forte@Sun.COM
433*7836SJohn.Forte@Sun.COM cfg_close(cfg);
434*7836SJohn.Forte@Sun.COM
435*7836SJohn.Forte@Sun.COM /* Set the last path to NULL */
436*7836SJohn.Forte@Sun.COM svn[index].svn_path[0] = '\0';
437*7836SJohn.Forte@Sun.COM
438*7836SJohn.Forte@Sun.COM return (index);
439*7836SJohn.Forte@Sun.COM }
440*7836SJohn.Forte@Sun.COM
441*7836SJohn.Forte@Sun.COM
442*7836SJohn.Forte@Sun.COM static void
suspend_dev(int fd,const caddr_t path)443*7836SJohn.Forte@Sun.COM suspend_dev(int fd, const caddr_t path)
444*7836SJohn.Forte@Sun.COM {
445*7836SJohn.Forte@Sun.COM struct stat stb;
446*7836SJohn.Forte@Sun.COM sv_conf_t svc;
447*7836SJohn.Forte@Sun.COM
448*7836SJohn.Forte@Sun.COM if (stat(path, &stb) < 0) {
449*7836SJohn.Forte@Sun.COM svc.svc_major = (major_t)-1;
450*7836SJohn.Forte@Sun.COM svc.svc_minor = (minor_t)-1;
451*7836SJohn.Forte@Sun.COM } else {
452*7836SJohn.Forte@Sun.COM svc.svc_major = major(stb.st_rdev);
453*7836SJohn.Forte@Sun.COM svc.svc_minor = minor(stb.st_rdev);
454*7836SJohn.Forte@Sun.COM }
455*7836SJohn.Forte@Sun.COM
456*7836SJohn.Forte@Sun.COM (void) strcpy(svc.svc_path, path);
457*7836SJohn.Forte@Sun.COM svc.svc_error = spcs_s_ucreate();
458*7836SJohn.Forte@Sun.COM
459*7836SJohn.Forte@Sun.COM if (ioctl(fd, SVIOC_DISABLE, &svc) < 0) {
460*7836SJohn.Forte@Sun.COM if (errno != SV_EDISABLED) {
461*7836SJohn.Forte@Sun.COM spcs_log("sv", &svc.svc_error,
462*7836SJohn.Forte@Sun.COM gettext("%s: unable to suspend %s"),
463*7836SJohn.Forte@Sun.COM program, path);
464*7836SJohn.Forte@Sun.COM
465*7836SJohn.Forte@Sun.COM warn(&svc.svc_error,
466*7836SJohn.Forte@Sun.COM gettext("unable to suspend %s"), path);
467*7836SJohn.Forte@Sun.COM return;
468*7836SJohn.Forte@Sun.COM }
469*7836SJohn.Forte@Sun.COM }
470*7836SJohn.Forte@Sun.COM
471*7836SJohn.Forte@Sun.COM spcs_log("sv", NULL, gettext("%s: suspend %s"), program, path);
472*7836SJohn.Forte@Sun.COM
473*7836SJohn.Forte@Sun.COM spcs_s_ufree(&svc.svc_error);
474*7836SJohn.Forte@Sun.COM }
475*7836SJohn.Forte@Sun.COM
476*7836SJohn.Forte@Sun.COM
477*7836SJohn.Forte@Sun.COM static void
suspend_sv(void)478*7836SJohn.Forte@Sun.COM suspend_sv(void)
479*7836SJohn.Forte@Sun.COM {
480*7836SJohn.Forte@Sun.COM sv_name_t *svn, *svn_system; /* Devices in system */
481*7836SJohn.Forte@Sun.COM sv_list_t svl_system;
482*7836SJohn.Forte@Sun.COM int i;
483*7836SJohn.Forte@Sun.COM int fd;
484*7836SJohn.Forte@Sun.COM
485*7836SJohn.Forte@Sun.COM svn_system = sv_alloc_svnames();
486*7836SJohn.Forte@Sun.COM
487*7836SJohn.Forte@Sun.COM svl_system.svl_count = read_libcfg(svn_system);
488*7836SJohn.Forte@Sun.COM
489*7836SJohn.Forte@Sun.COM if ((fd = open(sv_rpath, O_RDONLY)) < 0) {
490*7836SJohn.Forte@Sun.COM warn(NULL, gettext("unable to open %s: %s"),
491*7836SJohn.Forte@Sun.COM sv_rpath, strerror(errno));
492*7836SJohn.Forte@Sun.COM return;
493*7836SJohn.Forte@Sun.COM }
494*7836SJohn.Forte@Sun.COM
495*7836SJohn.Forte@Sun.COM for (i = 0; i < svl_system.svl_count; i++) {
496*7836SJohn.Forte@Sun.COM if (*svn_system[i].svn_path == '\0')
497*7836SJohn.Forte@Sun.COM break;
498*7836SJohn.Forte@Sun.COM
499*7836SJohn.Forte@Sun.COM svn = &svn_system[i];
500*7836SJohn.Forte@Sun.COM suspend_dev(fd, svn->svn_path);
501*7836SJohn.Forte@Sun.COM }
502*7836SJohn.Forte@Sun.COM
503*7836SJohn.Forte@Sun.COM (void) close(fd);
504*7836SJohn.Forte@Sun.COM }
505*7836SJohn.Forte@Sun.COM
506*7836SJohn.Forte@Sun.COM
507*7836SJohn.Forte@Sun.COM /*
508*7836SJohn.Forte@Sun.COM * Check kernel's sv_ndevices and thread sets,
509*7836SJohn.Forte@Sun.COM * if empty then change kernel state to allow unload,
510*7836SJohn.Forte@Sun.COM * and sleep SV_WAIT_UNLAOD (10 seconds).
511*7836SJohn.Forte@Sun.COM *
512*7836SJohn.Forte@Sun.COM * Only called in pkgrm time.
513*7836SJohn.Forte@Sun.COM */
514*7836SJohn.Forte@Sun.COM static void
prepare_unload_sv(void)515*7836SJohn.Forte@Sun.COM prepare_unload_sv(void)
516*7836SJohn.Forte@Sun.COM {
517*7836SJohn.Forte@Sun.COM int fd;
518*7836SJohn.Forte@Sun.COM int rc = 0;
519*7836SJohn.Forte@Sun.COM
520*7836SJohn.Forte@Sun.COM if ((fd = open(sv_rpath, O_RDONLY)) < 0) {
521*7836SJohn.Forte@Sun.COM warn(NULL, gettext("unable to open %s: %s"),
522*7836SJohn.Forte@Sun.COM sv_rpath, strerror(errno));
523*7836SJohn.Forte@Sun.COM return;
524*7836SJohn.Forte@Sun.COM }
525*7836SJohn.Forte@Sun.COM
526*7836SJohn.Forte@Sun.COM if (ioctl(fd, SVIOC_UNLOAD, &rc) < 0)
527*7836SJohn.Forte@Sun.COM error(NULL, gettext("unable to unload"));
528*7836SJohn.Forte@Sun.COM
529*7836SJohn.Forte@Sun.COM if (rc != 0)
530*7836SJohn.Forte@Sun.COM error(NULL, gettext("still has active devices or threads"));
531*7836SJohn.Forte@Sun.COM
532*7836SJohn.Forte@Sun.COM (void) close(fd);
533*7836SJohn.Forte@Sun.COM }
534