Skip to content

Commit d8140bd

Browse files
committed
A whole bunch of cool changes, including a helper class for gunits tests, global synchronized virtual clocks, XDR watchdog support, and more
1 parent aac1fe3 commit d8140bd

26 files changed

+22982
-112
lines changed

Diff for: Makefile

+28-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ include Make.rules.$(PLAT)
66
include Make.rules.arm
77

88
# Input/Output Variables
9-
SOURCES=priorityQueue.c events.c proclib.c ipc.c debug.c cmd.c config.c hashtable.c util.c md5.c critical.c eventTimer.c telm_dict.c zmqlite.c json.c cmd-pkt.c xdr.c plugin.c pseudo_threads.c
9+
SOURCES=priorityQueue.c events.c proclib.c ipc.c debug.c cmd.c config.c hashtable.c util.c md5.c critical.c eventTimer.c telm_dict.c zmqlite.c json.c cmd-pkt.c xdr.c plugin.c pseudo_threads.c globalTimer.c
10+
TEST_SOURCES=proctest.cpp
11+
1012
LIBRARY_NAME=proc
13+
TEST_LIBRARY_NAME=proctest
1114
MAJOR_VERS=3
1215
MINOR_VERS=0.4
1316

1417
# Install Variables
15-
INCLUDE=proclib.h events.h ipc.h config.h debug.h cmd.h polysat.h hashtable.h util.h md5.h priorityQueue.h eventTimer.h telm_dict.h zmqlite.h critical.h xdr.h cmd-pkt.h plugin.h pseudo_threads.h
18+
INCLUDE=proclib.h events.h ipc.h config.h debug.h cmd.h polysat.h hashtable.h util.h md5.h priorityQueue.h eventTimer.h telm_dict.h zmqlite.h critical.h xdr.h cmd-pkt.h plugin.h pseudo_threads.h proctest.h json.hpp zhelpers.hpp
1619

1720
# Build Variables
1821
override CFLAGS+=$(SYMBOLS) -Wall -Werror -Wno-unknown-warning-option -Wno-format-truncation -Wno-deprecated-declarations -std=gnu99 -D_GNU_SOURCE -D_FORTIFY_SOURCE=2 $(SO_CFLAGS)
22+
override CXXFLAGS+=$(SYMBOLS) -Wall -Werror -Wno-format-truncation -Wno-deprecated-declarations -D_GNU_SOURCE -D_FORTIFY_SOURCE=2 $(SO_CFLAGS)
1923
override LDFLAGS+= -ldl
2024
SRC_PATH=.
2125

@@ -26,20 +30,34 @@ SO_NAME=$(LIBRARY).$(SO_EXT).$(MAJOR_VERS)
2630
LIB_NAME=$(LIBRARY).$(SO_EXT).$(MAJOR_VERS).$(MINOR_VERS)
2731
LFLAGS=-shared -fPIC -Wl,-soname,$(SO_NAME)
2832

29-
all: $(OBJECTS) $(LIBRARY)
33+
TEST_OBJECTS=$(TEST_SOURCES:.cpp=.o)
34+
TEST_LIBRARY=lib$(TEST_LIBRARY_NAME)
35+
TEST_SO_NAME=$(TEST_LIBRARY).$(SO_EXT).$(MAJOR_VERS)
36+
TEST_LIB_NAME=$(TEST_LIBRARY).$(SO_EXT).$(MAJOR_VERS).$(MINOR_VERS)
37+
TEST_SO_LDFLAGS=-shared -fPIC -Wl,-soname,$(TEST_SO_NAME)
38+
39+
all: $(OBJECTS) $(LIBRARY) $(TEST_OBJECTS) $(TEST_LIBRARY)
3040

3141
$(LIBRARY): $(LIB_NAME)
3242
ln -sf $(LIB_NAME) $(LIBRARY).$(SO_EXT)
3343
ln -sf $(LIB_NAME) $(LIBRARY).$(SO_EXT).$(MAJOR_VERS)
3444

45+
$(TEST_LIBRARY): $(TEST_LIB_NAME)
46+
ln -sf $(TEST_LIB_NAME) $(TEST_LIBRARY).$(SO_EXT)
47+
ln -sf $(TEST_LIB_NAME) $(TEST_LIBRARY).$(SO_EXT).$(MAJOR_VERS)
48+
3549
$(LIB_NAME): $(OBJECTS)
3650
$(CC) $(SO_LDFLAGS) $(OBJECTS) $(LDFLAGS) -lpthread -o $(LIB_NAME)
3751

52+
$(TEST_LIB_NAME): $(TEST_OBJECTS)
53+
$(CXX) $(TEST_SO_LDFLAGS) $(TEST_OBJECTS) $(LDFLAGS) -lzmq -lproc -lpthread -o $(TEST_LIB_NAME)
54+
3855
cleanup_test: cleanup_test.c util.c priorityQueue.c debug.c
3956
$(CC) $(CFLAGS) cleanup_test.c util.c priorityQueue.c debug.c $(LDFLAGS) -o $@
4057

4158
cmd.c: cmd-pkt.h
4259
ipc.c: cmd-pkt.h
60+
proclib.c: cmd-pkt.h
4361

4462
cmd-pkt.h: cmd-pkt.xp
4563
$(XDRGEN) --target libproc --output cmd-pkt cmd-pkt.xp
@@ -53,6 +71,10 @@ install: all
5371
$(INST_STRIP) $(LIB_PATH)/$(LIB_NAME)
5472
ln -sf $(LIB_NAME) $(LIB_PATH)/$(LIBRARY).$(SO_EXT)
5573
ln -sf $(LIB_NAME) $(LIB_PATH)/$(LIBRARY).$(SO_EXT).$(MAJOR_VERS)
74+
cp $(TEST_LIB_NAME) $(LIB_PATH)
75+
$(INST_STRIP) $(LIB_PATH)/$(TEST_LIB_NAME)
76+
ln -sf $(TEST_LIB_NAME) $(LIB_PATH)/$(TEST_LIBRARY).$(SO_EXT)
77+
ln -sf $(TEST_LIB_NAME) $(LIB_PATH)/$(TEST_LIBRARY).$(SO_EXT).$(MAJOR_VERS)
5678
install -d $(INC_PATH)/polysat
5779
cp $(INCLUDE) $(INC_PATH)/polysat
5880
# ldconfig -linux-ld -n $(LIB_PATH)
@@ -68,5 +90,8 @@ test:
6890
.c.o:
6991
$(CC) $(CFLAGS) -c $(SRC_PATH)/$< -o $@
7092

93+
.cpp.o:
94+
$(CXX) $(CXXFLAGS) -c $(SRC_PATH)/$< -o $@
95+
7196
clean:
7297
rm -rf *.o $(LIBRARY).$(SO_EXT)* $(LIBRARY) cleanup_test cmd-pkt.c cmd-pkt.h

Diff for: cmd-pkt.xp

+36-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const ERR_BASE = 0x02000100;
77
enum Cmds {
88
RESPONSE = CMD_BASE + 0,
99
STATUS = CMD_BASE + 1,
10-
DATA_REQ = CMD_BASE + 2
10+
DATA_REQ = CMD_BASE + 2,
11+
WD_REGISTER_STATIC = CMD_BASE + 3,
12+
WD_REG_INFO = CMD_BASE + 4,
1113
};
1214

1315
enum types {
@@ -20,6 +22,8 @@ enum types {
2022
RESPONSE_HDR = TYPE_BASE + 6,
2123
HEARTBEAT = TYPE_BASE + 7,
2224
POPULATOR_ERROR = TYPE_BASE + 8,
25+
WD_PROC_NAME = TYPE_BASE + 9,
26+
WD_REG_INFO = TYPE_BASE + 10,
2327
};
2428

2529
command "proc-status" {
@@ -70,17 +74,47 @@ struct Heartbeat {
7074
};
7175
} = types::HEARTBEAT;
7276

77+
struct WDProcName {
78+
string name<128> {
79+
key name;
80+
name "Name";
81+
description "The name of the process to query details about";
82+
};
83+
} = types::WD_PROC_NAME;
84+
85+
struct WDRegInfo {
86+
string name<128> {
87+
key name;
88+
name "Name";
89+
description "The name of the process being watched";
90+
};
91+
92+
unsigned int pid {
93+
key pid;
94+
name "Process ID";
95+
description "The system's process id for the process being watched";
96+
};
97+
98+
bool watched {
99+
key watched;
100+
name "Watched";
101+
description "Flag indicating that the process is being watched or not";
102+
};
103+
} = types::WD_REG_INFO;
104+
73105
enum ResultCode {
74106
SUCCESS = ERR_BASE + 0,
75107
INCORRECT_PARAMETER_TYPE = ERR_BASE + 1,
76108
UNSUPPORTED = ERR_BASE + 2,
77-
ALLOCATION_ERR = ERR_BASE + 3
109+
ALLOCATION_ERR = ERR_BASE + 3,
110+
NO_SUCH_PROCESS = ERR_BASE + 4,
78111
};
79112

80113
error ResultCode::SUCCESS = "No error - success";
81114
error ResultCode::INCORRECT_PARAMETER_TYPE = "Type of command parameter didn't match the expected type";
82115
error ResultCode::UNSUPPORTED = "The target process does not support the command sent";
83116
error ResultCode::ALLOCATION_ERR = "Failed to allocate heap memory";
117+
error ResultCode::NO_SUCH_PROCESS = "The requested process can not be found";
84118

85119
struct DataReq {
86120
int length;

Diff for: cmd.c

+13-8
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,13 @@ int cmd_handler_init(const char * procName, struct ProcessData *proc,
468468
sprintf(cfgFile, "./%s.cmd.cfg", procName);
469469

470470
// Open the configuration file with the commands
471-
if (!CFG_locateConfigFile(cfgFile)) {
471+
if (!CFG_locateConfigFile(cfgFile))
472472
DBG_print(DBG_LEVEL_WARN, "No command configuration file found\n");
473-
cmds->cmds = NULL;
474-
return EXIT_SUCCESS;
473+
else {
474+
// Parse the commands
475+
root = (struct CFG_Root*) CFG_parseFile(&CFG_OBJNAME(Root));
476+
DBG_print(DBG_LEVEL_INFO, "%s found config file\n", procName);
475477
}
476-
// Parse the commands
477-
root = (struct CFG_Root*) CFG_parseFile(&CFG_OBJNAME(Root));
478-
479-
DBG_print(DBG_LEVEL_INFO, "%s found config file\n", procName);
480478
}
481479

482480
cmds->cmds = (struct Command*)malloc(sizeof(struct Command)*MAX_NUM_CMDS);
@@ -517,6 +515,11 @@ int cmd_handler_init(const char * procName, struct ProcessData *proc,
517515
return EXIT_SUCCESS;
518516
}
519517

518+
int CMD_pending_responses(struct CommandCbArg *cmds)
519+
{
520+
return cmds->resp != NULL;
521+
}
522+
520523
static void cmd_handle_xdr_response(ProcessData *proc,
521524
char *data, size_t dataLen, struct sockaddr_in *src)
522525
{
@@ -1165,8 +1168,10 @@ void CMD_set_xdr_cmd_handler(uint32_t num, CMD_XDR_handler_t cb, void *arg)
11651168
struct CMD_XDRCommandInfo *cmd;
11661169

11671170
cmd = CMD_xdr_cmd_by_number(num);
1168-
if (!cmd)
1171+
if (!cmd) {
1172+
DBG_print(DBG_LEVEL_WARN, "Missing XDR description for command %u, will not respond to command", num);
11691173
return;
1174+
}
11701175

11711176
cmd->handler = cb;
11721177
cmd->arg = arg;

Diff for: cmd.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ enum XDR_PRINT_STYLE { XDR_PRINT_HUMAN, XDR_PRINT_KVP, XDR_PRINT_CSV_HEADER,
147147
#endif
148148
#endif
149149

150+
extern int CMD_pending_responses(struct CommandCbArg *cmd);
150151
extern void CMD_register_commands(struct CMD_XDRCommandInfo*, int);
151152
extern void CMD_register_command(struct CMD_XDRCommandInfo*, int);
152153
extern void CMD_set_xdr_cmd_handler(uint32_t num, CMD_XDR_handler_t cb,

Diff for: config.c

+32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <fcntl.h>
3232
#include "config.h"
3333
#include "debug.h"
34+
#include <sys/types.h>
35+
#include <sys/socket.h>
36+
#include <netdb.h>
3437

3538
struct CFG_buffered_obj
3639
{
@@ -585,6 +588,35 @@ void *CFG_InetAtonCB(const char *key, const char *value, void *data, void *p1,
585588
return data;
586589
}
587590

591+
void *CFG_InetDNSCB(const char *key, const char *value, void *data, void *p1,
592+
void *p2)
593+
{
594+
void *dest_v = data + (p1 - ((void*)0));
595+
struct in_addr *dest = (struct in_addr*)dest_v;
596+
struct addrinfo hint;
597+
struct addrinfo *res = NULL, *curr;
598+
599+
memset(&hint, 0, sizeof(hint));
600+
hint.ai_family = AF_INET;
601+
602+
if (!getaddrinfo(value, NULL, &hint, &res)) {
603+
for (curr = res; curr; curr = curr->ai_next) {
604+
if (curr->ai_family == AF_INET) {
605+
*dest = ((struct sockaddr_in*)curr->ai_addr)->sin_addr;
606+
break;
607+
}
608+
}
609+
610+
if (res)
611+
freeaddrinfo(res);
612+
}
613+
else
614+
DBG_print(DBG_LEVEL_WARN, "Failed to resolve hostname %s: %s", value,
615+
strerror(errno));
616+
617+
return data;
618+
}
619+
588620
void *CFG_PtrCpyCB(const char *key, const char *value, void *data, void *p1,
589621
void *p2)
590622
{

Diff for: config.h

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ void *CFG_StrdupCB(const char *key, const char *value, void *data, void *p1,
7171
void *p2);
7272
void *CFG_InetAtonCB(const char *key, const char *value, void *data, void *p1,
7373
void *p2);
74+
void *CFG_InetDNSCB(const char *key, const char *value, void *data, void *p1,
75+
void *p2);
7476
void *CFG_uint32_CB(const char *key, const char *value, void *data, void *p1,
7577
void *p2);
7678
void *CFG_uint16_CB(const char *key, const char *value, void *data, void *p1,
@@ -103,10 +105,13 @@ void *CFG_PtrArrayAppendCB(const char *key, const char *value, void *data,
103105
#define CFG_INT16(n, str, field) { n, NULL, NULL, { &CFG_int16_CB, (void*)offsetof(str,field), NULL } }
104106
#define CFG_FLOAT(n, str, field) { n, NULL, NULL, { &CFG_float_CB, (void*)offsetof(str,field), NULL } }
105107
#define CFG_INET_ATON(n, str, field) { n, NULL, NULL, { &CFG_InetAtonCB, (void*)offsetof(str,field), NULL } }
108+
#define CFG_INET_DNS(n, str, field) { n, NULL, NULL, { &CFG_InetDNSCB, (void*)offsetof(str,field), NULL } }
106109

107110
#define CFG_OBJNAME(x) CFG__##x##__obj
108111
#define CFG_VALNAME(x) CFG__##x##__keys
109112
#define CFG_SUBNAME(x) CFG__##x##__subtypes
113+
#define CFG_VALNAME_CPP(c,x) CFG__##c##_##x##__keys
114+
110115
#define CFG_NEWOBJ_GBL(x, ini, fin, ...) static struct CFG_ParseValue CFG_VALNAME(x)[] = { __VA_ARGS__, CFG_NULLK }; \
111116
struct CFG_ParseObj CFG_OBJNAME(x) = { "", ini, fin, CFG_VALNAME(x) };
112117
#define CFG_NEWOBJ(x, ini, fin, ...) static struct CFG_ParseValue CFG_VALNAME(x)[] = { __VA_ARGS__, CFG_NULLK }; \
@@ -117,6 +122,11 @@ struct CFG_ParseObj CFG_OBJNAME(x) = { name, ini, fin, CFG_VALNAME(x) };
117122
#define CFG_EMPTYOBJ(x, ini, fin) static struct CFG_ParseValue CFG_VALNAME(x)[] = { CFG_NULLK }; \
118123
static struct CFG_ParseObj CFG_OBJNAME(x) = { "", ini, fin, CFG_VALNAME(x) };
119124

125+
#define CFG_OBJ_CPPDECL(x) static struct CFG_ParseObj x
126+
#define CFG_NEWOBJ_CPP(c, x, ini, fin, ...) static struct CFG_ParseValue CFG_VALNAME_CPP(x,c)[] = { __VA_ARGS__, CFG_NULLK }; \
127+
struct CFG_ParseObj c::x = { "", ini, fin, CFG_VALNAME_CPP(x,c) };
128+
#define CFG_NEWSUBOBJ_CPP(c, x, name, ini, fin, ...) static struct CFG_ParseValue CFG_VALNAME_CPP(c,x)[] = { __VA_ARGS__, CFG_NULLK }; \
129+
struct CFG_ParseObj c::x = { name, ini, fin, CFG_VALNAME_CPP(c,x) };
120130
/**
121131
* \brief Get the path of the configuration file.
122132
* \returns The full POSIX path of the current configuration file. If there is

Diff for: debug.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,38 @@
2929
#include <stdio.h>
3030
#include <string.h>
3131
#include "debug.h"
32+
#include "eventTimer.h"
3233

3334
/// Message buffer length
3435
#define MESSAGE_BUFF_LENGTH 1024
3536

3637
/// Default level of debug message printing
3738
static int gDBGLevel = DBG_LEVEL_WARN;
39+
static struct EventTimer *gDBGTimer = NULL;
3840

3941
// Debug output function
4042
void DBG_print(int level, const char *fmt, ...)
4143
{
4244
va_list ap;
45+
char buff[MESSAGE_BUFF_LENGTH];
46+
struct timeval now;
4347

4448
if (level > gDBGLevel) {
4549
return;
4650
}
4751

4852
// Read in the variable arguments and print the message to the log
4953
va_start(ap, fmt);
50-
vsyslog(level, fmt, ap);
54+
if (gDBGTimer) {
55+
vsnprintf(&buff[0], sizeof(buff), fmt, ap);
56+
buff[sizeof(buff)-1] = 0;
57+
58+
gDBGTimer->get_gmt_time(gDBGTimer, &now);
59+
syslog(level, "%lu.%06lu: %s", now.tv_sec, now.tv_usec, buff);
60+
}
61+
else
62+
vsyslog(level, fmt, ap);
63+
5164
va_end(ap);
5265
}
5366

@@ -64,11 +77,17 @@ void DBG_init(const char * procName)
6477
openlog(procName, LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER);
6578
}
6679

80+
void DBG_set_timer(struct EventTimer *timer)
81+
{
82+
gDBGTimer = timer;
83+
}
84+
6785
// Print out a message with the system error number message
6886
void DBG_syserr(unsigned long line, const char *func, const char *file,
6987
int level, const char *fmt, ...)
7088
{
7189
char buff[MESSAGE_BUFF_LENGTH];
90+
struct timeval now;
7291
va_list ap;
7392

7493
if (level > gDBGLevel) {
@@ -78,8 +97,15 @@ void DBG_syserr(unsigned long line, const char *func, const char *file,
7897
// Read in the variable arguments to the buffer
7998
va_start(ap, fmt);
8099
vsnprintf(&buff[0], sizeof(buff), fmt, ap);
100+
buff[sizeof(buff)-1] = 0;
81101
va_end(ap);
82102

83-
syslog(level, "%m - %s in %s() at %s:%lu\n",
103+
if (gDBGTimer) {
104+
gDBGTimer->get_gmt_time(gDBGTimer, &now);
105+
syslog(level, "%lu,%06lu: %m - %s in %s() at %s:%lu\n",
106+
now.tv_sec, now.tv_usec, buff, func, file, line);
107+
}
108+
else
109+
syslog(level, "%m - %s in %s() at %s:%lu\n",
84110
buff, func, file, line);
85111
}

Diff for: debug.h

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ void DBG_setLevel(int newLevel);
7171
*/
7272
void DBG_init(const char *procName);
7373

74+
struct EventTimer;
75+
/**
76+
* Provides a timer to use when timestamping debug messages. Optional.
77+
*
78+
* @param timer The timer to use for message timestamps. Pass in NULL
79+
* to disable timestamps.
80+
*/
81+
void DBG_set_timer(struct EventTimer *timer);
82+
7483
/**
7584
* Message logging utilizing the built in error messages associated with errno.
7685
*

0 commit comments

Comments
 (0)