8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-24 12:03:02 +01:00
firebird-mirror/src/utilities/relay.cpp

129 lines
2.8 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: UNIX signal relay program
* MODULE: relay.cpp
2001-05-23 15:26:42 +02:00
* DESCRIPTION: Signal relay program
*
* 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): ______________________________________.
*/
#include "firebird.h"
2004-04-29 00:36:29 +02:00
#include <stdio.h>
2001-05-23 15:26:42 +02:00
#include <sys/param.h>
#include <signal.h>
#include "../jrd/license.h"
2001-07-12 07:46:06 +02:00
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
2001-05-23 15:26:42 +02:00
int CLIB_ROUTINE main( int argc, char **argv)
{
/**************************************
*
* m a i n
*
**************************************
*
* Functional description
* Wait on a pipe for a message, then forward a signal.
*
**************************************/
#ifndef DEBUG
if (setreuid(0, 0) < 0)
2004-04-29 00:36:29 +02:00
printf("gds_relay: couldn't set uid to superuser\n");
#ifdef HAVE_SETPGRP
#ifdef SETPGRP_VOID
2009-01-03 10:14:29 +01:00
setpgrp();
#else
2009-01-03 10:14:29 +01:00
setpgrp(0, 0);
2009-05-17 13:20:43 +02:00
#endif // SETPGRP_VOID
#else
#ifdef HAVE_SETPGID
2009-01-03 10:14:29 +01:00
setpgid(0, 0);
2009-05-17 13:20:43 +02:00
#endif // HAVE_SETPGID
#endif // HAVE_SETPGRP
2001-05-23 15:26:42 +02:00
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
2009-05-17 13:20:43 +02:00
#endif // !DEBUG
2001-05-23 15:26:42 +02:00
2009-05-17 13:20:43 +02:00
// Get the file descriptor ID - if it is present - make sure it's valid
int fd;
2001-05-23 15:26:42 +02:00
if (argc < 2 || (!(fd = atoi(argv[1])) && strcmp(argv[1], "0")))
fd = -1;
TEXT** end = argv + argc;
2009-01-18 12:29:24 +01:00
while (argv < end)
{
TEXT* p = *argv++;
2001-05-23 15:26:42 +02:00
if (*p++ == '-')
{
TEXT c;
2001-05-23 15:26:42 +02:00
while (c = *p++)
2009-04-26 15:51:56 +02:00
switch (UPPER(c))
{
2001-05-23 15:26:42 +02:00
case 'Z':
printf("Firebird relay version %s\n", FB_VERSION);
2001-05-23 15:26:42 +02:00
exit(FINI_OK);
}
}
2001-05-23 15:26:42 +02:00
}
if (fd == -1)
exit(FINI_OK);
2009-05-17 13:20:43 +02:00
// Close all files, except for the pipe input
for (int n = 0; n < NOFILE; n++)
{
2001-05-23 15:26:42 +02:00
#ifdef DEV_BUILD
2009-05-17 13:20:43 +02:00
// Don't close stderr - we might need to report something
2001-05-23 15:26:42 +02:00
if ((n != fd) && (n != 2))
#else
if (n != fd)
#endif
close(n);
}
2001-05-23 15:26:42 +02:00
SLONG msg[3];
2009-01-03 10:14:29 +01:00
while (read(fd, msg, sizeof(msg)) == sizeof(msg))
{
2001-05-23 15:26:42 +02:00
#ifdef DEV_BUILD
2009-05-17 13:20:43 +02:00
// This is #ifdef for DEV_BUILD just in case a V3 client will
// attempt communication with this V4 version.
2001-05-23 15:26:42 +02:00
if (msg[2] != (msg[0] ^ msg[1])) {
2009-05-17 13:20:43 +02:00
fprintf(stderr, "gds_relay received inconsistent message");
2001-05-23 15:26:42 +02:00
}
#endif
if (kill(msg[0], msg[1])) {
#ifdef DEV_BUILD
2004-04-29 00:36:29 +02:00
fprintf(stderr, "gds_relay error on kill()");
2001-05-23 15:26:42 +02:00
#endif
}
2001-05-23 15:26:42 +02:00
}
exit(FINI_OK);
}