2001-05-23 15:26:42 +02:00
|
|
|
/*
|
2007-02-11 10:04:54 +01: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): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
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 <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
|
|
|
|
|
2001-07-30 01:43:24 +02:00
|
|
|
#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");
|
2002-09-11 13:30:50 +02:00
|
|
|
#ifdef HAVE_SETPGRP
|
|
|
|
#ifdef SETPGRP_VOID
|
2009-01-03 10:14:29 +01:00
|
|
|
setpgrp();
|
2002-09-11 13:30:50 +02:00
|
|
|
#else
|
2009-01-03 10:14:29 +01:00
|
|
|
setpgrp(0, 0);
|
2009-05-17 13:20:43 +02:00
|
|
|
#endif // SETPGRP_VOID
|
2002-09-11 13:30:50 +02:00
|
|
|
#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
|
2007-02-11 10:04:54 +01:00
|
|
|
int fd;
|
2001-05-23 15:26:42 +02:00
|
|
|
if (argc < 2 || (!(fd = atoi(argv[1])) && strcmp(argv[1], "0")))
|
|
|
|
fd = -1;
|
|
|
|
|
2007-02-11 10:04:54 +01:00
|
|
|
TEXT** end = argv + argc;
|
2009-01-18 12:29:24 +01:00
|
|
|
while (argv < end)
|
|
|
|
{
|
2007-02-11 10:04:54 +01:00
|
|
|
TEXT* p = *argv++;
|
2001-05-23 15:26:42 +02:00
|
|
|
if (*p++ == '-')
|
2007-02-11 10:04:54 +01:00
|
|
|
{
|
|
|
|
TEXT c;
|
2001-05-23 15:26:42 +02:00
|
|
|
while (c = *p++)
|
2014-02-25 07:13:30 +01:00
|
|
|
{
|
2009-04-26 15:51:56 +02:00
|
|
|
switch (UPPER(c))
|
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
case 'Z':
|
2012-09-12 09:45:51 +02:00
|
|
|
printf("Firebird relay version %s\n", FB_VERSION);
|
2001-05-23 15:26:42 +02:00
|
|
|
exit(FINI_OK);
|
|
|
|
}
|
2014-02-25 07:13:30 +01:00
|
|
|
}
|
2007-02-11 10:04:54 +01:00
|
|
|
}
|
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
|
2007-02-11 10:04:54 +01:00
|
|
|
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);
|
2007-02-11 10:04:54 +01:00
|
|
|
}
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2007-02-11 10:04:54 +01: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
|
2014-02-25 07:13:30 +01:00
|
|
|
if (kill(msg[0], msg[1]))
|
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
#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
|
2004-11-07 11:52:36 +01:00
|
|
|
}
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(FINI_OK);
|
|
|
|
}
|