-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServerSpell.c
177 lines (152 loc) · 4.35 KB
/
ServerSpell.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#define _GNU_SOURCE
int sort(int param, char *option, char *soption);
int create_config(char* path);
int registerJar(char* path, char* jar);
int startJar();
int showLog();
int server_status();
int stopJar();
int main(int argc, char* argv[]){
// Config set;
// Config_Read(&set, "ServerSpell.conf");
if(argc < 2){
printf("ServerSpell v1.0.0 : no command specified\nTry \'ServerSpell -h\' for more information.\n");
}else{
sort(argc, argv[1], argv[2]);
}
return 0;
}
int sort(int param, char *option, char *soption){
if(strncmp(option, "-h",sizeof(option))==0){
printf("ServerSpell v1.0.0\n\nCommands\n\n-h : show command option information\n\n-v : show ServerSpells Version\n\n-r [jarName] : Register jar to run\n\n");
printf("--start : Run jar server\n\n--log : Show log for jar server\n\n--status: Check jar server status\n\n--stop : Stop jar Server\n\n");
}else if(strncmp(option,"-v",sizeof(option))==0){
printf("\nServerSpell Version 1.0.0 Develop by Archan All right Reversed\n\n");
}else if(strncmp(option,"-r",sizeof(option))==0){
if(param == 3){
registerJar("ServerSpell.conf", soption);
}else{
printf("\nServerSpell v1.0.0 : Option \"-r\" invalid argument\n\nPlease check ServerSpell -h\n\n");
}
}else if(strncmp(option,"--start",sizeof(option))==0){
startJar();
}else if(strncmp(option,"--log",sizeof(option))==0){
showLog();
} else if(strncmp(option, "--status",sizeof(option))==0) {
server_status();
}else if(strncmp(option,"--stop",sizeof(option))==0){
stopJar();
}else{
printf("Not valid Option\n");
}
return 0;
}
int registerJar(char* path, char* jar) {
int fd;
printf("Conf file create at \"%s\"\n", path);
FILE* stream = NULL;
stream = fopen(path, "w");
if(stream == NULL){
printf("Error : Can't create file\n");
return 0;
}else{
printf("Successfully create Conf file\n");
fprintf(stream, "%s\n", jar);
}
fclose(stream);
return 0;
}
int startJar(){
FILE* stream = NULL;
stream = fopen("ServerSpell.conf", "r");
if(stream == NULL){
printf("Error : Can't open file Please register jar file\n");
return 0;
}else{
char line[256];
char command[256];
size_t len = 0;
while(fgets(line, sizeof(line), stream) != NULL){
line[strlen(line) - 1] = '\0';
printf("Command executed: nohup java -jar %s &\n", line);
sprintf(command, "nohup java -jar %s &", line);
system(command);
printf("Starting jar file named %s\n", line);
printf("Please use --log to see log\n");
}
}
fclose(stream);
return 0;
}
int showLog(){
system("tail -f nohup.out");
return 0;
}
int server_status() {
char command[256];
char line[256];
char pid[256];
FILE* stream = NULL;
FILE* shell = NULL;
stream = fopen("ServerSpell.conf", "r");
if (stream == NULL) {
printf("Error: Can't open file Please register jar file\n");
goto err;
}
printf("PID\t\tName\t\t\tStatus\n");
printf("---------------------------------------------------\n");
if(fgets(line, sizeof(line), stream) != NULL) {
line[strlen(line) - 1] = '\0';
//find process pid with jar name
snprintf(command, sizeof(command), "ps -ef | grep %s | grep -v grep | awk '{print $2}'", line);
shell = popen(command, "r");
if (shell == NULL) {
printf("[%s/%d] Error: status command error\n", __func__, __LINE__);
}
fgets(pid, sizeof(pid), shell);
if (atoi(pid) == 0) {
printf("\t\t(%s)\t\tStop...\n", line);
} else {
pid[strlen(pid) - 1] = '\0';
printf("(%s)\t\t(%s)\t\tRunning...\n", pid, line);
}
}
return 0;
err:
return -1;
}
int stopJar(){
FILE* stream = NULL;
stream = fopen("ServerSpell.conf", "r");
if(stream == NULL){
printf("Error : Can't open file Please register jar file\n");
return 0;
}else{
char line[256];
char command[256];
size_t len = 0;
while(fgets(line, sizeof(line), stream) != NULL){
line[strlen(line) - 1] = '\0';
//find proccess pid with jar name
sprintf(command, "ps -ef | grep %s | grep -v grep | awk '{print $2}'", line);
FILE* stream = popen(command, "r");
char pid[256];
fgets(pid, sizeof(pid), stream);
pid[strlen(pid) - 1] = '\0';
printf("Command executed: kill -9 %s\n", pid);
sprintf(command, "kill -9 %s", pid);
system(command);
printf("Stopping jar file named %s\n", line);
}
}
fclose(stream);
return 0;
}