/* Unix SMB/Netbios implementation. Version 1.9. Shared memory functions Copyright (C) Erik Devriendt 1996 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef FAST_SHARE_MODES #include "includes.h" extern int DEBUGLEVEL; #define SHM_MAGIC 0x53484100 /* = "SHM" in hex */ #define SHM_VERSION 1 /* WARNING : offsets are used because mmap() does not guarantee that all processes have the shared memory mapped to the same address */ struct ShmHeader { int shm_magic; int shm_version; int total_size; /* in bytes */ BOOL consistent; shm_offset_t first_free_off; shm_offset_t userdef_off; /* a userdefined offset. can be used to store root of tree or list */ struct { /* a cell is a range of bytes of sizeof(struct ShmBlockDesc) size */ int cells_free; int cells_used; int cells_system; /* number of cells used as allocated block descriptors */ } statistics; }; #define SHM_NOT_FREE_OFF (-1) struct ShmBlockDesc { shm_offset_t next; /* offset of next block in the free list or SHM_NOT_FREE_OFF when block in use */ int size; /* user size in BlockDescSize units */ }; #define EOList_Addr (struct ShmBlockDesc *)( 0 ) #define EOList_Off (NULL_OFFSET) #define CellSize sizeof(struct ShmBlockDesc) /* HeaderSize aligned on 8 byte boundary */ #define AlignedHeaderSize ((sizeof(struct ShmHeader)+7) & ~7) static int shm_fd = -1; static pstring shm_processreg_name = ""; static struct ShmHeader *shm_header_p = (struct ShmHeader *)0; static int shm_times_locked = 0; static BOOL shm_register_process(char *processreg_file, pid_t pid, BOOL *other_processes) { int old_umask; int shm_processes_fd = -1; int nb_read; pid_t other_pid; int free_slot = -1; int erased_slot; old_umask = umask(0); shm_processes_fd = open(processreg_file, O_RDWR | O_CREAT, 0666); umask(old_umask); if ( shm_processes_fd < 0 ) { DEBUG(0,("ERROR shm_register_process : processreg_file open failed with code %d\n",errno)); return False; } *other_processes = False; while ((nb_read = read(shm_processes_fd, &other_pid, sizeof(other_pid))) > 0) { if(other_pid) { if(process_exists(other_pid)) *other_processes = True; else { /* erase old pid */ DEBUG(2,("shm_register_process : erasing stale record for pid %d\n",other_pid)); other_pid = (pid_t)0; erased_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR); write(shm_processes_fd, &other_pid, sizeof(other_pid)); if(free_slot < 0) free_slot = erased_slot; } } else if(free_slot < 0) free_slot = lseek(shm_processes_fd, -sizeof(other_pid), SEEK_CUR); } if (nb_read < 0) { DEBUG(0,("ERROR shm_register_process : processreg_file read failed with code %d\n",errno)); close(shm_processes_fd); return False; } if(free_slot < 0) free_slot = lseek(shm_processes_fd, 0, SEEK_END); DEBUG(2,("shm_register_process : writing record for pid %d at offset %d\n",pid,free_slot)); lseek(shm_processes_fd, free_slot, SEEK_SET); if(write(shm_processes_fd, &pid, sizeof(pid)) < 0) { DEBUG(0,("ERROR shm_register_process : processreg_file write failed with code %d\n",errno)); close(shm_processes_fd); return False; } close(shm_processes_fd); return True; } static BOOL shm_unregister_process(char *processreg_file, pid_t pid#!/bin/sh # options '-D' and '-c cmd' are mutually exclusive # #-D Run in background. This requires '-o' option. #-c cmd Command 'cmd' will be run and staprun will # exit when it does. The '_stp_target' variable # will contain the pid for the command. staprun -c /bin/ls -D fake_module