1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * Creates and maintains a short-term cache of live upgrade slices.
31*0Sstevel@tonic-gate */
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <dirent.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <synch.h>
38*0Sstevel@tonic-gate #include <sys/errno.h>
39*0Sstevel@tonic-gate #include <sys/param.h>
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/stat.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/wait.h>
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate #include "libdiskmgt.h"
47*0Sstevel@tonic-gate #include "disks_private.h"
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate #define TMPNM_SIZE 25
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate * The list of live upgrade slices in use.
53*0Sstevel@tonic-gate */
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate struct lu_list {
56*0Sstevel@tonic-gate struct lu_list *next;
57*0Sstevel@tonic-gate char *slice;
58*0Sstevel@tonic-gate char *name;
59*0Sstevel@tonic-gate };
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate static struct lu_list *lu_listp = NULL;
62*0Sstevel@tonic-gate static time_t timestamp = 0;
63*0Sstevel@tonic-gate static mutex_t lu_lock = DEFAULTMUTEX;
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate static int add_use_record(char *devname, char *name);
66*0Sstevel@tonic-gate static void free_lu(struct lu_list *listp);
67*0Sstevel@tonic-gate static int load_lu();
68*0Sstevel@tonic-gate static int lustatus(int fd);
69*0Sstevel@tonic-gate static int lufslist(int fd);
70*0Sstevel@tonic-gate static int run_cmd(char *path, char *cmd, char *arg, int fd);
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate * Search the list of devices under live upgrade for the specified device.
74*0Sstevel@tonic-gate */
75*0Sstevel@tonic-gate int
inuse_lu(char * slice,nvlist_t * attrs,int * errp)76*0Sstevel@tonic-gate inuse_lu(char *slice, nvlist_t *attrs, int *errp)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate int found = 0;
79*0Sstevel@tonic-gate time_t curr_time;
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate *errp = 0;
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate if (slice == NULL) {
84*0Sstevel@tonic-gate return (found);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate /*
88*0Sstevel@tonic-gate * We don't want to have to re-read the live upgrade config for
89*0Sstevel@tonic-gate * every slice, but we can't just cache it since there is no event
90*0Sstevel@tonic-gate * when this changes. So, we'll keep the config in memory for
91*0Sstevel@tonic-gate * a short time (1 minute) before reloading it.
92*0Sstevel@tonic-gate */
93*0Sstevel@tonic-gate (void) mutex_lock(&lu_lock);
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate curr_time = time(NULL);
96*0Sstevel@tonic-gate if (timestamp < curr_time && (curr_time - timestamp) > 60) {
97*0Sstevel@tonic-gate free_lu(lu_listp); /* free old entries */
98*0Sstevel@tonic-gate lu_listp = NULL;
99*0Sstevel@tonic-gate *errp = load_lu(); /* load the cache */
100*0Sstevel@tonic-gate timestamp = curr_time;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate if (*errp == 0) {
104*0Sstevel@tonic-gate struct lu_list *listp;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate listp = lu_listp;
107*0Sstevel@tonic-gate while (listp != NULL) {
108*0Sstevel@tonic-gate if (strcmp(slice, listp->slice) == 0) {
109*0Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_LU, errp);
110*0Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, listp->name, errp);
111*0Sstevel@tonic-gate found = 1;
112*0Sstevel@tonic-gate break;
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate listp = listp->next;
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate (void) mutex_unlock(&lu_lock);
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate return (found);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate static int
add_use_record(char * devname,char * name)124*0Sstevel@tonic-gate add_use_record(char *devname, char *name)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate struct lu_list *sp;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate sp = (struct lu_list *)malloc(sizeof (struct lu_list));
129*0Sstevel@tonic-gate if (sp == NULL) {
130*0Sstevel@tonic-gate return (ENOMEM);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate if ((sp->slice = strdup(devname)) == NULL) {
134*0Sstevel@tonic-gate free(sp);
135*0Sstevel@tonic-gate return (ENOMEM);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate if ((sp->name = strdup(name)) == NULL) {
139*0Sstevel@tonic-gate free(sp->slice);
140*0Sstevel@tonic-gate free(sp);
141*0Sstevel@tonic-gate return (ENOMEM);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate sp->next = lu_listp;
145*0Sstevel@tonic-gate lu_listp = sp;
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate return (0);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate /*
151*0Sstevel@tonic-gate * Free the list of liveupgrade entries.
152*0Sstevel@tonic-gate */
153*0Sstevel@tonic-gate static void
free_lu(struct lu_list * listp)154*0Sstevel@tonic-gate free_lu(struct lu_list *listp) {
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate struct lu_list *nextp;
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate while (listp != NULL) {
159*0Sstevel@tonic-gate nextp = listp->next;
160*0Sstevel@tonic-gate free((void *)listp->slice);
161*0Sstevel@tonic-gate free((void *)listp->name);
162*0Sstevel@tonic-gate free((void *)listp);
163*0Sstevel@tonic-gate listp = nextp;
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate * Create a list of live upgrade devices.
169*0Sstevel@tonic-gate */
170*0Sstevel@tonic-gate static int
load_lu()171*0Sstevel@tonic-gate load_lu()
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate char tmpname[TMPNM_SIZE];
174*0Sstevel@tonic-gate int fd;
175*0Sstevel@tonic-gate int status = 0;
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate (void) strlcpy(tmpname, "/var/run/dm_lu_XXXXXX", TMPNM_SIZE);
178*0Sstevel@tonic-gate if ((fd = mkstemp(tmpname)) != -1) {
179*0Sstevel@tonic-gate (void) unlink(tmpname);
180*0Sstevel@tonic-gate if (run_cmd("/usr/sbin/lustatus", "lustatus", NULL, fd)) {
181*0Sstevel@tonic-gate status = lustatus(fd);
182*0Sstevel@tonic-gate } else {
183*0Sstevel@tonic-gate (void) close(fd);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate return (status);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate * The XML generated by the live upgrade commands is not parseable by the
192*0Sstevel@tonic-gate * standard Solaris XML parser, so we have to do it ourselves.
193*0Sstevel@tonic-gate */
194*0Sstevel@tonic-gate static int
lufslist(int fd)195*0Sstevel@tonic-gate lufslist(int fd)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate FILE *fp;
198*0Sstevel@tonic-gate char line[MAXPATHLEN];
199*0Sstevel@tonic-gate int status;
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate if ((fp = fdopen(fd, "r")) == NULL) {
202*0Sstevel@tonic-gate (void) close(fd);
203*0Sstevel@tonic-gate return (0);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate (void) fseek(fp, 0L, SEEK_SET);
207*0Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) == line) {
208*0Sstevel@tonic-gate char *devp;
209*0Sstevel@tonic-gate char *nmp;
210*0Sstevel@tonic-gate char *ep;
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate if (strncmp(line, "<beFsComponent ", 15) != 0) {
213*0Sstevel@tonic-gate continue;
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate if ((devp = strstr(line, "fsDevice=\"")) == NULL) {
217*0Sstevel@tonic-gate continue;
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate devp = devp + 10;
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate if ((ep = strchr(devp, '"')) == NULL) {
223*0Sstevel@tonic-gate continue;
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate *ep = 0;
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate /* try to get the mountpoint name */
229*0Sstevel@tonic-gate if ((nmp = strstr(ep + 1, "mountPoint=\"")) != NULL) {
230*0Sstevel@tonic-gate nmp = nmp + 12;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if ((ep = strchr(nmp, '"')) != NULL) {
233*0Sstevel@tonic-gate *ep = 0;
234*0Sstevel@tonic-gate } else {
235*0Sstevel@tonic-gate nmp = "";
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate } else {
239*0Sstevel@tonic-gate nmp = "";
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate if ((status = add_use_record(devp, nmp)) != 0) {
243*0Sstevel@tonic-gate break;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate (void) fclose(fp);
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate return (status);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate static int
lustatus(int fd)253*0Sstevel@tonic-gate lustatus(int fd)
254*0Sstevel@tonic-gate {
255*0Sstevel@tonic-gate FILE *fp;
256*0Sstevel@tonic-gate char line[MAXPATHLEN];
257*0Sstevel@tonic-gate int status = 0;
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate if ((fp = fdopen(fd, "r")) == NULL) {
260*0Sstevel@tonic-gate (void) close(fd);
261*0Sstevel@tonic-gate return (0);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate (void) fseek(fp, 0L, SEEK_SET);
265*0Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) == line) {
266*0Sstevel@tonic-gate char *sp;
267*0Sstevel@tonic-gate char *ep;
268*0Sstevel@tonic-gate char tmpname[TMPNM_SIZE];
269*0Sstevel@tonic-gate int ffd;
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate if (strncmp(line, "<beStatus ", 10) != 0) {
272*0Sstevel@tonic-gate continue;
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate if ((sp = strstr(line, "name=\"")) == NULL) {
276*0Sstevel@tonic-gate continue;
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate sp = sp + 6;
280*0Sstevel@tonic-gate
281*0Sstevel@tonic-gate if ((ep = strchr(sp, '"')) == NULL) {
282*0Sstevel@tonic-gate continue;
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate *ep = 0;
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate (void) strlcpy(tmpname, "/var/run/dm_lu_XXXXXX", TMPNM_SIZE);
288*0Sstevel@tonic-gate if ((ffd = mkstemp(tmpname)) != -1) {
289*0Sstevel@tonic-gate (void) unlink(tmpname);
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate if (run_cmd("/usr/sbin/lufslist", "lufslist", sp, ffd) == 0) {
292*0Sstevel@tonic-gate (void) close(ffd);
293*0Sstevel@tonic-gate break;
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gate if ((status = lufslist(ffd)) != 0) {
297*0Sstevel@tonic-gate break;
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate (void) fclose(fp);
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gate return (status);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate static int
run_cmd(char * path,char * cmd,char * arg,int fd)308*0Sstevel@tonic-gate run_cmd(char *path, char *cmd, char *arg, int fd)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate pid_t pid;
311*0Sstevel@tonic-gate int loc;
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate /* create the server process */
314*0Sstevel@tonic-gate switch ((pid = fork1())) {
315*0Sstevel@tonic-gate case 0:
316*0Sstevel@tonic-gate /* child process */
317*0Sstevel@tonic-gate (void) close(1);
318*0Sstevel@tonic-gate (void) dup(fd);
319*0Sstevel@tonic-gate (void) close(2);
320*0Sstevel@tonic-gate (void) dup(fd);
321*0Sstevel@tonic-gate closefrom(3);
322*0Sstevel@tonic-gate (void) execl(path, cmd, "-X", arg, NULL);
323*0Sstevel@tonic-gate _exit(1);
324*0Sstevel@tonic-gate break;
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gate case -1:
327*0Sstevel@tonic-gate return (0);
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate default:
330*0Sstevel@tonic-gate /* parent process */
331*0Sstevel@tonic-gate break;
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate (void) waitpid(pid, &loc, 0);
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate /* printf("got 0x%x %d %d\n", loc, WIFEXITED(loc), WEXITSTATUS(loc)); */
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
339*0Sstevel@tonic-gate return (1);
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate return (0);
343*0Sstevel@tonic-gate }
344