Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion acceptFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ void acceptItem::readFromFd(void)
newFd = accept( _fd, &addr, &len );
if (newFd >= 0) {
PRINTF("acceptItem: Accepted connection on handle %d\n", newFd);
AddConnection(clientFactory(newFd, _readonly));
AddConnection(clientFactory(newFd, _readonly), true);
} else {
PRINTF("Accept error: %s\n", strerror(errno)); // on Cygwin got error EINVAL
remakeConnection();
Expand Down
47 changes: 38 additions & 9 deletions procServ.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include <string.h>
#include <string>

#include <boost/circular_buffer.hpp>

#ifdef __CYGWIN__
#include <windows.h>
Expand Down Expand Up @@ -95,6 +98,8 @@ int logFileFD=-1; // FD for log file
char *logPort; // address for logger connections
int debugFD=-1; // FD for debug output

boost::circular_buffer<std::string> logBuffer(0);

#define MAX_CONNECTIONS 64

// mLoop runs the program
Expand Down Expand Up @@ -174,6 +179,7 @@ void printHelp()
" -d --debug debug mode (keeps child in foreground)\n"
" -e --exec <str> specify child executable (default: arg0 of <command>)\n"
" -f --foreground keep child in foreground (interactive)\n"
" -H --history <n> display the last n child messages for new connections\n"
" -h --help print this message\n"
" --holdoff <n> set holdoff time [sec] between child restarts\n"
" -i --ignore <str> ignore all chars in <str> (^ for ctrl)\n"
Expand Down Expand Up @@ -236,7 +242,8 @@ int main(int argc,char * argv[])
{"exec", required_argument, 0, 'e'},
{"foreground", no_argument, 0, 'f'},
{"help", no_argument, 0, 'h'},
{"holdoff", required_argument, 0, 'H'},
{"history", required_argument, 0, 'H'},
{"holdoff", required_argument, 0, 'O'},
{"ignore", required_argument, 0, 'i'},
{"info-file", required_argument, 0, 'I'},
{"killcmd", required_argument, 0, 'k'},
Expand All @@ -261,7 +268,7 @@ int main(int argc,char * argv[])
/* getopt_long stores the option index here. */
int option_index = 0;

c = getopt_long (argc, argv, "+c:de:fhi:I:k:l:L:n:op:P:qVwx:",
c = getopt_long (argc, argv, "+c:de:fhH:i:I:k:l:L:n:op:P:qVwx:",
long_options, &option_index);

/* Detect the end of the options. */
Expand Down Expand Up @@ -309,12 +316,17 @@ int main(int argc,char * argv[])
if (optarg)
stampFormat = strdup(optarg);
break;

case 'H': // Size of history buffer
logBuffer.set_capacity( atoi( optarg ) );
break;


case 'h': // Help
printHelp();
exit(0);

case 'H': // Holdoff time
case 'O': // Holdoff time
k = atoi( optarg );
if ( k >= 0 ) holdoffTime = k;
break;
Expand Down Expand Up @@ -514,7 +526,7 @@ int main(int argc,char * argv[])
{
for(size_t i=0; i<ctlSpecs.size(); i++) {
connectionItem *acceptItem = acceptFactory( ctlSpecs[i].c_str(), ctlPortLocal );
AddConnection(acceptItem);
AddConnection(acceptItem, false);
}
}
catch (int error)
Expand All @@ -531,7 +543,7 @@ int main(int argc,char * argv[])
try
{
connectionItem *acceptItem = acceptFactory( logPort, logPortLocal, true );
AddConnection(acceptItem);
AddConnection(acceptItem, false);
}
catch (int error)
{
Expand Down Expand Up @@ -565,7 +577,7 @@ int main(int argc,char * argv[])

if (inFgMode && !(logFile && strcmp(logFile, "-")==0)) {
ttySetCharNoEcho(true);
AddConnection(clientFactory(0));
AddConnection(clientFactory(0), false);
}

// Record some useful data for managers
Expand Down Expand Up @@ -658,7 +670,7 @@ int main(int argc,char * argv[])
shutdownServer = true;
} else {
npi= processFactory(childExec, childArgv);
if (npi) AddConnection(npi);
if (npi) AddConnection(npi, false);
if (firstRun) {
firstRun = false;
}
Expand Down Expand Up @@ -718,6 +730,16 @@ void SendToAll(const char * message,
strftime(stamp, sizeof(stamp)-1, stampFormat, &now_tm);
len = strlen(stamp);

if (sender && sender->isProcess()) {
std::string s_message = "";
if (stampLog) {
s_message += stamp;
s_message += " ";
}
s_message += message;
logBuffer.push_back(s_message);
}

// Log the traffic to file / stdout (debug)
if (sender==NULL || sender->isProcess())
{
Expand All @@ -730,6 +752,7 @@ void SendToAll(const char * message,
for (i = 0; i < count; ++i) {
if (!log_stamp_sent) {
ignore_result( write(logFileFD, stamp, len) );
ignore_result( write(logFileFD, " ", 1) );
log_stamp_sent = true;
}
if (message[i] == '\n') {
Expand Down Expand Up @@ -758,6 +781,7 @@ void SendToAll(const char * message,
p->Send(stamp, len, message, count);
else
p->Send(message, count);

}
}
p = p->next;
Expand Down Expand Up @@ -815,7 +839,7 @@ void OnPollTimeout()
}

// Call this to add the item to the list of connections
void AddConnection(connectionItem * ci)
void AddConnection(connectionItem * ci, bool show_log)
{
PRINTF("Adding connection %p to list\n", ci);
if (connectionItem::head )
Expand All @@ -828,8 +852,13 @@ void AddConnection(connectionItem * ci)
ci->prev=NULL;
connectionItem::head=ci;
connectionNo++;
}

if (show_log) {
for (int i=0; i < logBuffer.size(); i++) {
ci->Send(logBuffer[i].c_str(), strlen(logBuffer[i].c_str()));
}
}
}

void DeleteConnection(connectionItem *ci)
{
Expand Down
2 changes: 1 addition & 1 deletion procServ.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void SendToAll(const char * message,
const connectionItem * sender);

// Call this to add the item to the list of connections
void AddConnection(connectionItem *);
void AddConnection(connectionItem *, bool show_log);
void DeleteConnection(connectionItem *ci);

// connectionItems are made in class factories so none of the
Expand Down
2 changes: 1 addition & 1 deletion procServ.spec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Release: 8%{?dist}
License: GPLv3
URL: https://github.com/ralphlange/procServ
Source0: https://github.com/ralphlange/procServ/releases/download/V%{version}/procServ-%{version}.tar.gz
BuildRequires: libtelnet-devel gcc-c++
BuildRequires: libtelnet-devel gcc-c++ boost-devel

%description
procServ is a wrapper that starts an arbitrary command as a child process in
Expand Down