forked from justanhduc/task-spooler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
346 lines (310 loc) · 8.32 KB
/
user.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "default.inc"
#include "main.h"
#include "user.h"
void send_list_line(int s, const char *str);
void error(const char *str, ...);
int user_locked[USER_MAX] = {0};
const char *get_user_path() {
char *str;
str = getenv("TS_USER_PATH");
if (str == NULL || strlen(str) == 0) {
return DEFAULT_USER_PATH;
} else {
return str;
}
}
int get_env(const char *env, int v0) {
char *str;
str = getenv(env);
if (str == NULL || strlen(str) == 0) {
return v0;
} else {
int i = atoi(str);
if (i < 0)
i = v0;
return i;
}
}
//按空格自动分割子串的函数
char **split_str(const char *str0, int *size) {
char **result = (char **)malloc(sizeof(char *)); //存储分割后的子串
char *str = (char *)malloc(sizeof(char) * strlen(str0));
strcpy(str, str0);
int n = 0; //数组的大小
char *token; //分割得到的子串
token = strtok(str, " "); //以空格为分隔符分割字符串
while (token != NULL) { //循环分割,直到遇到NULL
result = realloc(result,
(n + 1) * sizeof(char *)); //重新分配内存空间,增加一个元素
if (result == NULL) { //如果内存分配失败,返回NULL
return NULL;
}
result[n] = token; //将子串存入数组
n++; //更新数组的大小
token = strtok(NULL, " "); //继续分割
}
*size = n; //返回数组的大小
return result; //返回数组
}
/*
char* linux_cmd(char* CMD, char* out, int out_size) {
FILE *fp;
/- Open the command for reading. -/
fp = popen(CMD, "r");
if (fp == NULL) {
printf("Failed to run command: %s\n", CMD);
exit(1);
}
/- Read the output a line at a time - output it. -/
while (fgets(out, out_size, fp) != NULL) {
; // printf("%s", path);
}
char* end = memchr(out, '\n', out_size);
if (end != NULL) *end = '\0';
/- close -/
pclose(fp);
return out;
}
*/
long str2int(const char *str) {
long i;
if (sscanf(str, "%ld", &i) == 0) {
printf("Error in convert %s to number\n", str);
exit(-1);
}
return i;
}
const char *set_server_logfile() {
logfile_path = getenv("TS_LOGFILE_PATH");
if (logfile_path == NULL || strlen(logfile_path) == 0) {
logfile_path = DEFAULT_LOG_PATH;
}
return logfile_path;
}
void check_relink(int pid) {
char buff[256];
struct stat t_stat;
snprintf(buff, 255, "/proc/%d/stat", pid);
if (stat(buff, &t_stat) != -1) {
command_line.start_time = t_stat.st_ctime;
} else {
if (kill(pid, 0) != 0) {
error("Client: PID[%d] is dead\n", pid);
}
}
command_line.outfile = NULL;
}
/*
ts_UID from user_number is 1-indexing
The minimal ts_UID for a valid user is 1;
*/
void write_logfile(const struct Job *p) {
// char buf[1024] = "";
FILE *f = fopen(logfile_path, "a");
if (f == NULL) {
return;
}
static char buf[100];
time_t now = time(0);
strftime(buf, 100, "%Y-%m-%d %H:%M:%S", localtime(&now));
// snprintf(buf, 1024, "[%d] %s @ %s\n", p->jobid, p->command, date);
int ts_UID = p->ts_UID;
char *label = "..";
if (p->label)
label = p->label;
fprintf(f, "[%d] %s P:%d <%s> Pid: %d CMD: %s @ %s\n", p->jobid,
user_name[ts_UID], p->num_slots, label, p->pid, p->command, buf);
fclose(f);
}
void debug_write(const char *str) {
FILE *f = fopen(logfile_path, "a");
if (f == NULL) {
return;
}
char buf[100];
time_t now = time(0);
strftime(buf, 100, "%Y-%m-%d %H:%M:%S", localtime(&now));
fprintf(f, "%s @ %s\n", buf, str);
fclose(f);
}
/*
static int find_user_by_name(const char *name) {
for (int i = 0; i < user_number; i++) {
if (strcmp(user_name[i], name) == 0)
return i;
}
return -1;
}
*/
int read_first_jobid_from_logfile(const char *path) {
FILE *fp;
fp = fopen(path, "r");
if (fp == NULL)
return 1000; // default start from 1000
char *line = NULL;
size_t len = 0;
size_t read;
int jobid;
while ((read = getline(&line, &len, fp)) != -1) {
}
int res = sscanf(line, "[%d]", &jobid);
if (jobid <= 0 || res != 1) {
jobid = 999;
}
printf("last line is %s with jobid = %d\n", line, jobid);
fclose(fp);
return jobid + 1;
}
void read_user_file(const char *path) {
server_uid = getuid();
// if (server_uid != root_UID) {
// error("the service is not run as root!");
//}
FILE *fp;
fp = fopen(path, "r");
if (fp == NULL)
exit(EXIT_FAILURE);
char *line = NULL;
size_t len = 0;
size_t read;
int UID, slots;
char name[USER_NAME_WIDTH];
while ((read = getline(&line, &len, fp)) != -1) {
if (line[0] == '#')
continue;
if (strncmp("TS_SLOTS", line, 8) == 0) {
int res = sscanf(line, "TS_SLOTS = %d", &slots);
if (slots > 0 && res == 1) {
printf("TS_SLOTS = %d\n", slots);
s_set_max_slots(0, slots);
continue;
}
} else if (strncmp("TS_FIRST_JOBID", line, 14) == 0) {
int res = sscanf(line, "TS_FIRST_JOBID = %d", &slots);
if (slots > 0 && res == 1) {
printf("TS_FIRST_JOBID = %d\n", slots);
s_set_jobids(slots);
continue;
}
}
int res = sscanf(line, "%d %256s %d", &UID, name, &slots);
if (res != 3) {
printf("error in read %s at line %s", path, line);
continue;
} else {
int ts_UID = get_tsUID(UID);
if (ts_UID == -1) {
if (user_number >= USER_MAX)
continue;
ts_UID = user_number;
user_number++;
user_UID[ts_UID] = UID;
user_max_slots[ts_UID] = slots;
strncpy(user_name[ts_UID], name, USER_NAME_WIDTH - 1);
} else {
user_max_slots[ts_UID] = slots;
}
}
}
fclose(fp);
if (line)
free(line);
}
const char *uid2user_name(int uid) {
// if (uid == 0)
// return "Root";
int ts_UID = get_tsUID(uid);
if (ts_UID != -1) {
return user_name[ts_UID];
} else {
return "Unknown";
}
}
void s_user_status_all(int s) {
char buffer[256];
char *extra;
send_list_line(s, "-- Users ----------- \n");
for (int i = 0; i < user_number; i++) {
extra = user_locked[i] != 0 ? "Locked" : "";
if (user_max_slots[i] == 0 && user_busy[i] == 0)
continue;
snprintf(buffer, 256, "[%04d] %3d/%-4d Q:%-3d %16s Run. %2d %s\n",
user_UID[i], user_busy[i], abs(user_max_slots[i]), user_queue[i],
user_name[i], user_jobs[i], extra);
send_list_line(s, buffer);
}
snprintf(buffer, 256, "Service at UID:%d\n", server_uid);
send_list_line(s, buffer);
}
// i = ts_UID;
void s_user_status(int s, int i) {
char buffer[256];
char *extra = "";
if (user_locked[i] != 0)
extra = "Locked";
snprintf(buffer, 256, "[%04d] %3d/%-4d Q:%-3d %16s Run. %2d %s\n",
user_UID[i], user_busy[i], abs(user_max_slots[i]), user_queue[i],
user_name[i], user_jobs[i], extra);
send_list_line(s, buffer);
}
int get_tsUID(int uid) {
for (int i = 0; i < user_number; i++) {
if (uid == user_UID[i]) {
return i;
}
}
return -1;
}
void kill_pids(int parent_pid, int signal, const char* cmd) {
char path[256];
DIR *dir;
struct dirent *entry;
snprintf(path, sizeof(path), "/proc/%d/task", parent_pid);
if ((dir = opendir(path)) == NULL) {
// printf("Cannot find PID from %s\n", path);
return;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
int tid = atoi(entry->d_name);
// printf("TID: %d\n", tid);
snprintf(path, sizeof(path), "/proc/%d/task/%d/children", parent_pid, tid);
FILE *file = fopen(path, "r");
if (path == NULL) {
printf("cannot open %s\n", path);
continue;
}
int cPID;
while (fscanf(file, "%d", &cPID) == 1) {
// printf("Children PID: %d\n", cPID);
kill_pids(cPID, signal, cmd);
}
fclose(file);
}
closedir(dir);
if (signal >= 0 && kill(parent_pid, signal) != 0) {
printf("kill %d -%d ", parent_pid, signal);
perror("Error resuming process");
}
if (cmd != NULL) {
char buf[1024] = "";
snprintf(buf, 1024, "%s %d", cmd, parent_pid);
printf("%s\n", buf);
int result = system(buf);
if (result == -1) {
; // perror("Error executing command");
}
}
}