comparison ie-docker.c @ 19:fe7095c365cf

WIP: Cleanup source code
author atton
date Tue, 24 Nov 2015 17:22:12 +0900
parents 8efb8b39ae0c
children c908e2c7315d
comparison
equal deleted inserted replaced
18:8efb8b39ae0c 19:fe7095c365cf
228 { 228 {
229 printf("Usage:\tie-docker run\n"); 229 printf("Usage:\tie-docker run\n");
230 printf("\tie-docker [option] --name [ps_name] {image name}:{tag} [execute command] [argument]"); 230 printf("\tie-docker [option] --name [ps_name] {image name}:{tag} [execute command] [argument]");
231 } 231 }
232 232
233 int
234 command_is_matched(const char const * arg, const char const * command)
235 {
236 return strcmp(arg, command) == 0;
237 }
238
233 /* main(int argc, char **argv) - main process loop */ 239 /* main(int argc, char **argv) - main process loop */
234 240
235 int 241 int
236 main(int argc, char **argv) 242 main(int argc, char **argv)
237 { 243 {
245 gid = getgid(); 251 gid = getgid();
246 printf("uid %d gid %d name %s\n", uid,gid,name); 252 printf("uid %d gid %d name %s\n", uid,gid,name);
247 setegid(getgid()); 253 setegid(getgid());
248 seteuid(getuid()); 254 seteuid(getuid());
249 255
256 regex_t *pattern = NEW(regex_t);
257 if (regcomp(pattern, name, 0) != 0) {
258 exit(0);
259 }
260
250 /* Confirm user is in GROUP(999) group */ 261 /* Confirm user is in GROUP(999) group */
251 262
252 /* 263 /*
253 if ( gid != 999 ) { 264 if ( gid != 999 ) {
254 printf("User Not Authorized! Exiting...\n"); 265 printf("User Not Authorized! Exiting...\n");
256 } 267 }
257 */ 268 */
258 269
259 /* Set uid, gid, euid and egid to root */ 270 /* Set uid, gid, euid and egid to root */
260 271
261 regex_t *pattern = NEW(regex_t);
262 if (regcomp(pattern, name, 0) != 0) {
263 exit(0);
264 }
265
266 setegid(0); 272 setegid(0);
267 seteuid(0); 273 seteuid(0);
268 setgid(0); 274 setgid(0);
269 setuid(0); 275 setuid(0);
270 276
271
272 char *ps_name = (char *)malloc(sizeof(char) * PS_NAME_LENGTH);
273 if (ps_name == NULL) {
274 printf("[!] malloc error.");
275 }
276 ps_name[0] = '\0';
277
278 run_command_opt *run_opt = (run_command_opt *)malloc(sizeof(run_command_opt));
279 exec_command_opt *exec_opt = (exec_command_opt *)malloc(sizeof(exec_command_opt));
280 if (run_opt == NULL && exec_opt == NULL) {
281 printf("[!] malloc error.");
282 exit(1);
283 }
284 run_opt->tty = FALSE;
285 run_opt->dettach = FALSE;
286 run_opt->interactive = FALSE;
287
288 exec_opt->tty = FALSE;
289 exec_opt->dettach = FALSE;
290 exec_opt->interactive = FALSE;
291
292 if (argc < 2) { 277 if (argc < 2) {
293 usage(); 278 usage();
294 exit(0); 279 exit(0);
295 }
296
297 if (strncmp(argv[1], "ps", 4) != 0) {
298 if (strncmp(argv[1], run_command, 3) == 0) {
299 parse_run_command(argc, argv, run_opt);
300 if (check_name(run_opt->ps_name)) {
301 fprintf(stderr, bad_name);
302 exit(0);
303 }
304 get_port_number(name, run_opt->ps_name, run_opt->outerport);
305 strncpy(ps_name, run_opt->ps_name, 64);
306 run_opt->ps_name[0] = '\0';
307 //make_ps_name(run_opt->ps_name, account_type, name, ps_name);
308 } else if (strncmp(argv[1], exec_command, 4) == 0){
309 parse_exec_command(argc, argv, exec_opt);
310 strncpy(ps_name, exec_opt->ps_name, 64);
311 //make_ps_name(exec_opt->ps_name, account_type, name, ps_name);
312 } else if (strncmp(argv[1], rm_command, 4) == 0) {
313 char exec[512];
314 sprintf(exec, "/usr/local/bin/remove.py %s", argv[2]);
315 system(exec);
316
317 //make_ps_name(ps_name, account_type, name, argv[2]);
318 free(run_opt);
319 free(exec_opt);
320 } else {
321 //make_ps_name(ps_name, account_type, name, argv[2]);
322 free(run_opt);
323 free(exec_opt);
324 }
325 } 280 }
326 281
327 PSLISTPTR pslist = get_pslist(pattern); 282 PSLISTPTR pslist = get_pslist(pattern);
328 283
329 /* 284 /*
330 * Check argv for proper arguments and run 285 * Check argv for proper arguments and run
331 * the corresponding script, if invoked. 286 * the corresponding script, if invoked.
332 */ 287 */
333 288
334 if (argv[1]==0 || strncmp(argv[1], "ps", 4) == 0 ) { 289 if (command_is_matched(argv[1], ps_command)) {
290 printf("ps"); //TODO
335 print_pslist(pslist); 291 print_pslist(pslist);
336 } else if (strncmp(argv[1], run_command, 5) == 0) { 292 } else if (command_is_matched(argv[1], run_command)) {
337 293 printf("run"); // TODO
338 char *run_args[17]; 294 } else if (command_is_matched(argv[1], start_command)) {
339 int i = 0; 295 printf("start"); // TODO
340 296 } else if (command_is_matched(argv[1], stop_command)) {
341 run_args[i++] = command; 297 printf("stop"); // TODO
342 run_args[i++] = run_command; 298 } else if (command_is_matched(argv[1], attach_command)) {
343 run_args[i++] = "--privileged"; 299 printf("attach"); // TODO
344 if (run_opt->dettach) run_args[i++] = "-d"; 300 } else if (command_is_matched(argv[1], rm_command)) {
345 if (run_opt->tty) run_args[i++] = "-t"; 301 printf("rm"); // TODO
346 if (run_opt->interactive) run_args[i++] = "-i"; 302 } else if (command_is_matched(argv[1], cp_command)) {
347 run_args[i++] = "-m"; 303 printf("cp"); // TODO
348 run_args[i++] = "512m";
349 run_args[i++] = "-v";
350 run_args[i++] = run_opt->volume;
351 run_args[i++] = "-p";
352 char port[32];
353 sprintf(port, "%s:%s", run_opt->outerport, run_opt->innerport);
354 run_args[i++] = port;
355 run_args[i++] = "--name";
356 run_args[i++] = run_opt->ps_name;
357 run_args[i++] = run_opt->image_name;
358 run_args[i++] = run_opt->exec_ps_command;
359 run_args[i++] = NULL;
360 /*
361 printf("run command opt ::memory-%s innerport-%s outerport-%s tty-%d dettach-%d interactive-%d ps_name-%s exec_ps_command-%s volume-%s image-name-%s\n",
362 opt->memory,
363 opt->innerport,
364 opt->outerport,
365 opt->tty,
366 opt->dettach,
367 opt->interactive,
368 opt->ps_name,
369 opt->exec_ps_command,
370 opt->volume,
371 opt->image_name);
372
373 */
374 if (execv(run_args[0], run_args) < 0) {
375 perror("[!] Execv:");
376 }
377 } else if (strncmp(argv[1], start_command, 5) == 0) {
378 if (execl(command, command, start_command, ps_name, NULL) < 0) {
379 perror("[!] Execl:");
380 }
381 } else if (strncmp(argv[1], exec_command, 5) == 0) {
382
383 int i = 0;
384 char *exec_args[8];
385
386 exec_args[i++] = command;
387 exec_args[i++] = exec_command;
388 if (exec_opt->dettach) exec_args[i++] = "-d";
389 if (exec_opt->tty) exec_args[i++] = "-t";
390 if (exec_opt->interactive) exec_args[i++] = "-i";
391 exec_args[i++] = exec_opt->ps_name;
392 exec_args[i++] = exec_opt->exec_ps_command;
393 exec_args[i++] = NULL;
394
395 if (execv(exec_args[0], exec_args) < 0) {
396 perror("[!] Execl:");
397 }
398 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
399 if (execl(command, command, stop_command, ps_name, NULL) < 0) {
400 perror("[!] Execl:");
401 }
402 } else if ( strncmp(argv[1], build_command, 8) == 0 ) {
403 if (execl(command, command, build_command, argv[2], NULL) < 0) {
404 perror("[!] Execl:");
405 }
406 } else if (strncmp(argv[1], attach_command, 6) == 0 ) {
407 if (execl(command, command, attach_command, ps_name, NULL) < 0) {
408 perror("[!] Execl:");
409 }
410 } else if ( strncmp(argv[1], rm_command, 2) == 0 ) {
411 if (execl(command, command, rm_command, ps_name, NULL) < 0) {
412 perror("[!] Execl:");
413 }
414 } else { 304 } else {
415 usage(); 305 usage();
416 } 306 return 0;
417 free(ps_name); 307 }
418 free(exec_opt); 308 return 0;
419 free(run_opt);
420 exit(0);
421 } 309 }
422 310
423 /* end */ 311 /* end */