Remove fd/socket tracking needed to close files/sockets after lsh would exit

This commit is contained in:
Brendan Shanks 2017-08-03 23:53:15 -07:00
parent 67e2c69620
commit d301ab2c04
3 changed files with 1 additions and 209 deletions

View File

@ -12,7 +12,6 @@
#include "lsh.h"
#include "exception.h"
#define MAXFILESCOUNT 64
#define CONSOLEBUFSIZE 16384
#define ERROR_BUF_SIZE 512
@ -26,7 +25,6 @@ typedef struct lshcontext {
int _socket; // for cvs
int _rcmdline; // for cvs
jmp_buf *_pexitbuf;
int _filesTable[MAXFILESCOUNT];
struct termios _mactermios;
Boolean _gConsoleInEOF;

View File

@ -70,8 +70,6 @@ int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
int close(int s);
void ssh2_doevent(long sleepTime);
void add_one_file(struct lshcontext *context, int fd);
void remove_one_file(struct lshcontext *context, int fd);
#ifdef __cplusplus
}
@ -255,7 +253,7 @@ bool GUSISIOUXSocket::select(bool * canRead, bool * canWrite, bool *)
bool cond = false;
if (canRead) {
if (*canRead = (AvailableFromTTY(0, (lshcontext *)pthread_getspecific(ssh2threadkey))))
if (*canRead = (AvailableFromTTY(0, pthread_getspecific(ssh2threadkey))))
cond = true;
}
if (canWrite)
@ -428,154 +426,3 @@ pascal void GUSIOTNetDBNotify(
context->Wakeup();
}
*/
/*
* we need to track open()/dup()/close()/socket() calls to close files/sockets
* upon abort/exit
*/
/*
* open.
*/
int open(const char * path, int mode, ...)
{
GUSIErrorSaver saveError;
GUSIDeviceRegistry * factory = GUSIDeviceRegistry::Instance();
GUSIDescriptorTable * table = GUSIDescriptorTable::Instance();
GUSISocket * sock;
int fd;
if (sock = factory->open(path, mode)) {
if ((fd = table->InstallSocket(sock)) > -1) {
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if ( context ) {
add_one_file(context, fd);
}
return fd;
}
sock->close();
}
if (!errno)
return GUSISetPosixError(ENOMEM);
else
return -1;
}
/*
* dup.
*/
int dup(int s)
{
GUSIDescriptorTable *table = GUSIDescriptorTable::Instance();
GUSISocket *sock = GUSIDescriptorTable::LookupSocket(s);
int fd;
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if (!sock)
return -1;
fd = table->InstallSocket(sock);
if ( context ) {
add_one_file(context, fd);
}
return fd;
}
/*
* dup2.
*/
int dup2(int s, int s1)
{
GUSIDescriptorTable *table = GUSIDescriptorTable::Instance();
GUSISocket *sock = GUSIDescriptorTable::LookupSocket(s);
int fd;
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if (!sock)
return -1;
table->RemoveSocket(s1);
fd = table->InstallSocket(sock, s1);
if ( context && s1 != fd ) {
remove_one_file(context, s1);
add_one_file(context, fd);
}
return fd;
}
/*
* socket.
*/
int socket(int domain, int type, int protocol)
{
GUSIErrorSaver saveError;
GUSISocketFactory * factory = GUSISocketDomainRegistry::Instance();
GUSIDescriptorTable * table = GUSIDescriptorTable::Instance();
GUSISocket * sock;
int fd;
if (sock = factory->socket(domain, type, protocol)) {
if ((fd = table->InstallSocket(sock)) > -1) {
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if ( context ) {
add_one_file(context, fd);
}
return fd;
}
sock->close();
}
if (!errno)
return GUSISetPosixError(ENOMEM);
else
return -1;
}
/*
* accept.
*/
int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
GUSIDescriptorTable *table = GUSIDescriptorTable::Instance();
GUSISocket *sock= GUSIDescriptorTable::LookupSocket(s);
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if (!sock)
return -1;
if (sock = sock->accept(addr, addrlen)) {
if ((s = table->InstallSocket(sock)) != -1) {
if ( context ) {
add_one_file(context, s);
}
return s;
} else {
sock->close();
}
}
return -1;
}
/*
* close.
*/
int close(int s)
{
if ( s > STDERR_FILENO ) {
GUSIDescriptorTable * table = GUSIDescriptorTable::Instance();
lshcontext *context = (lshcontext *)pthread_getspecific(ssh2threadkey);
if ( context ) {
remove_one_file(context, s);
}
return table->RemoveSocket(s);
} else {
/* we don't close stdin/stdout/stderr */
return 0;
}
}

View File

@ -1028,58 +1028,6 @@ void ssh2_doevent(long sleepTime)
}
}
/*
* we need to track open()/close()/socket() calls to close files/sockets
* upon abort/exit
*/
void add_one_file(struct lshcontext *context, int fd)
{
int i;
if ( fd != -1 ) {
for (i = 0; i < MAXFILESCOUNT; i++) {
if (context->_filesTable[i] == -1) {
context->_filesTable[i] = fd;
break;
}
}
}
}
/*
* remove_one_file
*/
void remove_one_file(struct lshcontext *context, int fd)
{
int i;
if ( fd != -1 ) {
for (i = 0; i < MAXFILESCOUNT; i++) {
if (context->_filesTable[i] == fd) {
context->_filesTable[i] = -1;
break;
}
}
}
}
/*
* close_all_files
*/
void close_all_files(lshcontext *context)
{
int i;
for (i = 0; i < MAXFILESCOUNT; i++) {
if (context->_filesTable[i] != -1) {
close(context->_filesTable[i]);
}
}
}
#pragma mark -
/*
@ -1151,7 +1099,6 @@ void init_context(lshcontext *context, short port)
context->_listener = -1;
context->_socket = -1;
/*context->_exitbuf = 0;*/
memset(context->_filesTable, 0xff, sizeof(context->_filesTable));
memcpy(&context->_mactermios, &defaulttermios, sizeof(struct termios));
context->_gConsoleInEOF = 0;
/*context->_convertLFs = 0;*/