Subversion Repositories Mokou

Rev

Rev 12 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 13
Line 1... Line 1...
1
/* $Id: service.c 12 2024-09-07 10:18:01Z nishi $ */
1
/* $Id: service.c 13 2024-09-07 12:44:18Z nishi $ */
2
 
2
 
3
#include "mk_service.h"
3
#include "mk_service.h"
4
 
4
 
5
#include <fcntl.h>
5
#include <fcntl.h>
6
#include <stdio.h>
6
#include <stdio.h>
Line 96... Line 96...
96
 
96
 
97
					char* desc = NULL;
97
					char* desc = NULL;
98
					char* exec = NULL;
98
					char* exec = NULL;
99
					char* stop = NULL;
99
					char* stop = NULL;
100
					char* pidfile = NULL;
100
					char* pidfile = NULL;
-
 
101
					uid_t uid = 0;
-
 
102
					gid_t gid = 0;
-
 
103
					bool bad = false;
101
					
104
					
102
					for(i = 0;; i++){
105
					for(i = 0;; i++){
103
						if(buffer[i] == '\n' || buffer[i] == 0){
106
						if(buffer[i] == '\n' || buffer[i] == 0){
104
							char oldc = buffer[i];
107
							char oldc = buffer[i];
105
							buffer[i] = 0;
108
							buffer[i] = 0;
Line 125... Line 128...
125
											if(pidfile != NULL) free(pidfile);
128
											if(pidfile != NULL) free(pidfile);
126
											pidfile = mk_strdup(value);
129
											pidfile = mk_strdup(value);
127
										}else if(strcmp(key, "stop") == 0){
130
										}else if(strcmp(key, "stop") == 0){
128
											if(stop != NULL) free(stop);
131
											if(stop != NULL) free(stop);
129
											stop = mk_strdup(value);
132
											stop = mk_strdup(value);
-
 
133
										}else if(strcmp(key, "user") == 0){
-
 
134
											struct passwd* p = getpwnam(value);
-
 
135
											if(p != NULL){
-
 
136
												uid = p->pw_uid;
-
 
137
												gid = p->pw_gid;
-
 
138
											}else{
-
 
139
												mk_log("Could not find the specified user");
-
 
140
												bad = true;
-
 
141
											}
130
										}
142
										}
131
	
143
	
132
										break;
144
										break;
133
									}
145
									}
134
								}
146
								}
Line 138... Line 150...
138
							if(oldc == 0) break;
150
							if(oldc == 0) break;
139
						}
151
						}
140
					}
152
					}
141
					fclose(f);
153
					fclose(f);
142
 
154
 
143
					bool bad = false;
-
 
144
					if(exec == NULL){
155
					if(exec == NULL){
145
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec");
156
						char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec");
146
						mk_log(log);
157
						mk_log(log);
147
						free(log);
158
						free(log);
148
						bad = true;
159
						bad = true;
Line 172... Line 183...
172
 
183
 
173
						serv->description = desc != NULL ? mk_strdup(desc) : NULL;
184
						serv->description = desc != NULL ? mk_strdup(desc) : NULL;
174
						serv->stop = stop != NULL ? mk_strdup(stop) : NULL;
185
						serv->stop = stop != NULL ? mk_strdup(stop) : NULL;
175
						serv->exec = mk_strdup(exec);
186
						serv->exec = mk_strdup(exec);
176
						serv->pidfile = mk_strdup(pidfile);
187
						serv->pidfile = mk_strdup(pidfile);
-
 
188
						serv->uid = uid;
-
 
189
						serv->gid = gid;
177
						serv->stopped = false;
190
						serv->stopped = false;
178
 
191
 
179
						struct mk_service** oldsrvs = services;
192
						struct mk_service** oldsrvs = services;
180
						for(i = 0; oldsrvs[i] != NULL; i++);
193
						for(i = 0; oldsrvs[i] != NULL; i++);
181
						services = malloc(sizeof(*services) * (i + 2));
194
						services = malloc(sizeof(*services) * (i + 2));
Line 289... Line 302...
289
				pid_t pid = fork();
302
				pid_t pid = fork();
290
				if(pid == 0){
303
				if(pid == 0){
291
					int n = open("/dev/null", O_RDWR);
304
					int n = open("/dev/null", O_RDWR);
292
					dup2(n, 1);
305
					dup2(n, 1);
293
					dup2(n, 2);
306
					dup2(n, 2);
-
 
307
					setgid(srv->gid);
-
 
308
					setegid(srv->gid);
-
 
309
					setuid(srv->uid);
-
 
310
					seteuid(srv->uid);
294
					execvp(pargv[0], pargv);
311
					execvp(pargv[0], pargv);
295
					_exit(-1);
312
					_exit(-1);
296
				}else{
313
				}else{
297
					int status;
314
					int status;
298
					waitpid(pid, &status, 0);
315
					waitpid(pid, &status, 0);
Line 386... Line 403...
386
			pid_t pid = fork();
403
			pid_t pid = fork();
387
			if(pid == 0){
404
			if(pid == 0){
388
				int n = open("/dev/null", O_RDWR);
405
				int n = open("/dev/null", O_RDWR);
389
				dup2(n, 1);
406
				dup2(n, 1);
390
				dup2(n, 2);
407
				dup2(n, 2);
-
 
408
				setgid(srv->gid);
-
 
409
				setegid(srv->gid);
-
 
410
				setuid(srv->uid);
-
 
411
				seteuid(srv->uid);
391
				execvp(pargv[0], pargv);
412
				execvp(pargv[0], pargv);
392
				_exit(-1);
413
				_exit(-1);
393
			}else{
414
			}else{
394
				int status;
415
				int status;
395
				waitpid(pid, &status, 0);
416
				waitpid(pid, &status, 0);