-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommon.h
executable file
·161 lines (134 loc) · 4 KB
/
common.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
153
154
155
156
157
158
159
160
161
#ifndef DLUA_COMMON_H
#define DLUA_COMMON_H
#include <string>
#include <list>
#include <vector>
#include <map>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <time.h>
#include <stdarg.h>
#include <assert.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <unordered_map>
#include <fcntl.h>
#include <sstream>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <set>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
const int open_debug = 0;
#define DLOG(...) if (open_debug) {dlog(stdout, "[DEBUG] ", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);}
#define DERR(...) {dlog(stderr, "[ERROR] ", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);}
void dlog(FILE *fd, const char *header, const char *file, const char *func, int pos, const char *fmt, ...) {
time_t clock1;
struct tm *tptr;
va_list ap;
clock1 = time(0);
tptr = localtime(&clock1);
struct timeval tv;
gettimeofday(&tv, NULL);
fprintf(fd, "%s[%d.%d.%d,%d:%d:%d,%llu]%s:%d,%s: ", header,
tptr->tm_year + 1900, tptr->tm_mon + 1,
tptr->tm_mday, tptr->tm_hour, tptr->tm_min,
tptr->tm_sec, (long long) ((tv.tv_usec) / 1000) % 1000, file, pos, func);
va_start(ap, fmt);
vfprintf(fd, fmt, ap);
fprintf(fd, "\n");
va_end(ap);
}
const int QUEUED_MESSAGE_MSG_LEN = 1023;
struct QueuedMessage {
long type;
char payload[QUEUED_MESSAGE_MSG_LEN + 1];
};
const int LOGIN_MSG = 1;
const int COMMAND_MSG = 2;
const int SHOW_MSG = 3;
const int INPUT_MSG = 4;
static int send_msg(int qid, long type, const char *data) {
QueuedMessage msg;
msg.type = type;
strncpy(msg.payload, data, QUEUED_MESSAGE_MSG_LEN);
msg.payload[QUEUED_MESSAGE_MSG_LEN] = 0;
int msgsz = strlen(msg.payload);
if (msgsnd(qid, &msg, msgsz, 0) != 0) {
DERR("send_msg %d %d error %d %s", qid, msgsz, errno, strerror(errno));
return -1;
}
return 0;
}
static int recv_msg(int qid, long &type, char data[QUEUED_MESSAGE_MSG_LEN]) {
QueuedMessage msg;
int size = msgrcv(qid, &msg, QUEUED_MESSAGE_MSG_LEN, 0, IPC_NOWAIT);
if (size == -1) {
if (errno == ENOMSG) {
type = 0;
data[0] = 0;
return 0;
}
DERR("recv_msg %d error %d %s", qid, errno, strerror(errno));
return -1;
}
type = msg.type;
strncpy(data, msg.payload, QUEUED_MESSAGE_MSG_LEN);
data[size] = 0;
return 0;
}
static int open_msg_queue(const char *tmpfilename, int pid) {
int tmpfd = open(tmpfilename, O_RDWR | O_CREAT, 0777);
if (tmpfd == -1) {
DERR("open tmpfile fail %s %d %s", tmpfilename, errno, strerror(errno));
return -1;
}
close(tmpfd);
key_t key = ftok(tmpfilename, pid);
if (key == -1) {
DERR("ftok fail %s %d %d %s", tmpfilename, pid, errno, strerror(errno));
return -1;
}
DLOG("ftok key %d", key);
int qid = msgget(key, 0666 | IPC_CREAT);
if (qid < 0) {
DERR("msgget fail %d %d %s", key, errno, strerror(errno));
return -1;
}
DLOG("qid %d", qid);
return qid;
}
std::string exec_command(const char *cmd, int &out) {
out = 0;
auto pPipe = ::popen(cmd, "r");
if (pPipe == nullptr) {
out = -1;
return "";
}
std::array<char, 256> buffer;
std::string result;
while (not std::feof(pPipe)) {
auto bytes = std::fread(buffer.data(), 1, buffer.size(), pPipe);
result.append(buffer.data(), bytes);
}
auto rc = ::pclose(pPipe);
if (WIFEXITED(rc)) {
out = WEXITSTATUS(rc);
}
return result;
}
#endif //DLUA_COMMON_H