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 2000-2003 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 #include "rdlist.h"
30*0Sstevel@tonic-gate #include "rdtable.h"
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate static int list_read(int listt, int elemn);
33*0Sstevel@tonic-gate static int lwp_write(list_t *list);
34*0Sstevel@tonic-gate static int lwp_read(int lwpn);
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate * This procedure stores the current state of the lists (lwps, processes,
38*0Sstevel@tonic-gate * users and project) into the file defined by 'file'.
39*0Sstevel@tonic-gate * param file - the file name to be used
40*0Sstevel@tonic-gate * return 0, or -1 on error and store the error message in
41*0Sstevel@tonic-gate * the global buffer 'errmsg'.
42*0Sstevel@tonic-gate */
43*0Sstevel@tonic-gate int
list_store(char * file)44*0Sstevel@tonic-gate list_store(char *file)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate int storefd;
47*0Sstevel@tonic-gate time_t tv;
48*0Sstevel@tonic-gate char *tstr;
49*0Sstevel@tonic-gate int ret = -1;
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate if ((storefd = open(file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR))
52*0Sstevel@tonic-gate == -1) {
53*0Sstevel@tonic-gate format_err("can't open list db: %s\n", file);
54*0Sstevel@tonic-gate (void) fprintf(stderr, errmsg);
55*0Sstevel@tonic-gate return (-1);
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate log_msg("writing persistence file: %s\n", file);
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate * the next do {..} while (false); statement is a replacement
61*0Sstevel@tonic-gate * of goto;
62*0Sstevel@tonic-gate */
63*0Sstevel@tonic-gate do {
64*0Sstevel@tonic-gate if (open_prot(storefd, "w") == -1) {
65*0Sstevel@tonic-gate format_err("can't open list db: %s\n", file);
66*0Sstevel@tonic-gate (void) fprintf(stderr, errmsg);
67*0Sstevel@tonic-gate break;
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate (void) time(&tv);
70*0Sstevel@tonic-gate tstr = ctime(&tv);
71*0Sstevel@tonic-gate if (wr_string("# RDS data base file generated on: ") == -1)
72*0Sstevel@tonic-gate break;
73*0Sstevel@tonic-gate if (wr_string(tstr) == -1)
74*0Sstevel@tonic-gate break;
75*0Sstevel@tonic-gate if (wr_value(LTDB_VERSION_KEY, LTDB_VERSION) == -1)
76*0Sstevel@tonic-gate break;
77*0Sstevel@tonic-gate if (wr_value(LTDB_TIMESTAMP, tv) == -1)
78*0Sstevel@tonic-gate break;
79*0Sstevel@tonic-gate /* we will write 4 lists */
80*0Sstevel@tonic-gate if (wr_lshead(4) != 0) {
81*0Sstevel@tonic-gate format_err("can't write into list db: %s\n",
82*0Sstevel@tonic-gate "./listdb");
83*0Sstevel@tonic-gate break;
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate if (list_write(L_LWP, 0) == -1)
86*0Sstevel@tonic-gate break;
87*0Sstevel@tonic-gate if (list_write(L_PRC_SI, 0) == -1)
88*0Sstevel@tonic-gate break;
89*0Sstevel@tonic-gate if (list_write(L_USR_SI, 0) == -1)
90*0Sstevel@tonic-gate break;
91*0Sstevel@tonic-gate if (list_write(L_PRJ_SI, 0) == -1)
92*0Sstevel@tonic-gate break;
93*0Sstevel@tonic-gate ret = 0;
94*0Sstevel@tonic-gate } while (ret);
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate if (ret == 0) {
97*0Sstevel@tonic-gate struct stat stat_buf;
98*0Sstevel@tonic-gate (void) fstat(storefd, &stat_buf);
99*0Sstevel@tonic-gate log_msg("wrote: %ld bytes\n", stat_buf.st_size);
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate /* close_prot(); */
103*0Sstevel@tonic-gate (void) close(storefd);
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate return (ret);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate * This procedure restores the last state of the lists (lwps, processes,
110*0Sstevel@tonic-gate * users and project) from the file defined by 'file'.
111*0Sstevel@tonic-gate * param file - the file name to be used
112*0Sstevel@tonic-gate * return 0, or -1 on error and store the error message in
113*0Sstevel@tonic-gate * the global buffer 'errmsg'.
114*0Sstevel@tonic-gate */
115*0Sstevel@tonic-gate int
list_restore(char * file)116*0Sstevel@tonic-gate list_restore(char *file)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate int storefd;
119*0Sstevel@tonic-gate int listt, elemn, listn;
120*0Sstevel@tonic-gate int64_t timestamp;
121*0Sstevel@tonic-gate time_t tv;
122*0Sstevel@tonic-gate int version;
123*0Sstevel@tonic-gate int ret = -1;
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate if ((storefd = open(file, O_RDONLY)) == -1)
126*0Sstevel@tonic-gate return (ret);
127*0Sstevel@tonic-gate log_msg("reading persistence file: %s\n", file);
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * the next do {..} while (false); statement is a replacement
131*0Sstevel@tonic-gate * of goto;
132*0Sstevel@tonic-gate */
133*0Sstevel@tonic-gate do {
134*0Sstevel@tonic-gate if (open_prot(storefd, "r") == -1)
135*0Sstevel@tonic-gate break;
136*0Sstevel@tonic-gate if (skip_line() == -1)
137*0Sstevel@tonic-gate break;
138*0Sstevel@tonic-gate if ((version = r_value(LTDB_VERSION_KEY)) == -1)
139*0Sstevel@tonic-gate break;
140*0Sstevel@tonic-gate if (version != LTDB_VERSION) {
141*0Sstevel@tonic-gate (void) fprintf(stderr,
142*0Sstevel@tonic-gate "wrong version %d of db file %s\n",
143*0Sstevel@tonic-gate version, file);
144*0Sstevel@tonic-gate break;
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate if ((timestamp = r_value(LTDB_TIMESTAMP)) == -1)
147*0Sstevel@tonic-gate break;
148*0Sstevel@tonic-gate /* check the file decay time is expired */
149*0Sstevel@tonic-gate (void) time(&tv);
150*0Sstevel@tonic-gate if ((tv - timestamp) > LTDB_DECAYTIME)
151*0Sstevel@tonic-gate break;
152*0Sstevel@tonic-gate if ((listn = r_lshead()) == -1)
153*0Sstevel@tonic-gate break;
154*0Sstevel@tonic-gate while (listn-- > 0) {
155*0Sstevel@tonic-gate if ((elemn = r_lhead(&listt)) == -1)
156*0Sstevel@tonic-gate break;
157*0Sstevel@tonic-gate if (list_read(listt, elemn) != 0) {
158*0Sstevel@tonic-gate break;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate ret = 0;
162*0Sstevel@tonic-gate } while (ret);
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate if (ret == 0) {
165*0Sstevel@tonic-gate struct stat stat_buf;
166*0Sstevel@tonic-gate (void) fstat(storefd, &stat_buf);
167*0Sstevel@tonic-gate log_msg("read: %ld bytes\n", stat_buf.st_size);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /* close_prot(); */
171*0Sstevel@tonic-gate (void) close(storefd);
172*0Sstevel@tonic-gate (void) unlink(file);
173*0Sstevel@tonic-gate return (ret);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate * This procedure writes a list of type 'listt' according to the
178*0Sstevel@tonic-gate * rds interface protocol. It uses the already opened and initialized
179*0Sstevel@tonic-gate * protocol module (see file protocol.[c,h]).
180*0Sstevel@tonic-gate * param listt - the type of the list, see rdimpl.h
181*0Sstevel@tonic-gate * param Po - print option, if 1 the list will be also printed on stdout.
182*0Sstevel@tonic-gate * return 0, or -1 on error and store the error message in
183*0Sstevel@tonic-gate * the global buffer 'errmsg'.
184*0Sstevel@tonic-gate */
185*0Sstevel@tonic-gate int
list_write(int listt,int Po)186*0Sstevel@tonic-gate list_write(int listt, int Po)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate char idstr[P_MAXVAL];
189*0Sstevel@tonic-gate list_t *list;
190*0Sstevel@tonic-gate id_info_t *id = NULL, *nextid;
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate if (listt == L_LWP) {
193*0Sstevel@tonic-gate return (lwp_write(&lwps));
194*0Sstevel@tonic-gate } else if (listt == L_SYSTEM) {
195*0Sstevel@tonic-gate if (wr_lhead(listt, 1) != 0) {
196*0Sstevel@tonic-gate format_err(
197*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
198*0Sstevel@tonic-gate return (-1);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate (void) snprintf(idstr, sizeof (idstr), "%s", sys_info.name);
201*0Sstevel@tonic-gate if (wr_element(listt, (char *)(&sys_info), idstr) != 0) {
202*0Sstevel@tonic-gate format_err(
203*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
204*0Sstevel@tonic-gate return (-1);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate } else {
208*0Sstevel@tonic-gate switch (listt) {
209*0Sstevel@tonic-gate case L_PRC_SI : list = &processes;
210*0Sstevel@tonic-gate break;
211*0Sstevel@tonic-gate case L_AC_USR :
212*0Sstevel@tonic-gate case L_USR_SI : list = &users;
213*0Sstevel@tonic-gate break;
214*0Sstevel@tonic-gate case L_AC_PRJ :
215*0Sstevel@tonic-gate case L_PRJ_SI : list = &projects;
216*0Sstevel@tonic-gate break;
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate id = list->l_head;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate if (wr_lhead(listt, list->l_count) != 0) {
221*0Sstevel@tonic-gate format_err(
222*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
223*0Sstevel@tonic-gate return (-1);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate while (id != NULL) {
226*0Sstevel@tonic-gate switch (listt) {
227*0Sstevel@tonic-gate case L_PRC_SI :
228*0Sstevel@tonic-gate (void) sprintf(idstr, "%d", id->id_pid);
229*0Sstevel@tonic-gate break;
230*0Sstevel@tonic-gate case L_AC_USR :
231*0Sstevel@tonic-gate case L_USR_SI :
232*0Sstevel@tonic-gate (void) sprintf(idstr, "%d", id->id_uid);
233*0Sstevel@tonic-gate break;
234*0Sstevel@tonic-gate case L_AC_PRJ :
235*0Sstevel@tonic-gate case L_PRJ_SI :
236*0Sstevel@tonic-gate (void) snprintf(idstr, sizeof (idstr), "%s",
237*0Sstevel@tonic-gate id->id_name);
238*0Sstevel@tonic-gate break;
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate if (wr_element(listt, (char *)id, idstr) != 0) {
241*0Sstevel@tonic-gate format_err(
242*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate if (Po == 1)
245*0Sstevel@tonic-gate prtelement(stderr, id);
246*0Sstevel@tonic-gate nextid = id->id_next;
247*0Sstevel@tonic-gate id = nextid;
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate }
250*0Sstevel@tonic-gate return (0);
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate /*
254*0Sstevel@tonic-gate * This procedure prints out all list elements on stdout. The elements
255*0Sstevel@tonic-gate * int the list must be of type id_info_t.
256*0Sstevel@tonic-gate * param list - the list to be printed
257*0Sstevel@tonic-gate */
258*0Sstevel@tonic-gate void
list_print(list_t * list,int xid)259*0Sstevel@tonic-gate list_print(list_t *list, int xid)
260*0Sstevel@tonic-gate {
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate id_info_t *id = list->l_head;
263*0Sstevel@tonic-gate id_info_t *nextid;
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate while (id) {
266*0Sstevel@tonic-gate if (xid == -1) {
267*0Sstevel@tonic-gate prtelement(stdout, id);
268*0Sstevel@tonic-gate } else {
269*0Sstevel@tonic-gate switch (list->l_type) {
270*0Sstevel@tonic-gate case LT_PROCESS : if (xid == id->id_pid)
271*0Sstevel@tonic-gate prtelement(stdout, id);
272*0Sstevel@tonic-gate break;
273*0Sstevel@tonic-gate case LT_USERS : if (xid == id->id_uid)
274*0Sstevel@tonic-gate prtelement(stdout, id);
275*0Sstevel@tonic-gate break;
276*0Sstevel@tonic-gate case LT_PROJECTS : if (xid == id->id_projid)
277*0Sstevel@tonic-gate prtelement(stdout, id);
278*0Sstevel@tonic-gate break;
279*0Sstevel@tonic-gate default: prtelement(stdout, id);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate nextid = id->id_next;
283*0Sstevel@tonic-gate id = nextid;
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate }
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate static int
list_read(int listt,int elemn)289*0Sstevel@tonic-gate list_read(int listt, int elemn)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate char idstr[P_MAXVAL];
292*0Sstevel@tonic-gate list_t *list;
293*0Sstevel@tonic-gate id_info_t *id;
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate if (listt == L_LWP)
296*0Sstevel@tonic-gate return (lwp_read(elemn));
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate while (elemn-- > 0) {
299*0Sstevel@tonic-gate switch (listt) {
300*0Sstevel@tonic-gate case L_PRC_SI : list = &processes;
301*0Sstevel@tonic-gate break;
302*0Sstevel@tonic-gate case L_USR_SI : list = &users;
303*0Sstevel@tonic-gate break;
304*0Sstevel@tonic-gate case L_PRJ_SI : list = &projects;
305*0Sstevel@tonic-gate break;
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate if (list->l_head == NULL) { /* first element */
309*0Sstevel@tonic-gate list->l_head = list->l_tail = id =
310*0Sstevel@tonic-gate Zalloc(sizeof (id_info_t));
311*0Sstevel@tonic-gate list->l_count++;
312*0Sstevel@tonic-gate } else {
313*0Sstevel@tonic-gate /* a new element */
314*0Sstevel@tonic-gate id = list->l_tail;
315*0Sstevel@tonic-gate id->id_next = Zalloc(sizeof (id_info_t));
316*0Sstevel@tonic-gate id->id_next->id_prev = list->l_tail;
317*0Sstevel@tonic-gate id->id_next->id_next = NULL;
318*0Sstevel@tonic-gate list->l_tail = id->id_next;
319*0Sstevel@tonic-gate id = list->l_tail;
320*0Sstevel@tonic-gate list->l_count++;
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate if (r_element((char *)id, idstr) == -1) {
323*0Sstevel@tonic-gate list_clear(list);
324*0Sstevel@tonic-gate return (-1);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate return (0);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate static int
lwp_write(list_t * list)331*0Sstevel@tonic-gate lwp_write(list_t *list)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate lwpinfo_t lwpsi;
334*0Sstevel@tonic-gate lwp_info_t *li = NULL, *nextli;
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate li = list->l_head;
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate if (wr_lhead(L_LWP, list->l_count) != 0) {
339*0Sstevel@tonic-gate format_err(
340*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
341*0Sstevel@tonic-gate err_exit();
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate while (li != NULL) {
344*0Sstevel@tonic-gate lwpsi.pr_pid = li->li_psinfo->pr_pid;
345*0Sstevel@tonic-gate lwpsi.pr_lwpid = li->li_lwpsinfo->pr_lwpid;
346*0Sstevel@tonic-gate
347*0Sstevel@tonic-gate if (wr_element(L_LWP__I, (char *)&lwpsi, "lwpi") != 0) {
348*0Sstevel@tonic-gate format_err(
349*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate if (wr_element(L_LWP__U, (char *)&(li->li_usage), "lwpu")
352*0Sstevel@tonic-gate != 0) {
353*0Sstevel@tonic-gate format_err(
354*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate if (wr_element(L_LWP, (char *)li, "lwp") != 0) {
357*0Sstevel@tonic-gate format_err(
358*0Sstevel@tonic-gate "RDS protocol error: cannot write list header");
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate nextli = li->li_next;
361*0Sstevel@tonic-gate li = nextli;
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate return (0);
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate
366*0Sstevel@tonic-gate static int
lwp_read(int lwpn)367*0Sstevel@tonic-gate lwp_read(int lwpn)
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate lwp_info_t *lwp;
370*0Sstevel@tonic-gate lwpinfo_t lwpsi;
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate char idstr[P_MAXVAL];
373*0Sstevel@tonic-gate
374*0Sstevel@tonic-gate while (lwpn-- > 0) {
375*0Sstevel@tonic-gate if (r_element((char *)&lwpsi, idstr) == -1) {
376*0Sstevel@tonic-gate return (-1);
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate lwp = list_add_lwp(&lwps, lwpsi.pr_pid, lwpsi.pr_lwpid);
379*0Sstevel@tonic-gate lwp->li_psinfo->pr_pid = lwpsi.pr_pid;
380*0Sstevel@tonic-gate lwp->li_lwpsinfo->pr_lwpid = lwpsi.pr_lwpid;
381*0Sstevel@tonic-gate if (r_element((char *)&(lwp->li_usage), idstr) == -1) {
382*0Sstevel@tonic-gate return (-1);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate if (r_element((char *)lwp, idstr) == -1) {
385*0Sstevel@tonic-gate return (-1);
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate
388*0Sstevel@tonic-gate }
389*0Sstevel@tonic-gate return (0);
390*0Sstevel@tonic-gate }
391