forked from ralphlange/procServ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
procServ.h
152 lines (120 loc) · 4.48 KB
/
procServ.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Process server for soft ioc
// David H. Thompson 8/29/2003
// Ralph Lange <[email protected]> 2007-2019
// Michael Davidsaver 2017
// GNU Public License (GPLv3) applies - see www.gnu.org
#ifndef procServH
#define procServH
#include <ostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <poll.h>
#include <assert.h>
#include <stdio.h>
#include <time.h>
/* whether to enable UNIX domain sockets */
#if defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__)
#include <sys/un.h>
# define USOCKS
#endif
#ifndef PRINTF
#define PRINTF if (inDebugMode) printf
#endif
#define PROCSERV_VERSION_STRING PACKAGE_STRING
enum RestartMode { restart, norestart, oneshot };
extern bool inDebugMode;
extern bool logPortLocal;
extern bool waitForManualStart;
extern volatile bool shutdownServer;
extern bool setCoreSize;
extern RestartMode restartMode;
extern bool firstRun;
extern char *procservName;
extern char *childName;
extern char *ignChars;
extern const char *timeFormat;
extern char killChar;
extern char toggleRestartChar;
extern char restartChar;
extern char quitChar;
extern char logoutChar;
extern int killSig;
extern const size_t INFO1LEN;
extern const size_t INFO2LEN;
extern const size_t INFO3LEN;
extern char infoMessage1[];
extern char infoMessage2[];
extern char infoMessage3[];
extern pid_t procservPid;
extern rlim_t coreSize;
extern char *chDir;
extern time_t holdoffTime;
#define NL "\r\n"
#define CTL_SC(c) c > 0 && c < 32 ? "^" : "", c > 0 && c < 32 ? c + 64 : c
class connectionItem;
extern time_t procServStart; // Time when this IOC started
extern time_t IOCStart; // Time when the current IOC was started
// Connection items call this to send messages to others
// This is a party line system, messages go to everyone
// the sender's this pointer keeps it from getting its own
// messages.
void SendToAll(const char * message,
int count,
const connectionItem * sender);
// Call this to add the item to the list of connections
void AddConnection(connectionItem *);
void DeleteConnection(connectionItem *ci);
// connectionItems are made in class factories so none of the
// constructors are public:
// processFactory creates the process that we are managing
connectionItem * processFactory(char *exe, char *argv[]);
bool processFactoryNeedsRestart(); // Call to test status of the server process
void processFactorySendSignal(int signal);
// clientFactory manages an open socket connected to a user
connectionItem * clientFactory(int ioSocket, bool readonly=false);
// acceptFactory opens a socket creating the inital listening
// service and calls clientFactory when clients are accepted
// local: restrict to localhost (127.0.0.1)
// readonly: discard any input from the client
connectionItem * acceptFactory( const char *spec, bool local=true, bool readonly=false );
extern connectionItem * processItem; // Set if it exists
// connectionItem class definition
// This is an abstract class that all of the other classes use
//
class connectionItem
{
public:
virtual ~connectionItem();
// Called from main() when input from this client is ready to be read.
virtual void readFromFd(void) = 0;
// Send characters to this client.
virtual int Send(const char * message, int count) = 0;
virtual int Send(const char * stamp, int stamp_len,
const char * message, int count) { return Send(message, count); }
virtual void markDeadIfChildIs(pid_t pid); // called if parent receives sig child
int getFd() const { return _fd; }
bool IsDead() const { return _markedForDeletion; }
// Return false unless you are the process item (processClass overloads)
virtual bool isProcess() const { return false; }
virtual bool isLogger() const { return _readonly; }
virtual void writeAddress(std::ostream& fp) {}
virtual void writeAddressEnv(std::ostringstream& env_var) {}
protected:
connectionItem ( int fd = -1, bool readonly = false );
int _fd; // File descriptor of this connection
bool _markedForDeletion; // True if this connection is dead
bool _readonly; // True if input has to be ignored
bool _log_stamp_sent; // Flag for timestamping log output
public:
connectionItem * next,*prev;
static connectionItem *head;
private:
// This should never happen
connectionItem(const connectionItem & item)
{
assert(0);
};
};
#endif /* #ifndef procServH */