File:  [Coherent Logic Development] / freem / src / ssvn_zprocess.c
Revision 1.3: download - view: text, annotated - select for diffs
Sun Mar 9 19:50:47 2025 UTC (12 months, 3 weeks ago) by snw
Branches: MAIN
CVS tags: v0-62-1, v0-62-0, HEAD
Second phase of REUSE compliance and header reformat

    1: /*
    2:  *   $Id: ssvn_zprocess.c,v 1.3 2025/03/09 19:50:47 snw Exp $
    3:  *    ^$ZPROCESS ssvn
    4:  *
    5:  *  
    6:  *   Author: Serena Willis <snw@coherent-logic.com>
    7:  *    Copyright (C) 1998 MUG Deutschland
    8:  *    Copyright (C) 2020, 2025 Coherent Logic Development LLC
    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:  *
   26:  *   $Log: ssvn_zprocess.c,v $
   27:  *   Revision 1.3  2025/03/09 19:50:47  snw
   28:  *   Second phase of REUSE compliance and header reformat
   29:  *
   30:  *
   31:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
   32:  * SPDX-License-Identifier: AGPL-3.0-or-later
   33:  **/
   34: 
   35: #include <stdlib.h>
   36: #include <stdio.h>
   37: #include <string.h>
   38: #include <dirent.h>
   39: 
   40: #include "mpsdef.h"
   41: #include "mref.h"
   42: #include "iniconf.h"
   43: 
   44: void ssvn_zprocess (short action, char *key, char *data)
   45: {
   46:     freem_ref_t *ref = (freem_ref_t *) malloc (sizeof(freem_ref_t));
   47: 
   48:     char *kbuf = (char *) malloc (STRLEN * sizeof(char));
   49:     char *verb = (char *) malloc (STRLEN * sizeof(char));
   50:     char *proc_path = (char *) malloc (STRLEN * sizeof(char));
   51: 
   52:     pid_t t_pid;
   53: 
   54:     NULLPTRCHK(ref,"ssvn_zprocess");
   55:     NULLPTRCHK(kbuf,"ssvn_zprocess");
   56:     NULLPTRCHK(verb,"ssvn_zprocess");
   57:     NULLPTRCHK(proc_path,"ssvn_zprocess");
   58:     
   59:     mref_init (ref, MREF_RT_SSVN, "");
   60:     internal_to_mref (ref, key);
   61: 
   62:     if (ref->subscript_count < 2) {
   63: 
   64:         free (kbuf);
   65:         free (ref);
   66:         free (verb);
   67:         free (proc_path);
   68:         
   69:         merr_raise (INVREF);
   70:         return;
   71:     }
   72: 
   73:     t_pid = atol(ref->subscripts[0]);
   74:     snprintf (proc_path, STRLEN - 1, "/proc/%d", t_pid);
   75: 
   76:     strncpy (verb, ref->subscripts[1], STRLEN - 1);
   77: 
   78: 
   79:     stcpy (kbuf, key);
   80: 
   81:     switch (action) {
   82:     
   83:         case get_sym:
   84: 
   85:             if (strcmp (verb, "EXISTS") == 0) {
   86:                 
   87:                 if (kill (t_pid, 0) == 0) {
   88:                     snprintf (data, STRLEN - 1, "%d\201", 1);
   89:                 }
   90:                 else {
   91:                     snprintf (data, STRLEN - 1, "%d\201", 0);
   92:                 }
   93: 
   94:                 free (kbuf);
   95:                 free (ref);
   96:                 free (verb);
   97:                 free (proc_path);
   98:                 
   99:                 merr_raise (OK);
  100:                 return;
  101: 
  102:             }
  103:             else if (strcmp (verb, "ATTRIBUTE") == 0) {
  104: 
  105:                 char attrib[256];
  106:                 char fpath[4096];
  107:                 
  108:                 FILE *fp;
  109: 
  110:                 strncpy (attrib, ref->subscripts[2], 255);
  111: 
  112:                 snprintf (fpath, 4095, "/proc/%d/%s", t_pid, attrib);
  113: 
  114:                 if ((fp = fopen (fpath, "r")) == NULL) {
  115: 
  116:                     free (kbuf);
  117:                     free (ref);
  118:                     free (verb);
  119:                     free (proc_path);
  120:  
  121:                     merr_raise (INVREF);
  122:                     return;
  123:                 }
  124: 
  125:                 fgets (data, 256, fp);
  126:                 stcnv_c2m (data);
  127: 
  128:                 fclose (fp);
  129: 
  130:                 free (kbuf);
  131:                 free (ref);
  132:                 free (verb);
  133:                 free (proc_path);
  134: 
  135:                 
  136:                 merr_raise (OK);
  137:                 return;
  138: 
  139:             }            
  140: 
  141:             free (kbuf);            
  142:             free (ref);
  143:             free (verb);
  144:             free (proc_path);
  145: 
  146:             merr_raise (INVREF);
  147:             return;
  148: 
  149:         case set_sym:   
  150: 
  151:             if (strcmp (verb, "SIGNAL") == 0) {
  152: 
  153:                 int signum;
  154: 
  155:                 stcnv_m2c (data);
  156: 
  157:                 signum = atoi (data);
  158:                 kill (t_pid, signum);
  159: 
  160:                 stcnv_c2m (data);
  161: 
  162:                 free (kbuf);
  163:                 free (ref);
  164:                 free (verb);
  165:                 free (proc_path);
  166: 
  167:                 
  168:                 merr_raise (OK);
  169:                 return;
  170: 
  171:                 
  172:             }
  173:             else {
  174: 
  175:                 free (kbuf);
  176:                 free (ref);
  177:                 free (verb);
  178:                 free (proc_path);
  179: 
  180:                 merr_raise (M29);
  181:                 return;
  182:             }
  183: 
  184:         case killone:
  185:         case kill_sym:     
  186:         
  187:             kill (t_pid, 15);
  188: 
  189:             free (kbuf);
  190:             free (ref);
  191:             free (verb);
  192:             free (proc_path);
  193: 
  194:             merr_raise (OK);
  195:             return;
  196:         
  197:         case dat:
  198:         case fra_order:
  199:         case fra_query:
  200:         case bigquery:
  201:         case getnext:
  202:         case m_alias:
  203:         case zdata:
  204:             
  205:         default:
  206: 
  207:             free (kbuf);
  208:             free (ref);
  209:             free (verb);
  210:             free (proc_path);
  211:             
  212:             merr_raise (INVREF);
  213:             return;
  214: 
  215:     }
  216: 
  217:     free (kbuf);
  218:     free (ref);
  219:     free (verb);
  220:     free (proc_path);
  221:     
  222:     *data = EOL;
  223:     return;
  224: }

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