File:  [Coherent Logic Development] / freem / src / fma_locks.c
Revision 1.4: download - view: text, annotated - select for diffs
Sun Mar 9 19:14:25 2025 UTC (4 months, 3 weeks ago) by snw
Branches: MAIN
CVS tags: v0-63-1-rc1, v0-63-0-rc1, v0-63-0, v0-62-3, v0-62-2, v0-62-1, v0-62-0, HEAD
First phase of REUSE compliance and header reformat

    1: /*
    2:  *   $Id: fma_locks.c,v 1.4 2025/03/09 19:14:25 snw Exp $
    3:  *    fmadm - locks
    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: fma_locks.c,v $
   27:  *   Revision 1.4  2025/03/09 19:14:25  snw
   28:  *   First 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 "config.h"
   37: 
   38: #if defined(HAVE_GETOPT_H)
   39: # include <getopt.h>
   40: #endif
   41: 
   42: #include "fmadm.h"
   43: 
   44: #include <stdio.h>
   45: #include <sys/types.h>
   46: #include <unistd.h>
   47: #include <ctype.h>
   48: #include <sys/stat.h>
   49: #include <fcntl.h>
   50: #include "locktab.h"
   51: 
   52: int unlock (char *locktab, long pid);
   53: long int tell (int fd);
   54: 
   55: int fma_locks_list (int optc, char **opts)
   56: {
   57: 
   58:     printf ("\nFreeM Lock Table\n");
   59:     printf ("----------------\n\n");
   60: 
   61:     locktab_dump ();
   62:     
   63:     return 0;
   64: } /* fma_locks_list() */
   65: 
   66: int fma_locks_remove (int optc, char **opts)
   67: {
   68: 
   69:     int ct;
   70:     int c;
   71:     int option_index = 0;
   72:     pid_t t_pid = 0;
   73: 
   74: #if defined(HAVE_GETOPT_LONG)
   75: 
   76:     struct option long_options[] = {        
   77:         {"pid", required_argument, 0, 'p'},
   78:         {0, 0, 0, 0}
   79:     };
   80: 
   81: #endif
   82: 
   83: #if defined(HAVE_GETOPT_LONG)
   84: 
   85:     while(1) {
   86:         
   87:         c = getopt_long (optc, opts, "p:", long_options, &option_index);
   88: 
   89:         if (c == -1) break;
   90: 
   91:         switch (c) {
   92: 
   93:             case 'p':
   94:                 t_pid = atol (optarg);
   95:                 break;
   96: 
   97:         }
   98:     }
   99: 
  100: 
  101: 
  102: 
  103: #else
  104: 
  105: #endif
  106: 
  107:     if (t_pid == 0) {
  108:         fprintf (stderr, "fmadm:  must supply -p or --pid argument\n");
  109:         return 1;
  110:     }
  111: 
  112:     printf ("\nRemoving all LOCKs for PID %ld:\t", (long) t_pid);
  113: 
  114:     ct = unlock (fma_locktab, t_pid);
  115: 
  116:     if (ct >= 0) {
  117:         printf ("[%d MATCHES]\n\n", ct);
  118:     }
  119:     else {
  120:         printf ("[ERROR]\n\n");
  121:     }
  122: 
  123:     return ct >= 0 ? 0 : 1;
  124: 
  125: } /* fma_locks_remove() */
  126: 
  127: /* unLOCK all entries of pid */
  128: int unlock (char *locktab, long n_pid)           
  129: {
  130:     pid_t old_pid = pid;
  131:     pid = n_pid;
  132:     
  133: 
  134:     if (locktab_get_sem () == TRUE) {
  135: 
  136:         locktab_unlock_all ();
  137: 
  138:         pid = old_pid;
  139:         locktab_release_sem ();
  140: 
  141:         return 1;
  142:     }
  143:     else {
  144:         fprintf (stderr, "fmadm:  could not acquire LOCK table semaphore\n");
  145:         pid = old_pid;
  146:         return 0;
  147:     }
  148: 
  149:     pid = old_pid;
  150:     return 0;
  151: 
  152: } /* lock() */

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