Annotation of freem/src/namespace.c, revision 1.6
1.1 snw 1: /*
1.6 ! snw 2: * $Id: namespace.c,v 1.5 2025/04/09 19:52:02 snw Exp $
1.1 snw 3: * Namespace support
4: *
5: *
1.2 snw 6: * Author: Serena Willis <snw@coherent-logic.com>
1.1 snw 7: * Copyright (C) 1998 MUG Deutschland
1.3 snw 8: * Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1 snw 9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
1.4 snw 26: * $Log: namespace.c,v $
1.6 ! snw 27: * Revision 1.5 2025/04/09 19:52:02 snw
! 28: * Eliminate as many warnings as possible while building with -Wall
! 29: *
1.5 snw 30: * Revision 1.4 2025/04/02 03:02:42 snw
31: * Stop requiring users to pass -e to fmadm when -u or -g are passed
32: *
1.4 snw 33: * Revision 1.3 2025/03/09 19:50:47 snw
34: * Second phase of REUSE compliance and header reformat
35: *
1.3 snw 36: *
37: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
38: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 39: **/
40:
41: #include <stddef.h>
42: #include <stdio.h>
43: #include <string.h>
44: #include <unistd.h>
45: #include <stdlib.h>
46:
47: #include "mpsdef.h"
48: #include "iniconf.h"
49: #include "journal.h"
50: #include "init.h"
51: #include "namespace.h"
52:
53: #include <limits.h>
54:
55: #if !defined(PATH_MAX) && defined(_SCO_DS)
56: # define PATH_MAX 4096
57: #endif
58:
59: #if !defined(PATH_MAX) && defined(__gnu_hurd__)
60: # define PATH_MAX 1024
61: #endif
62:
63: #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
64: # include <sys/syslimits.h>
65: #endif
66:
67: #define LOCK 'l'
68:
69: void ns_error(char *ns, char *e)
70: {
71: char msg_buf[256];
72:
1.6 ! snw 73: snprintf(msg_buf, sizeof (msg_buf) - 1, "error switching to namespace '%s': %s (possibly a configuration error?)\r\n\201", ns, e);
1.1 snw 74: write_m(msg_buf);
75:
76: return;
77: }
78:
79: void set_namespace(char *ns, int verbose)
80: {
81: register int i;
82:
83: char tmps[256];
84:
85: char notif[256];
86: char ns_m[256];
87: char ns_buf[PATH_MAX];
88:
89: char jour_file[PATH_MAX];
90:
91: unsigned long cut_threshold = 1073741824; /* default journal cut threshold of 1GiB */
92:
93: strncpy (ns_m, ns, 256 - 1);
94: stcnv_c2m (ns_m);
95:
96:
97: /* get the root directory of the namespace */
98: get_conf (ns, "root", nsroot);
99:
100: if(!file_exists (config_file)) {
1.6 ! snw 101: snprintf (tmps, sizeof (tmps) - 1, "configuration file '%s' does not exist.\n", config_file);
1.1 snw 102: ns_error (ns, tmps);
103:
104: cleanup ();
105:
106: exit (1);
107: }
108:
109:
110: /* turn off all the old so-called "journal" implementation */
111: ug_buf[HOME][0] = EOL;
112: jour_flag = 0;
113:
114: /* the real journal file */
115: jour_file[0] = NUL;
116:
117:
118: /* only read journal config for SYSTEM namespace, as journaling
119: is across all namespaces */
120:
121: /* clear private buffer */
122: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
123:
124: if(get_conf("SYSTEM", "journal_file", ns_buf) == TRUE) {
125: strncpy (jour_file, ns_buf, PATH_MAX);
126: }
127:
128: if(get_conf("SYSTEM", "journal_host_id", ns_buf) == TRUE) {
129: strncpy (jour_hostid, ns_buf, 255);
130: }
131: else {
132: strncpy (jour_hostid, "DEFAULT", 255);
133: }
134:
135: if(get_conf("SYSTEM", "journal_cut_threshold", ns_buf) == TRUE) {
136: cut_threshold = (unsigned long) strtol (ns_buf, NULL, 0);
137: }
138:
139: /* clear private buffer */
140: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
141:
142: if(get_conf("SYSTEM", "journal_mode", ns_buf) == TRUE) {
143:
144: if(strcmp(ns_buf, "off") == 0) {
145: /* journaling is disabled */
146: }
147: else if(strcmp(ns_buf, "on") == 0) {
148:
149: if (jour_file[0] == NUL) {
150: ns_error ("SYSTEM", "journal file undefined while trying to set journal mode");
151: goto jour_end;
152: }
153:
154: jnl_init (jour_file, jour_hostid, cut_threshold, 0);
155:
156: }
157: else {
1.6 ! snw 158: snprintf (tmps, sizeof (tmps) - 1, "invalid journal_mode '%s'", ns_buf);
1.1 snw 159: ns_error ("SYSTEM", tmps);
160:
161: goto jour_end;
162: }
163:
164: }
165:
166:
167: jour_end:
168:
169:
170: /* clear private buffer */
171: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
172:
173: /* set up percent routines -- always in SYSTEM */
174: if(get_conf("SYSTEM", "routines_path", ns_buf) != TRUE) {
175: ns_error("SYSTEM", "could not get routines_path");
176: }
177: else {
178: stcnv_c2m(ns_buf);
179: stcpy(rou0plib, ns_buf); /* Set DO-GOTO-JOB % routine access path */
180: stcpy(rou1plib, ns_buf); /* Set ZLOAD-ZSAVE % routine access path */
181:
182: /* clear %-routine buffer */
183: for (i = 0; i < NO_OF_RBUF; i++) {
184:
185: if (pgms[i][0] == '%') {
186:
187: if (rouptr != (buff + (i * PSIZE0))) {
188: pgms[i][0] = EOL;
189: ages[i] = 0L;
190: }
191:
192: path[i][0] = EOL;
193: }
194:
195: }
196: }
197:
198: /* clear private buffer */
199: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
200:
201: /* set up percent globals -- always in SYSTEM */
202: if(get_conf("SYSTEM", "globals_path", ns_buf) != TRUE) {
203: ns_error("SYSTEM", "could not get globals_path");
204: }
205: else {
206: stcnv_c2m(ns_buf);
207: stcpy(gloplib, ns_buf); /* Set % globals path */
208:
209: /* close % globals */
210: for (i = 0; i < NO_GLOBLS; i++) {
211:
212: if (oldfil[i][0] == '%') {
213:
214: close (olddes[i]);
215:
216: usage[i] = 0;
217: olddes[i] = 0;
218: oldfil[i][0] = NUL;
219: }
220:
221: }
222: }
223:
224:
225: /* set up global engines */
226: /* SYSTEM */
227: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
228:
229: if(get_conf("SYSTEM", "global_engine", ns_buf) == TRUE) {
230: global_set_engine ('s', ns_buf);
231: }
232: else {
233: global_set_engine ('s', "BUILTIN");
234: }
235:
236: /* primary namespace */
237: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
238:
239: if(get_conf(ns, "global_engine", ns_buf) == TRUE) {
240: global_set_engine ('u', ns_buf);
241: }
242: else {
243: global_set_engine ('u', "BUILTIN");
244: }
245:
246: /* set up local engine */
247: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
248:
1.5 snw 249: if(get_conf (ns, "local_engine", ns_buf) == TRUE) {
250: snprintf (loc_engine, sizeof (loc_engine), "%s", ns_buf);
1.1 snw 251: }
252: else {
1.5 snw 253: sprintf (loc_engine, "BUILTIN");
1.1 snw 254: }
255:
256:
257: /* clear private buffer */
258: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
259:
260: /* set up regular routines */
261: if(get_conf(ns, "routines_path", ns_buf) != TRUE) {
262: if (verbose) {
263: ns_error(ns, "could not get routines_path");
264: }
265: else {
266: merr_raise (M26);
267: return;
268: }
269: }
270: else {
271: stcnv_c2m(ns_buf);
272: stcpy(rou0path, ns_buf); /* Set DO-GOTO-JOB routine access path */
273: stcpy(rou1path, ns_buf); /* Set ZLOAD-ZSAVE routine access path */
274:
275: /* clear routine buffer */
276: for (i = 0; i < NO_OF_RBUF; i++) {
277:
278: if (pgms[i][0] != '%') {
279:
280: if (rouptr != (buff + (i * PSIZE0))) {
281: pgms[i][0] = EOL;
282: ages[i] = 0L;
283: }
284:
285: path[i][0] = EOL;
286: }
287:
288: }
289: }
290:
291: /* clear private buffer */
292: for(i = 0; i < 256; i++) ns_buf[i] = NUL;
293:
294: /* set up regular globals */
295: if (get_conf (ns, "globals_path", ns_buf) != TRUE) {
296: if (verbose) {
297: ns_error (ns, "could not get globals_path");
298: }
299: else {
300: merr_raise (M26);
301: return;
302: }
303: }
304: else {
305: stcnv_c2m (ns_buf);
306: stcpy (glopath, ns_buf); /* Set globals path */
307:
308: /* close regular globals
309: for (i = 0; i < NO_GLOBLS; i++) {
310:
311: if (oldfil[i][0] != '%') {
312:
313: close (olddes[i]);
314:
315: usage[i] = 0;
316: olddes[i] = 0;
317: oldfil[i][0] = NUL;
318: }
319:
320: }
321: */
322: }
323:
324: strcpy (nsname, ns);
325:
326: if (verbose == TRUE) {
1.6 ! snw 327: snprintf (notif, sizeof (notif) - 1, "Namespace set to '%s'\r\n\201", ns);
1.1 snw 328: write_m (notif);
329: }
330:
331: }
332:
333: short validate_namespace (char *nsn_v)
334: {
335: char scratch[256];
336:
337: if (get_conf (nsn_v, "routines_path", scratch) == FALSE) return FALSE;
338: if (get_conf (nsn_v, "globals_path", scratch) == FALSE) return FALSE;
339:
340: return TRUE;
341:
342: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>