2001-05-23 15:26:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: UNIX resource removal program
|
2004-11-10 05:26:45 +01:00
|
|
|
* MODULE: drop.cpp
|
2001-05-23 15:26:42 +02:00
|
|
|
* DESCRIPTION: Drop shared memory and semaphores
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
2002-10-28 04:47:47 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 2002.10.27 Sean Leyne - Completed removal of obsolete "DELTA" port
|
2002-10-28 04:52:09 +01:00
|
|
|
* 2002.10.27 Sean Leyne - Completed removal of obsolete "IMP" port
|
2002-10-28 04:47:47 +01:00
|
|
|
*
|
2002-10-30 07:40:58 +01:00
|
|
|
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
|
|
|
|
*
|
2001-05-23 15:26:42 +02:00
|
|
|
*/
|
|
|
|
|
2001-07-30 01:43:24 +02:00
|
|
|
#include "firebird.h"
|
2004-04-29 00:36:29 +02:00
|
|
|
#include <stdio.h>
|
2001-05-23 15:26:42 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "../jrd/common.h"
|
|
|
|
#include "../jrd/isc.h"
|
2008-12-01 15:52:56 +01:00
|
|
|
#include "../jrd/isc_s_proto.h"
|
2001-07-30 01:43:24 +02:00
|
|
|
#include "../lock/lock_proto.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#include "../jrd/license.h"
|
|
|
|
#include "../jrd/gds_proto.h"
|
|
|
|
#include "../jrd/isc_proto.h"
|
2002-12-07 14:32:34 +01:00
|
|
|
#include "../common/config/config.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2001-07-30 01:43:24 +02:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-17 13:25:50 +02:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2002-09-18 14:50:13 +02:00
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
2001-07-30 01:43:24 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2008-01-16 08:40:12 +01:00
|
|
|
const int FTOK_KEY = 15;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2009-01-18 12:29:24 +01:00
|
|
|
static SLONG get_key(const TEXT*);
|
2008-01-16 08:40:12 +01:00
|
|
|
static void remove_resource(const TEXT*, SLONG, const TEXT*);
|
2003-08-10 00:32:39 +02:00
|
|
|
#ifndef HAVE_MMAP
|
2008-01-16 08:40:12 +01:00
|
|
|
static void dummy_init();
|
2001-05-23 15:26:42 +02:00
|
|
|
static int shm_exclusive(SLONG, SLONG);
|
2003-08-10 00:32:39 +02:00
|
|
|
#endif
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2009-05-17 13:20:43 +02:00
|
|
|
//static int orig_argc;
|
|
|
|
//static SCHAR **orig_argv;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
int CLIB_ROUTINE main( int argc, char *argv[])
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* m a i n
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
2008-10-21 08:20:33 +02:00
|
|
|
* Drop Lock Table and associated semaphores.
|
2001-05-23 15:26:42 +02:00
|
|
|
*
|
|
|
|
**************************************/
|
2003-08-26 08:56:42 +02:00
|
|
|
bool sw_lockmngr = false;
|
|
|
|
bool sw_events = false;
|
|
|
|
bool sw_version = false;
|
|
|
|
bool sw_shutmngr = false;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2009-05-17 13:20:43 +02:00
|
|
|
//orig_argc = argc;
|
|
|
|
//orig_argv = argv;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2004-11-10 05:26:45 +01:00
|
|
|
SCHAR** const end = argv + argc;
|
2001-05-23 15:26:42 +02:00
|
|
|
while (++argv < end)
|
|
|
|
if (**argv == '-')
|
2004-11-10 05:26:45 +01:00
|
|
|
for (const char* p = *argv + 1; *p; p++)
|
2009-01-18 12:29:24 +01:00
|
|
|
switch (UPPER(*p))
|
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
case 'E':
|
2003-08-26 08:56:42 +02:00
|
|
|
sw_events = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
2003-08-26 08:56:42 +02:00
|
|
|
sw_lockmngr = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
2003-08-26 08:56:42 +02:00
|
|
|
sw_events = sw_lockmngr = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
2008-10-21 08:20:33 +02:00
|
|
|
sw_shutmngr = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
2008-10-21 08:20:33 +02:00
|
|
|
//case 'N':
|
|
|
|
// break;
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
case 'Z':
|
2003-08-26 08:56:42 +02:00
|
|
|
sw_version = true;
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-04-29 00:36:29 +02:00
|
|
|
printf("***Ignoring unknown switch %c.\n", *p);
|
2001-05-23 15:26:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sw_version)
|
2004-04-29 00:36:29 +02:00
|
|
|
printf("gds_drop version %s\n", GDS_VERSION);
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
if (sw_events)
|
2008-01-16 08:40:12 +01:00
|
|
|
remove_resource(EVENT_FILE, Config::getEventMemSize(), "events");
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
if (sw_lockmngr)
|
2008-01-16 08:40:12 +01:00
|
|
|
remove_resource(LOCK_FILE, Config::getLockMemSize(), "lock manager");
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
exit(FINI_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-10 02:41:39 +02:00
|
|
|
#ifndef HAVE_MMAP
|
2008-01-16 08:40:12 +01:00
|
|
|
static void dummy_init()
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* d u m m y _ i n i t
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* A dummy callback routine for ISC_map_file.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
}
|
2003-08-10 02:41:39 +02:00
|
|
|
#endif
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2009-01-18 12:29:24 +01:00
|
|
|
static SLONG get_key(const TEXT* filename)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
/*************************************
|
|
|
|
*
|
|
|
|
* g e t _ k e y
|
|
|
|
*
|
|
|
|
*************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Find the semaphore/shared memory key for a file.
|
|
|
|
*
|
|
|
|
************************************/
|
|
|
|
TEXT expanded_filename[128], hostname[64];
|
|
|
|
|
|
|
|
#ifdef NOHOSTNAME
|
|
|
|
strcpy(expanded_filename, filename);
|
|
|
|
#else
|
2009-01-18 12:29:24 +01:00
|
|
|
sprintf(expanded_filename, filename, ISC_get_host(hostname, sizeof(hostname)));
|
2001-05-23 15:26:42 +02:00
|
|
|
#endif
|
|
|
|
|
2009-05-17 13:20:43 +02:00
|
|
|
// Produce shared memory key for file
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
return ftok(expanded_filename, FTOK_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-26 14:10:19 +02:00
|
|
|
#ifndef HAVE_MMAP
|
2009-01-18 12:29:24 +01:00
|
|
|
static void remove_resource(const TEXT* filename, SLONG shm_length, const TEXT* label)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* r e m o v e _ r e s o u r c e ( n o n - m m a p )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
2003-04-16 12:18:51 +02:00
|
|
|
ISC_STATUS_ARRAY status_vector;
|
2001-05-23 15:26:42 +02:00
|
|
|
SH_MEM_T shmem_data;
|
|
|
|
TEXT expanded_filename[MAXPATHLEN];
|
|
|
|
|
|
|
|
gds__prefix_lock(expanded_filename, filename);
|
|
|
|
|
|
|
|
if (!ISC_map_file
|
2007-11-12 16:18:49 +01:00
|
|
|
#ifdef HP11
|
|
|
|
(status_vector, expanded_filename,
|
2009-01-18 12:29:24 +01:00
|
|
|
(void (*) (void*, sh_mem*, bool)) dummy_init, 0, shm_length, &shmem_data))
|
2007-11-12 16:18:49 +01:00
|
|
|
#else
|
2009-01-18 12:29:24 +01:00
|
|
|
(status_vector, expanded_filename, dummy_init, 0, shm_length, &shmem_data))
|
2007-11-12 16:18:49 +01:00
|
|
|
#endif
|
2004-11-10 05:26:45 +01:00
|
|
|
{
|
2004-04-29 00:36:29 +02:00
|
|
|
printf("\n***Unable to access %s resources:\n", label);
|
2001-05-23 15:26:42 +02:00
|
|
|
gds__print_status(status_vector);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-10 05:26:45 +01:00
|
|
|
const SLONG key = get_key(expanded_filename);
|
2009-07-03 12:44:55 +02:00
|
|
|
if (key == -1)
|
|
|
|
{
|
2009-01-18 12:29:24 +01:00
|
|
|
printf("\n***Unable to get the key value of the %s file.\n", label);
|
2001-05-23 15:26:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-21 08:20:33 +02:00
|
|
|
SLONG shmid = shm_exclusive(key, shmem_data.sh_mem_length_mapped);
|
|
|
|
if (shmid == -1)
|
2004-03-28 11:10:30 +02:00
|
|
|
{
|
2008-01-16 08:40:12 +01:00
|
|
|
printf("\n***File for %s is currently in use.\n", label);
|
2001-05-23 15:26:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shmctl(shmid, IPC_RMID, 0) == -1)
|
2009-01-18 12:29:24 +01:00
|
|
|
printf("\n***Error trying to drop %s file. ERRNO = %d.\n", label, errno);
|
2001-05-23 15:26:42 +02:00
|
|
|
else
|
2004-04-29 00:36:29 +02:00
|
|
|
printf("Successfully removed %s file.\n", label);
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-08-26 14:10:19 +02:00
|
|
|
#ifdef HAVE_MMAP
|
2009-01-18 12:29:24 +01:00
|
|
|
static void remove_resource(const TEXT* filename, SLONG shm_length, const TEXT* label)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* r e m o v e _ r e s o u r c e ( m m a p )
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
TEXT expanded_filename[MAXPATHLEN];
|
|
|
|
|
|
|
|
gds__prefix_lock(expanded_filename, filename);
|
|
|
|
|
2004-11-10 05:26:45 +01:00
|
|
|
const SLONG key = get_key(expanded_filename);
|
|
|
|
if (key == -1) {
|
2008-10-21 08:20:33 +02:00
|
|
|
printf("\n***Unable to get the key value of the %s file.\n", label);
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-08-26 14:10:19 +02:00
|
|
|
#ifndef HAVE_MMAP
|
2001-05-23 15:26:42 +02:00
|
|
|
static int shm_exclusive( SLONG key, SLONG length)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* s h m _ e x c l u s i v e
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Check to see if we are the only ones accessing
|
|
|
|
* shared memory. Return a shared memory id
|
|
|
|
* if so, -1 otherwise.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
2004-11-10 05:26:45 +01:00
|
|
|
const int id = shmget(key, (int) length, IPC_ALLOC);
|
2008-03-05 09:39:26 +01:00
|
|
|
if (id == -1 || shmctl(id, IPC_STAT, &buf) == -1 || buf.shm_nattch != 1)
|
2004-11-10 05:26:45 +01:00
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
return -1;
|
2004-11-10 05:26:45 +01:00
|
|
|
}
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
#endif
|