Annotation of freem/src/namespace.c, revision 1.5

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>