simple-init

A set of init binaries for linux
git clone git://depsterr.com/git/simple-init
Log | Files | Refs | README | LICENSE

util.c (2160B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 #include <sys/wait.h>
      4 #include <fcntl.h>
      5 #include <unistd.h>
      6 
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <mntent.h>
     10 #include <stdio.h>
     11 
     12 void log_message(char* msg) {
     13 	printf("\033[31;1m=>\033[m %s\n", msg);
     14 	if(access("/dev/kmsg", F_OK|W_OK) != -1) {
     15 		FILE* fp = fopen("/dev/kmsg", "w");
     16 		fprintf(fp, "init: %s\n", msg);
     17 		fclose(fp);
     18 	}
     19 }
     20 
     21 int spawn_process(char** argv) {
     22 	int child_pid;
     23 	if ((child_pid = fork()) == 0) {
     24 		setsid();
     25 		execvp(argv[0], argv);
     26 		fprintf(stderr, "execvp: %s\n", argv[0]);
     27 		exit(1);
     28 	}
     29 	return child_pid;
     30 }
     31 
     32 void respawn_process(char** argv) {
     33 	if (fork() == 0) {
     34 		int child_pid;
     35 		for(;;) {
     36 			child_pid = spawn_process(argv);
     37 			waitpid(child_pid, 0, 0);
     38 		}
     39 	}
     40 }
     41 
     42 void wait_for_children() {
     43 	int status = 0;
     44 	while(wait(&status) > 0) {
     45 		if (WEXITSTATUS(status) != 0 || !WIFEXITED(status)) {
     46 			log_message("Child failed, dropping to shell. Exit shell to continue normally.");
     47 			char* shellcmd[] = { "/bin/sh", 0 };
     48 			int pid = spawn_process(shellcmd);
     49 			waitpid(pid, &status, 0);
     50 			break;
     51 		}
     52 	}
     53 }
     54 
     55 void make_path(char* path) {
     56 	char  ret = 0;
     57 	char* dirpath = 0;
     58 	for (int n = 1; path[n] != '\0' && ret == 0; n++) {
     59 		if (path[n] == '/') {
     60 			dirpath = realloc(dirpath, n + 1);
     61 			strncpy(dirpath, path, n + 1);
     62 			dirpath[n] = '\0';
     63 			mkdir(dirpath, 0755);
     64 		}
     65 	}
     66 }
     67 
     68 void save_random_seed() {
     69 	make_path("/var/lib/init");
     70 
     71 	int fdesc = open("/dev/urandom", O_RDONLY);
     72 	char random_buffer[512];
     73 
     74 	if (fdesc < 0)
     75 		return;
     76 	else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0))
     77 		return;
     78 
     79 	fdesc = open("/var/lib/init/random-seed", O_CREAT|O_WRONLY);
     80 
     81 	if (fdesc < 0)
     82 		return;
     83 	else
     84 		write(fdesc, random_buffer, sizeof(random_buffer));
     85 }
     86 
     87 void load_random_seed() {
     88 	if (access("/var/lib/init/random-seed", F_OK) == -1) {
     89 		return;
     90 	}
     91 
     92 	int fdesc = open("/var/lib/init/random-seed", O_RDONLY);
     93 	char random_buffer[512];
     94 
     95 	if (fdesc < 0)
     96 		return;
     97 	else if (read(fdesc, random_buffer, sizeof(random_buffer) < 0))
     98 		return;
     99 
    100 	fdesc = open("/dev/urandom", O_WRONLY);
    101 
    102 	if (fdesc < 0)
    103 		return;
    104 	else
    105 		write(fdesc, random_buffer, sizeof(random_buffer));
    106 }