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