Commit 5e43d60b authored by ROOL's avatar ROOL :robot:
Browse files

* remotedb library moved onto trunk.

Version 0.02. Tagged as 'remotedb-0_02'
| Copyright 1997 Acorn Computers Ltd
|
| Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|
Dir <Obey$Dir>
amu_machine all
| Copyright 1997 Acorn Computers Ltd
|
| Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|
Dir <Obey$Dir>
amu_machine clean
| Copyright 1997 Acorn Computers Ltd
|
| Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
|
Dir <Obey$Dir>
amu_machine export_hdrs
amu_machine export_libs
c/** gitlab-language=c linguist-language=c linguist-detectable=true
h/** gitlab-language=c linguist-language=c linguist-detectable=true
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# Copyright 1997 Acorn Computers Ltd
#
# Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Makefile for remotedb
#
# ***********************************
# *** C h a n g e L i s t ***
# ***********************************
# Date Name Description
# ---- ---- -----------
# 24 Apr 1997 RWB Created
#
#
# Paths
#
#
# Generic options:
#
MKDIR = cdir
AS = objasm
CC = cc
CMHG = cmhg
CP = copy
LD = link
RM = remove
WIPE = -wipe
CD = dir
LIBFILE = libfile
SOCKLIBZM = TCPIPLibs:o.socklibzm
SOCKLIB = TCPIPLibs:o.socklib
UNIXLIBZM = TCPIPLibs:o.unixlibzm
UNIXLIB = TCPIPLibs:o.unixlib
INETLIBZM = TCPIPLibs:o.inetlibzm
INETLIB = TCPIPLibs:o.inetlib
DEBUGLIB = <Lib$Dir>.debug
CFLAGS = -c -depend !Depend -Wap -zps0 ${INCLUDES} -fah -Fn ${DFLAGS}
CFLAGSZM = -c -depend !Depend -Wap -zps0 ${INCLUDES} -fah -Fn ${DFLAGS} -zM
CPFLAGS = ~cfr~v
WFLAGS = ~c~v
CCZM= ${CC} ${CFLAGSZM}
#
# Libraries
#
CLIB = CLIB:o.stubs
#
# Include files
#
INCLUDES = -ITCPIPLibs:,C:
DFLAGS = -DCOMPAT_INET4
# Program specific options:
#
COMPONENT = remote
COMPONENTZM = remotezm
LCOMPONENT = lremote
LCOMPONENTZM= lremotezm
OBJ = o.${COMPONENT}
OBJZM = z.${COMPONENT}
#
# Rule patterns
#
.SUFFIXES: .z .o .c .s
.c.o:; ${CC} ${CFLAGS} -o $@ $<
.c.z:; ${CCZM} -c -o $@ $<
#
# Build
#
all: ${COMPONENT} ${COMPONENTZM} ${LCOMPONENT} ${LCOMPONENTZM} local_dirs
@echo ${COMPONENT}: all complete
${COMPONENT}: ${OBJ}
${LD} -o ${COMPONENT} -aof ${OBJ} ${SOCKLIB} ${UNIXLIB} ${INETLIB}
${COMPONENTZM}: ${OBJZM}
${LD} -o ${COMPONENTZM} -aof ${OBJZM} ${SOCKLIBZM} ${UNIXLIBZM} ${INETLIBZM}
${LCOMPONENT}: ${OBJ}
${LIBFILE} -o ${LCOMPONENT} -c ${OBJ}
${LCOMPONENTZM}: ${OBJZM}
${LIBFILE} -o ${LCOMPONENTZM} -c ${OBJZM}
local_dirs:
${MKDIR} o
${MKDIR} z
clean:
${WIPE} o.${COMPONENT} ${WFLAGS}
${WIPE} z.${COMPONENT} ${WFLAGS}
${WIPE} ${COMPONENT} ${WFLAGS}
${WIPE} ${COMPONENTZM} ${WFLAGS}
${WIPE} ${LCOMPONENT} ${WFLAGS}
${WIPE} ${LCOMPONENTZM} ${WFLAGS}
@echo ${COMPONENT}: cleaned
export: export_${PHASE}
export_hdrs:
${MKDIR} ${DEBUGLIB}
${MKDIR} ${DEBUGLIB}.h
${CP} h.${COMPONENT} ${DEBUGLIB}.h.${COMPONENT} ${CPFLAGS}
@echo ${COMPONENT}: export complete (hdrs)
export_libs: ${COMPONENT} ${COMPONENTZM} ${LCOMPONENT} ${LCOMPONENTZM} local_dirs
${MKDIR} ${DEBUGLIB}
${MKDIR} ${DEBUGLIB}.o
${CP} ${COMPONENT} ${DEBUGLIB}.o.${COMPONENT} ${CPFLAGS}
${CP} ${COMPONENTZM} ${DEBUGLIB}.o.${COMPONENTZM} ${CPFLAGS}
${CP} ${LCOMPONENT} ${DEBUGLIB}.o.${LCOMPONENT} ${CPFLAGS}
${CP} ${LCOMPONENTZM} ${DEBUGLIB}.o.${LCOMPONENTZM} ${CPFLAGS}
#
# Dynamic dependencies:
/* (0.02)
*
* This file is automatically maintained by srccommit, do not edit manually.
*
*/
#define Module_MajorVersion_CMHG 0.02
#define Module_MinorVersion_CMHG
#define Module_Date_CMHG 05 Nov 1998
#define Module_MajorVersion "0.02"
#define Module_Version 2
#define Module_MinorVersion ""
#define Module_Date "05 Nov 1998"
/* Copyright 1997 Acorn Computers Ltd
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/************************************************************************/
/* Copyright 1996 Acorn Computers Ltd */
/* */
/* This material is the confidential trade secret and proprietary */
/* information of Acorn Computers. It may not be reproduced, used */
/* sold, or transferred to any third party without the prior written */
/* consent of Acorn Computers. All rights reserved. */
/* */
/************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include "sys/types.h"
#include "sys/socket.h"
#include "sys/time.h"
#include "netdb.h"
#include "netinet/in.h"
#include "socklib.h"
#define REMOTE_DEBUG
#include "remote.h"
#define REMOTE_DEBUG_MAX_LINE (10*1024)
static int poll=1;
static char *debug_line = NULL;
/*
* compare two strings caselessly
*/
static int
caseless_strcmp(char *a, char *b)
{
int d;
while (*a || *b)
{
d = tolower( *(a++) ) - tolower( *(b++) );
if (d) return d;
}
return 0;
}
/*
* check the input port for any data, if there is, check it for a newline
* and if terminated correctly, extract the line.
*/
int
debug_poll(debug_session *sess)
{
fd_set read_set;
struct timeval tv;
char *argv[20];
int argc = 0;
char *cp;
int more;
if (!sess)
return (0);
poll=0; /* don't poll during debug_printf */
do
{
more = 0;
FD_ZERO(&read_set);
FD_SET(sess->sock,&read_set);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (select(FD_SETSIZE,&read_set,NULL,NULL,&tv)>0)
{
if (FD_ISSET(sess->sock,&read_set))
{
if (recv(sess->sock, debug_line, REMOTE_DEBUG_MAX_LINE,MSG_PEEK)>0)
if ((cp=strchr(debug_line,'\n'))!=NULL)
if (recv(sess->sock, debug_line, (cp-debug_line+1),0)>0)
{
debug_line[cp-debug_line]='\0'; /* remove the new line char */
/*
* break down string
*/
cp = debug_line;
while (*cp != '\0')
{
while (*cp == ' ')
cp++;
if (*cp == '\0')
break;
argv[argc++] = cp;
if (argc>=20)
break;
while (*cp != '\0' && *cp != ' ')
cp++;
if (*cp == 0)
break;
*cp++ = '\0';
}
*cp++ = '\0';
/*
* what do we want to do
*/
if (argc == 0 || argv[0][0]=='#' || argv[0][0] == 0) /* ignore comments */
;
else if (caseless_strcmp(argv[0],"PRIORITY")==0)
{
if (argc>1)
{
sess->priority = atoi(argv[1]);
if (sess->priority>7)
sess->priority = 7;
debug_printf(sess,"(6) debug priority changed to %d\n",
sess->priority);
}
}
else if (sess->cmd_handler) /* callback cmd handler if registered */
{
char name[32];
int rc;
strncpy(name, argv[0], sizeof(name));
name[sizeof(name)-1] = 0;
rc = sess->cmd_handler(argc, argv, sess->cmd_handle);
debug_printf(sess,"(6) cmd '%s' returns %d\n", name, rc);
}
else
debug_printf(sess,"(6) unknown command '%s'\n",argv[0]);
more = 1; /* try for more input */
}
}
}
} while (more);
poll=1; /* return to polling during debug_printf */
return (0);
}
/*
* send stuff to the debug session
*/
void
debug_printf(debug_session *sess, char *format, ...)
{
va_list list;
va_start (list, format);
vsprintf(debug_line,format, list);
va_end(list);
debug_print_line(sess, debug_line);
}
void
debug_vprintf(debug_session *sess, const char *format, va_list list)
{
vsprintf(debug_line,format, list);
debug_print_line(sess, debug_line);
}
void
debug_dvprintf(debug_session *sess, int priority, const char *file, int line, const char *format, va_list list)
{
int n = 0;
if (priority != 0)
n += sprintf(debug_line, "(%d)", priority);
if (file)
n += sprintf(debug_line+n, " %s:%d: ", file, line);
vsprintf(debug_line+n, format, list);
debug_print_line(sess, debug_line);
}
void
debug_print_line(debug_session *sess, const char *debug_line)
{
unsigned char priority=7;
if (!sess)
return;
if (poll)
debug_poll(sess);
if (debug_line[0]=='(') /* get opening bracket */
if (strchr(debug_line,')')!=NULL) /* find closing bracket */
priority=atoi(debug_line+1);
if (priority<=sess->priority)
{
send(sess->sock, sess->info, strlen(sess->info), 0);
send(sess->sock, debug_line, strlen(debug_line), 0);
if (debug_line[strlen(debug_line)-1]!='\n')
send(sess->sock, "\n", 1, 0);
}
}
void
remote_debug_register_cmd_handler(debug_session *sess, remote_debug_cmd_handler fn, void *handle)
{
if (sess)
{
sess->cmd_handler = fn;
sess->cmd_handle = handle;
}
}
/*
* Reads environment variable Inet$DebugHost in the form valhalla:1448 to
* obtain IP address and port number. Opens a socket to this host and returns
* a structure to be used for subsequent debug calls. Debug session should be
* closed using the remote_debug_session_close() function
*/
void
remote_debug_open(char *info, debug_session **db_sess)
{
int rc;
int debug_socket=-1;
struct sockaddr_in sockaddr;
struct hostent *hostent;
debug_session *sess=NULL;
char host[40];
char varname[100];
char *port;
/* malloc to avoid problems in SVC mode */
if (debug_line == NULL)
debug_line = malloc(REMOTE_DEBUG_MAX_LINE);
memset(&sockaddr, 0, sizeof(sockaddr));
/*
* where do we want to connect to
*/
sprintf (varname, "Inet$DebugHost_%s", info);
if (getenv (varname) == NULL)
{
if (getenv("Inet$DebugHost")==NULL)
goto exit_gracefully;
strncpy(host,getenv("Inet$DebugHost"),sizeof(host));
}
else
strncpy(host,getenv(varname),sizeof(host));
host[sizeof(host)-1] = '\0';
/*
* find port
*/
if ((port=strchr(host,':'))==NULL)
goto exit_gracefully;
*(port++) = 0; /* terminate host name and leave pointer to port */
sockaddr.sin_port = htons(atoi(port));
if (sockaddr.sin_port==0)
goto exit_gracefully;
/*
* allocate a socket
*/
do
{
debug_socket = socket(AF_INET, SOCK_STREAM, 0);
} while (debug_socket == 0);
hostent = gethostbyname(host);
if (hostent == NULL)
goto exit_gracefully;
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = *(u_long*)hostent->h_addr;
rc = connect(debug_socket, (struct sockaddr *)&sockaddr, sizeof(sockaddr) );
if (rc<0)
goto exit_gracefully;
/*
* allocate a debug session
*/
if ((sess=malloc(sizeof(debug_session)))==NULL)
goto exit_gracefully;
sess->sock = debug_socket;
if ((sess->info=malloc(strlen(info)+/*2 - No, you need + 3 JBeranek */ 3))==NULL)
goto exit_gracefully;
strcpy(sess->info, info);
strcat(sess->info, ": ");
sess->priority = 7;
/*
* send initial opening message
*/
debug_printf(sess,"(6) Open Version %s",REMOTE_DEBUG_VERSION);
*db_sess = sess;
return;
exit_gracefully:
if (debug_socket >= 0)
socketclose(debug_socket);
if (sess)
{
if (sess->info)
free(sess->info);
free(sess);
}
*db_sess = NULL;
}
/*
* close previously opened debug session
*/
void
remote_debug_close(debug_session *sess)
{
if (sess)
{
debug_printf(sess,"(6) Close");
if (sess->sock >= 0)
socketclose(sess->sock);
if (sess->info)
free(sess->info);
free(sess);
}
if (debug_line)
{
free(debug_line);
debug_line = NULL;
}
return;
}
#!/usr/local/bin/perl
$banner='# Debug daemon 0.02';
# Internals:
# $conns[$n] - hash for connection
# fh File handle ref
# peer Hash ref of other end, or undef for internal
# inbuf Input buffer
# outbuf Output buffer
# modname Name of module if is debug end
# defspec Default module name, IP address
$usage="Usage: $0 [-r] [-d] [port [address]]\n";
$timeout= undef;
$rfdmask='';
$wfdmask='';
while($ARGV[0]=~/^-/) {
my $arg=shift;
if($arg eq '--') {
last;
} elsif($arg eq '-r') {
$cr=1;
} elsif($arg eq '-d') {
$debug={fh => \*STDERR};
@conns[0]=$debug;
vec($rfdmask,fileno($debug->{fh}),1)=1;
} else {
die$usage;
};
};
use Socket;
use Fcntl;
$|=1;
$sockaddr = 'S n a4 x8';
if($#ARGV==-1 && ($peer=getpeername(STDIN))) { # Check if we're on a socket
open(SM,"<& STDIN")||failed("dup(STDIN)");
setsockopt(SM,SOL_SOCKET,SO_REUSEADDR,1)||failed("setsockopt");
doaccept();
} else {
$standalone=1;
$port= shift || 1447;
$inetaddr= shift || '0.0.0.0';
@ARGV && die "usage: debugd [port [address]]";
($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
unless $port =~ /^\d+$/;
$this = pack($sockaddr, &AF_INET, $port, pack("C4",split(/\./,$inetaddr)));
socket(SM, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
setsockopt(SM,SOL_SOCKET,SO_REUSEADDR,1)||failed("setsockopt");
bind(SM, $this) || die("bind: $!");
listen(SM, 5) || die("listen: $!");
};
vec($rfdmask,fileno(SM),1)=1;
doprint($debug,"[Debug Daemon started $port]\n");
for (;;) {
$nfound=select($rfsd=$rfdmask,$wfds=$wfdmask,$efds='',undef);
# print "select returned $nfound\n" if($debug);
if($nfound==-1) {
failed("select");
next;
};
if(vec($rfsd,fileno(SM),1)) {
doaccept();
};
for($x=0;$x<=$#conns;$x++) {
my($hash)=$conns[$x];
# print "Conn $x, fileno ",fileno($hash->{fh}),"\n" if($debug);
if(vec($rfsd,fileno($hash->{fh}),1)) {
doprint($debug,"Read on conn $x\n");
$nfound--;
$res=sysread($hash->{fh},$buf,1024);
if(!$res) {
if(!defined($res)) {
doprint($hash->{peer},"# Error from read: $!\n");
doprint($debug,"# Error from read: $!\n");
};
disconnect($x);
} else {
$hash->{inbuf}.=$buf;
while($hash->{inbuf}=~s/^(.*)\n//) {
my $line=$1;
$line=~s/\r$//;
doprint($hash,":ack\n") if($hash->{version} >= 0.04);
doprint($debug,"Got line '$line', version $hash->{version}\n");
if($line=~/^([^:].*)$/||$line=~/^:(:.*)$/) {
$line=$1;
if(defined($hash->{peer})) {
doprint($hash->{peer},"$line\n");
} elsif(!defined($hash->{modname})) {
if($line=~
/^(\w+):\W+(\(\d+\)\W+)?open version\W+(\d+\.\d+)$/i) {
$hash->{version}=$3;
doprint($debug,"Open from module $1, version $3\n");
my $name=ipname(getpeername($hash->{fh})).":".$1;
$hash->{modname}=$name;
for($x=0;$x<=$#conns;$x++) {
my $phash=$conns[$x];
if((!defined($phash->{peer}) || !defined($hash->{peer})) &&
$name eq $phash->{defname}) {
$hash->{peer}=$phash;
};
};
if($hash->{peer}) {
if($hash->{peer}{peer}) {
doprint($hash->{peer}{peer},"# Connection stolen\n");
undef $hash->{peer}{peer}{peer};
};
$hash->{peer}{peer}=$hash;
doprint($hash->{peer},"# Connection from $name\n");
};
} elsif($line=~/^\d+$/) {
my $other=$hash->{others}[$line];
if(defined($other)) {
my $x;
$hash->{defname}=$other;
for($x=0;$x<=$#conns;$x++) {
my $phash=$conns[$x];
if(!defined($phash->{peer})
&& $other eq $phash->{modname}) {
$hash->{peer}=$phash;
$phash->{peer}=$hash;
last;
};
};
if(!defined($hash->{peer})) {
doprint($hash,"# Connection gone away\n");
};
} else {
doprint($hash,"# Number too large\n");
};
} else {
doprint($hash,"# Bad connect line\n");
};
};
} elsif($line=~/^:debug$/i) {
$debug=$hash;
} elsif($line=~/^:info$/i) {
my $c;
doprint($hash,"# Current connection: $x\n");
for($c=0;$c<=$#conns;$c++) {
my $chash=$conns[$c],$key;
doprint($hash,"#\n# Connection $c: $chash\n");
foreach $key (keys %$chash) {
doprint($hash,"# $key -> $chash->{$key}\n")
if($key ne 'outbuf');
if($key eq 'others') {
my $n;
for($n=0;$n<=$#{$chash->{others}};$n++) {
doprint($hash,"# $n. $chash->{others}[$n]\n");
};
} elsif($key eq 'outbuf' || $key eq 'inbuf') {
doprint($hash,"# $key length ".length($chash->{$key}).
"\n");
};
};
};
doprint($hash,"#\n# Ready.\n");
} elsif($line=~/^:list$/i) {
dolist($hash);
} elsif($line=~/^:quit$/i) {
if(defined($hash->{peer})) {
doprint($hash->{peer},"# quit\n");
};
disconnect($x);
} elsif($line=~/^:status$i/) {
if(defined($hash->{peer})) {
doprint($hash,"# Connected");
if(defined($hash->{peer}{modname})) {
doprint($hash," to module ".$hash->{peer}{modname}."\n");
} else {
doprint($hash," to unknown\n");
};
};
if(defined($hash->{defname})) {
doprint($hash,"# default module: '".$hash->{defname}."'\n");
};
doprint($hash,"# Ready.\n");
} else {
$line=~/^:([^ \t]*)/;
doprint($hash,"# Bad command: $1\n");
};
};
};
};
if(vec($wfds,fileno($hash->{fh}),1)) {
# print "Write on conn $x\n" if($debug);
$res=syswrite($hash->{fh},$hash->{outbuf},length($hash->{outbuf}));
if(!$res) {
if(!defined($res)) {
doprint($hash->{peer},"# Error from write: $!\n");
};
disconnect($x);
} else {
substr($hash->{outbuf},0,$res)='';
vec($wfdmask,fileno($hash->{fh}),1)=length($hash->{outbuf})?1:0;
};
};
};
}
sub ipname {
my (@sa) = unpack($sockaddr,$_[0]);
scalar(gethostbyaddr($sa[2],$sa[0]));
}
sub dolist {
my ($hash)=$_[0];
my $x,$others=[];
for($x=0;$x<=$#conns;$x++) {
my $chash=$conns[$x];
my $name=$chash->{modname};
if(defined($name)&&!defined($chash->{peer})) {
push@$others,$name;
doprint($hash,"# $#{$others}: $name\n");
};
};
doprint($hash,"# Ready.\n");
$hash->{others}=$others;
}
my $id=0;
sub doaccept {
my $sock=\*{S.$id++};
my $hashref,$x;
accept($sock,SM)||failed("accept");
fcntl($sock,F_SETFL,O_NONBLOCK)||warn("$0: fcntl(F_SETFL): $!\n");
$hashref={fh => $sock};
push@conns,$hashref;
doprint($debug,"Connect from ".ipname(getpeername($sock))."\n") if($debug);
vec($rfdmask,fileno($sock),1)=1;
doprint($hashref,"$banner\n");
dolist($hashref);
}
sub failed {
die"$0: $_[0] failed: $!\n";
}
sub disconnect {
my ($hash)=$conns[$_[0]];
if(defined($hash->{peer})) {
unless(defined($hash->{peer}{modname})) {
doprint($hash->{peer},"# Connection closed\n");
};
delete $hash->{peer}{peer};
};
doprint($debug,"Disconnecting session $_[0], $#conns left\n");
shutdown($hash->{fh},2)||doprint($debug,"shutdown: $!\n");
vec($rfdmask,fileno($hash->{fh}),1)=0;
vec($wfdmask,fileno($hash->{fh}),1)=0;
close($hash->{fh});
undef($hash->{fh});
undef $debug if($debug eq $hash);
if($_[0]==$#conns) {
pop@conns;
} else {
$conns[$_[0]]=pop@conns;
};
if($#conns==-1 && !$standalone) {
exit 0;
};
}
sub doprint {
my ($hash,$str)=@_;
if(defined($hash)) {
# print "Writing string $str\n" if($debug);
$hash->{outbuf}.=$str;
vec($wfdmask,fileno($hash->{fh}),1)=1;
};
}
# Local Variables:
# mode: perl
# perl-indent-level: 2
# End:
/* Copyright 1997 Acorn Computers Ltd
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/************************************************************************/
/* Copyright 1996 Acorn Computers Ltd */
/* */
/* This material is the confidential trade secret and proprietary */
/* information of Acorn Computers. It may not be reproduced, used */
/* sold, or transferred to any third party without the prior written */
/* consent of Acorn Computers. All rights reserved. */
/* */
/************************************************************************/
#ifndef __debug_remote_h
#define __debug_remote_h
#include <stdarg.h>
#define REMOTE_DEBUG_VERSION "0.03"
#ifdef REMOTE_DEBUG
/*
* Function typedef for handlign commands sent from the server end
*/
typedef int (*remote_debug_cmd_handler)(int argc, char *argv[], void *handle);
/*
* structure to fold session data
*/
typedef struct
{
int sock;
char *info;
unsigned char priority;
remote_debug_cmd_handler cmd_handler;
void *cmd_handle;
} debug_session;
/*
* Open a connection to host specified by Inet$DebugHost and send an initial
* connection message. All future sends via this interface will be prefixed by
* info. A copy of info is made internally. This call will return a pointer to
* a session structure to be used in subsequent calls.
*/
void remote_debug_open(char *info, debug_session **sess);
/*
* Send an printf formatted string upto a max of 1024 characters to the debug
* host. If string is prefixed by (n), this is treated as the debug priority
* level.
*
* Also versions to send a neat string and one passed as a va_list.
*/
void debug_printf(debug_session *sess, char *format, ...);
void debug_vprintf(debug_session *sess, const char *format, va_list list);
void debug_dvprintf(debug_session *sess, int priority, const char *file, int line, const char *format, va_list list);
void debug_print_line(debug_session *sess, const char *debug_line);
/*
* Register a command handler to handle commands sent from the server
*/
void remote_debug_register_cmd_handler(debug_session *sess, remote_debug_cmd_handler fn, void *handle);
/*
* Close a previously open debug channel.
*/
void remote_debug_close(debug_session *sess);
/*
* check the input port for any data. Call this every so often to ensure commands
* get executed when entered rather than on the next debug write.
*/
int debug_poll(debug_session *sess);
#else
#define remote_debug_open(x,y)
#define debug_printf 1?0:printf
#define debug_vprintf(s,f,l)
#define debug_print_line(s,d)
#define remote_debug_close(x)
#endif /* REMOTE_DEBUG */
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment