int main(int argc, char * argv[]) { int port; if(argc == 1) { port = 9500; } else { port = atoi(argv[1]); } struct sockaddr_in s_addr; int cli_len, sfd, nsfd, i; char buf[BUFSIZE]; memset((char *)&s_addr, 0, sizeof(s_addr)); sfd = socket(AF_INET, SOCK_DGRAM, 0); if(sfd == -1) { eerror("socket() error"); } s_addr.sin_family = AF_INET; s_addr.sin_port = htons((u_short) port); s_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); for (i = 0; i < 10; ++i) { memset(buf, 0, BUFSIZE); fgets(buf,255,stdin); //sprintf(buf, "Hello %d", i+1); if(sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&s_addr, sizeof(s_addr)) < 0) { eerror("sendto() error"); } printf("%s\n", buf); cli_len = sizeof(s_addr); memset(buf, 0, BUFSIZE); if(recvfrom(sfd, buf, BUFSIZE, 0, (struct sockaddr *)&s_addr, &cli_len) < 0) { eerror("recvfrom() error"); } printf("%s\n", buf); } close(sfd); return 0; }
static pid_t get_pid(const char *pidfile, bool quiet) { FILE *fp; pid_t pid; if (! pidfile) return -1; if ((fp = fopen(pidfile, "r")) == NULL) { if (!quiet) eerror("%s: fopen `%s': %s", applet, pidfile, strerror(errno)); return -1; } if (fscanf(fp, "%d", &pid) != 1) { if (!quiet) eerror("%s: no pid found in `%s'", applet, pidfile); fclose(fp); return -1; } fclose(fp); return pid; }
static bool set_krunlevel(const char *level) { FILE *fp; if (!level || strcmp(level, getenv ("RC_BOOTLEVEL")) == 0 || strcmp(level, RC_LEVEL_SINGLE) == 0 || strcmp(level, RC_LEVEL_SYSINIT) == 0) { if (exists(RC_KRUNLEVEL) && unlink(RC_KRUNLEVEL) != 0) eerror("unlink `%s': %s", RC_KRUNLEVEL, strerror(errno)); return false; } if (!(fp = fopen(RC_KRUNLEVEL, "w"))) { eerror("fopen `%s': %s", RC_KRUNLEVEL, strerror(errno)); return false; } fprintf(fp, "%s", level); fclose(fp); return true; }
int main(int argc, char const *argv[]) { if (argc == 1) eerror("Usage: ./service <fifo-prefix>"); char buf[128], tmp[128]; strcpy(buf, argv[1]); strcat(buf, "r.dat"); int fd_w = open(buf, O_WRONLY); if(fd_w == -1) eerror("WERROR !!"); strcpy(tmp, argv[1]); strcat(tmp, "w.dat"); int fd_r = open(tmp, O_RDONLY); if(fd_r == -1) eerror("RERROR !!"); while(1){ memset(buf, 128, '\0'); if(read(fd_r, buf, 128) == 0){ sleep(0.1); continue; } if(fork() > 0){ wait(); } else { execlp("ls", "ls", buf, NULL); eerror("execlp error"); } } return 0; }
int main(int argc,char *argv[]) { if(argc == 1) eerror("Usage: ./client <unique-name>"); char buf[128], tmp[128]; // mkfifo(sfifo,0666); strcpy(tmp, argv[1]); strcat(tmp, "r"); mkfifo(tmp, 0666); strcpy(buf, argv[1]); strcat(buf, "w"); mkfifo(buf, 0666); char str[128]; strcpy(str,argv[1]); strcat(str," "); strcat(str,argv[2]); printf("%s\n", str ); int fd = open(sfifo, O_WRONLY|O_NONBLOCK); write(fd, str , strlen(str)); // write(fd, argv[2], strlen(argv[2])); close(fd); //printf("%s\n", tmp ); int fd_r = open(tmp, O_RDONLY); if(fd_r == -1) eerror("fd_r error"); printf("open %s\n", tmp); int fd_w = open(buf, O_WRONLY); if(fd_w == -1) eerror("fd_w error"); printf("open %s\n", buf); //write(fd, argv[2], strlen(argv[2])); int pid; pid = fork(); if(pid >0) { // parent process close(fd_r); while(1) { memset(tmp, 128, '\0'); scanf("%s", tmp); write(fd_w, tmp, strlen(tmp)+1); } } else { // child process close(fd_w); while(1) { memset(buf, 128, '\0'); read(fd_r, buf, 128); printf("%s\n", buf); } } return 0; }
int main(int argc, char **argv) { char *arg = NULL; int opt; bool dryrun = false; RC_STRINGLIST *omits = rc_stringlist_new(); int sig = SIGKILL; char *here; char *token; /* Ensure that we are only quiet when explicitly told to be */ unsetenv("EINFO_QUIET"); applet = basename_c(argv[0]); rc_stringlist_addu(omits, "1"); while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1) { switch (opt) { case 'd': dryrun = true; break; case 'o': here = optarg; while ((token = strsep(&here, ",;:"))) { if ((pid_t) atoi(token) > 0) rc_stringlist_addu(omits, token); else { eerror("Invalid omit pid value %s", token); usage(EXIT_FAILURE); } } break; case_RC_COMMON_GETOPT } } if (argc > optind) { arg = argv[optind]; sig = atoi(arg); if (sig <= 0 || sig > 31) { rc_stringlist_free(omits); eerror("Invalid signal %s", arg); usage(EXIT_FAILURE); } } openlog(applet, LOG_CONS|LOG_PID, LOG_DAEMON); if (mount_proc() != 0) { rc_stringlist_free(omits); eerrorx("Unable to mount /proc file system"); } signal_processes(sig, omits, dryrun); rc_stringlist_free(omits); return 0; }
/* Get a listener socket linked to an address */ static int getaddrlistener(const char *address, const char *service, int family) { struct addrinfo hints; struct addrinfo *servinfos; struct addrinfo *servinfo; int reuseaddr = 1; int ret; memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; if(address == NULL) hints.ai_flags = AI_PASSIVE; if((ret = getaddrinfo(address, service, &hints, &servinfos)) != 0) { error("Failed to get address info: %s", gai_strerror(ret)); return -1; } for(servinfo = servinfos; servinfo != NULL; servinfo = servinfo->ai_next) { if((ret = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0) { eerror("Failed to create socket"); continue; } if(setsockopt(ret, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) < 0) { eerror("Failed to set socket option: SO_REUSEADDR"); continue; } if(bind(ret, servinfo->ai_addr, servinfo->ai_addrlen) < 0) { close(ret); eerror("Failed to bind socket"); continue; } if(listen(ret, SOMAXCONN) < 0) { close(ret); eerror("Failed to listen"); return -1; } freeaddrinfo(servinfos); info("Listening on %s:%s", (address == NULL) ? "*" : address, service); return ret; } freeaddrinfo(servinfos); error("Failed to get socket"); return -1; }
int main (){ int i,j; shmid = shmget(SHMKEY, 40, 0666|IPC_CREAT); if(shmid == -1) { eerror("shmget() error"); } sbuf = (char *)shmat(shmid, NULL, 0); if(sbuf == (char *)-1) { eerror("shmat() error"); } shmid1 = shmget(SHMKEY1, 40, 0666|IPC_CREAT); if(shmid1 == -1) { eerror("shmget() error"); } int * serv_id = (int *)shmat(shmid1, NULL, 0); if(serv_id == (int *)-1) { eerror("shmat() error"); } for(i=0; i<10; i++){ *(serv_id+i) = 1; } mkfifo(sfifo, 0666); fd = open(sfifo, O_RDONLY); if(fd == -1) eerror("sfifo error"); while(1){ memset(buf, 128, '\0'); if(read(fd, buf, 128) == 0){ sleep(0.5); continue; } printf("Got a req %s\n", buf); strtok_r(buf, " ", &args); for(i=0; i<3; i++){ if(*(serv_id+5+i) == 0){ *(serv_id+5+i) = 1; sprintf(sbuf, "%s", buf); kill(*(serv_id+i), SIGUSR1); break; } } if(i == 3) { if(kill(atoi(args), SIGTERM) != -1) printf("No Service.. closing client.\n"); else { printf("%d\n", atoi(args)); } } nClient++; printf("Req success %d\n", nClient); } return 0; }
void acceptReq(const char arg[]){ printf("Got a req %s\n", arg); sprintf(tmp, "%sr.dat", arg); wfd[nClient] = open(tmp, O_WRONLY); if(wfd[nClient] == -1) eerror("WERROR!!!"); sprintf(tmp, "%sw.dat", arg); rfd[nClient] = open(tmp, O_RDONLY); if(rfd[nClient] == -1) eerror("RERROR!!!"); nClient++; printf("Req success %d\n", nClient); }
int main(int argc, char const *argv[]) { struct sockaddr_in s_addr, c_addr; int sfd, _sfd, cli_len, i, n, port; if(argc == 1) { eerror("Usage: ./service <port>") } else port = atoi(argv[1]); memset((char *) &s_addr, 0, sizeof(s_addr)); if ((sfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) eerror("socket() error"); s_addr.sin_family = AF_INET; s_addr.sin_port = htons((u_short) port); s_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if( bind(sfd, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0) eerror("bind() error"); if(listen(sfd, 5) < 0) eerror("listen() error"); printf("Service Listening on port %d ...\n", port); while(1) { cli_len = sizeof(c_addr); if((_sfd = accept(sfd, (struct sockaddr *) &c_addr, &cli_len)) < 0) eerror("accept() error"); if(fork() == 0) { close(sfd); dup2(_sfd, 0); dup2(_sfd, 1); while(1) { memset(buffer, '\0', BUFSIZE); read(0, buffer, BUFSIZE); if(strcmp(buffer, "exit") == 0) break; for (i = 0; i < strlen(buffer); ++i) { if(islower(buffer[i])) buffer[i] = buffer[i]-32; } write(1, buffer, strlen(buffer)); } close(_sfd); exit(1); } nClient++; printf("Client %d connected. !!\n", nClient); } return 0; }
void sharedMem() { shmid = shmget(0x124, 128, IPC_CREAT|0666); if(shmid == -1) eerror("shmget() error"); aval = (int *)shmat(shmid, NULL, 0); if(aval == (int *)-1) eerror("shmat() error"); shmid1 = shmget(0x123, 128, IPC_CREAT|0666); if(shmid1 == -1) eerror("shmget() error"); shm = (char *)shmat(shmid1, NULL, 0); if(shm == (char *)-1) eerror("shmat() error"); }
int main (int argc, char * argv[]){ if(argc < 3) eerror("Usage: ./client <service-name> <fifo-prefix>"); char buf[512], tmp[128]; strcpy(buf, argv[1]); strcat(buf, " "); strcat(buf, argv[2]); int fd = open(sfifo, O_WRONLY); write(fd, buf, strlen(buf)); close(fd); strcpy(tmp, argv[2]); strcat(tmp, "r.dat"); mkfifo(tmp, 0666); strcpy(buf, argv[2]); strcat(buf, "w.dat"); mkfifo(buf, 0666); int fd_r = open(tmp, O_RDONLY); if(fd_r == -1) eerror("fd_r error"); printf("open %s\n", tmp); int fd_w = open(buf, O_WRONLY); if(fd_w == -1) eerror("fd_w error"); printf("open %s\n", buf); int pid; pid = fork(); if(pid >0) { // parent process close(fd_r); while(1){ memset(tmp, 128, '\0'); scanf("%s", tmp); write(fd_w, tmp, strlen(tmp)+1); } } else { // child process close(fd_w); while(1){ memset(buf, 512, '\0'); read(fd_r, buf, 512); printf("%s\n", buf); } } return 0; }
/* Authenticates the user, returns 0 on success, 1 on fail */ static int check_auth() { struct passwd *pw; uid_t uid; #ifdef HAVE_AUDIT uid = audit_getloginuid(); if (uid == (uid_t) -1) uid = getuid(); #else uid = getuid(); #endif pw = getpwuid(uid); if (!pw) { eerror("cannot find your entry in the passwd file."); return (-1); } printf("Authenticating %s.\n", pw->pw_name); /* do the actual check */ if (check_password(pw->pw_name) == 0) { return 0; } eerrorx("Authentication failed for %s", pw->pw_name); return 1; }
static void clean_failed(void) { DIR *dp; struct dirent *d; size_t l; char *path; /* Clean the failed services state dir now */ if ((dp = opendir(RC_SVCDIR "/failed"))) { while ((d = readdir(dp))) { if (d->d_name[0] == '.' && (d->d_name[1] == '\0' || (d->d_name[1] == '.' && d->d_name[2] == '\0'))) continue; l = strlen(RC_SVCDIR "/failed/") + strlen(d->d_name) + 1; path = xmalloc(sizeof(char) * l); snprintf(path, l, RC_SVCDIR "/failed/%s", d->d_name); if (path) { if (unlink(path)) eerror("%s: unlink `%s': %s", applet, path, strerror(errno)); free(path); } } closedir(dp); } }
int main (){ mkfifo(sfifo, 0666); int i,j; fd = open(sfifo, O_RDONLY); if(fd == -1) eerror("sfifo error"); while(1){ memset(buf, 128, '\0'); if(read(fd, buf, 128) == 0){ sleep(0.5); continue; } printf("Got a req %s\n", buf); strtok_r(buf, " ", &args); strcpy(service, "./"); strcat(service, buf); if(fork() > 0){ printf("%s %s\n", buf, args); execlp(service, service, args, NULL); } nClient++; printf("Req success %d\n", nClient); } return 0; }
static char read_key(bool block) { struct termios termios; char c = 0; int fd = STDIN_FILENO; if (!isatty(fd)) return false; /* Now save our terminal settings. We need to restore them at exit as we will be changing it for non-blocking reads for Interactive */ if (!termios_orig) { termios_orig = xmalloc(sizeof(*termios_orig)); tcgetattr(fd, termios_orig); } tcgetattr(fd, &termios); termios.c_lflag &= ~(ICANON | ECHO); if (block) termios.c_cc[VMIN] = 1; else { termios.c_cc[VMIN] = 0; termios.c_cc[VTIME] = 0; } tcsetattr(fd, TCSANOW, &termios); if (read(fd, &c, 1) == -1) eerror("read: %s", strerror(errno)); tcsetattr(fd, TCSANOW, termios_orig); return c; }
static int do_mount(struct ENT *ent) { char *argv[8]; pid_t pid; int status; argv[0] = UNCONST("mount"); argv[1] = UNCONST("-o"); argv[2] = ENT_OPTS(*ent); argv[3] = UNCONST("-t"); argv[4] = ENT_TYPE(*ent); argv[5] = ENT_BLOCKDEVICE(*ent); argv[6] = ENT_FILE(*ent); argv[7] = NULL; switch (pid = vfork()) { case -1: eerrorx("%s: vfork: %s", applet, strerror(errno)); /* NOTREACHED */ case 0: execvp(argv[0], argv); eerror("%s: execv: %s", applet, strerror(errno)); _exit(EXIT_FAILURE); /* NOTREACHED */ default: waitpid(pid, &status, 0); if (WIFEXITED(status)) return WEXITSTATUS(status); else return -1; /* NOTREACHED */ } }
static void write_log(int logfd, const char *buffer, size_t bytes) { const char *p = buffer; while ((size_t)(p - buffer) < bytes) { switch (*p) { case '\r': goto cont; case '\033': in_escape = true; in_term = false; goto cont; case '\n': in_escape = in_term = false; break; case '[': if (in_escape) in_term = true; break; } if (!in_escape) { if (write(logfd, p++, 1) == -1) eerror("write: %s", strerror(errno)); continue; } if (! in_term || isalpha((unsigned char)*p)) in_escape = in_term = false; cont: p++; } }
int main(int argc,char *argv[]) { //pthread_t pread,pwrite; if(argc == 1) eerror("Usage: ./client <unique-name>"); // mkfifo(sfifo,0666); strcpy(tmp, argv[1]); strcat(tmp, "r"); mkfifo(tmp, 0666); strcpy(buf, argv[1]); strcat(buf, "w"); mkfifo(buf, 0666); char str[128]; strcpy(str,argv[1]); // strcat(str," "); // strcat(str,argv[2]); // printf("%s\n", str ); int fd = open(sfifo, O_WRONLY|O_NONBLOCK); write(fd, str , strlen(str)); close(fd); fd_r = open(tmp, O_RDONLY); if(fd_r == -1) eerror("fd_r error"); printf("%s%d\n",tmp,fd_r ); printf("open %s\n", tmp); fd_w = open(buf, O_WRONLY); if(fd_w == -1) eerror("fd_w error"); printf("open %s\n", buf); pthread_t A,B; pthread_create(&A, NULL, (void*)&fun1, NULL); pthread_create(&B, NULL, (void*)&fun2, NULL); pthread_join(A, NULL); pthread_join(B, NULL); return 0; }
static void handle_signal(int sig) { int status; int serrno = errno; char *signame = NULL; switch (sig) { case SIGINT: if (!signame) xasprintf(&signame, "SIGINT"); /* FALLTHROUGH */ case SIGTERM: if (!signame) xasprintf(&signame, "SIGTERM"); /* FALLTHROUGH */ case SIGQUIT: if (!signame) xasprintf(&signame, "SIGQUIT"); eerrorx("%s: caught %s, aborting", applet, signame); /* NOTREACHED */ case SIGCHLD: for (;;) { if (waitpid(-1, &status, WNOHANG) < 0) { if (errno != ECHILD) eerror("%s: waitpid: %s", applet, strerror(errno)); break; } } break; default: eerror("%s: caught unknown signal %d", applet, sig); } /* free signame */ free(signame); /* Restore errno */ errno = serrno; }
int getintflistener(const char *interface, const char *service, int family) { struct ifaddrs *ifaddrs; struct ifaddrs *ifaddr; char address[NI_MAXHOST]; int iffamily; int ret; if(strcmp(interface, WILDCARD_INTERFACE) == 0) { debug("Getting listener for wildcard interface"); return getaddrlistener(NULL, service, family); } if(getifaddrs(&ifaddrs) < 0) { eerror("Failed to get interface info"); return -1; } for(ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next) { if(ifaddr->ifa_addr == NULL) continue; iffamily = ifaddr->ifa_addr->sa_family; if((family == AF_INET && iffamily != AF_INET) || (family == AF_INET6 && iffamily != AF_INET6) || (family == AF_UNSPEC && iffamily != AF_INET && iffamily != AF_INET6)) continue; if((ret = getnameinfo(ifaddr->ifa_addr, (iffamily == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) < 0) { error("Failed to get interface name info: %s", gai_strerror(ret)); continue; } if(strcmp(interface, ifaddr->ifa_name) == 0) { if((ret = getaddrlistener(address, service, iffamily)) < 0) { debug("Failed to get listener from address %s", address); continue; } freeifaddrs(ifaddrs); return ret; } } freeifaddrs(ifaddrs); error("Failed to get interface address"); return -1; }
void * writer(void * args) { char buf[BUFSIZE]; int n,sfd = *(int *) args; while(1) { fgets(buf, BUFSIZE, stdin); write(sfd, buf, BUFSIZE); if (atoi(buf) == -1) { eerror("Exiting..."); } } }
int main(int argc, char const *argv[]) { int port, serv_no; char buf[128]; pthread_t r,w; if (argc == 1){ eerror("Usage: ./client <port>"); } else { port = atoi(argv[1]); if(port < 1024 || port > 65535) eerror("Port must between 1024 to 65535"); } struct sockaddr_in s_addr; char buffer[BUFSIZE]; int sfd, n; memset((char *) &s_addr, 0, sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons((u_short) port); s_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) eerror("socket() error1"); if (connect(sfd, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0) eerror("connect() error1"); printf("Type Group Id (port) : "); scanf("%d", &serv_no); sprintf(buf, "%d", serv_no); write(sfd, buf, strlen(buf)); read(sfd, buf, 128); close(sfd); if(strcmp(buf, "exit") == 0) eerror("Service Unavailable !"); if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) eerror("socket() error2"); memset((char *) &s_addr, 0, sizeof(s_addr)); s_addr.sin_family = AF_INET; s_addr.sin_port = htons((u_short) serv_no); s_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); sleep(1); if (connect(sfd, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0) eerror("connect() error2"); printf("connection successfull\n"); pthread_create(&r, NULL, * reader, (void *)&sfd); pthread_create(&w, NULL, * writer, (void *)&sfd); pthread_join(r, NULL); pthread_join(w, NULL); printf("\nExiting ...\n"); close(sfd); return 0; }
RC_DEPTREE * _rc_deptree_load(int force, int *regen) { int fd; int retval; int serrno = errno; int merrno; time_t t; char file[PATH_MAX]; struct stat st; struct utimbuf ut; FILE *fp; t = 0; if (rc_deptree_update_needed(&t, file) || force != 0) { /* Test if we have permission to update the deptree */ fd = open(RC_DEPTREE_CACHE, O_WRONLY); merrno = errno; errno = serrno; if (fd == -1 && merrno == EACCES) return rc_deptree_load(); close(fd); if (regen) *regen = 1; ebegin("Caching service dependencies"); retval = rc_deptree_update() ? 0 : -1; eend (retval, "Failed to update the dependency tree"); if (retval == 0) { stat(RC_DEPTREE_CACHE, &st); if (st.st_mtime < t) { eerror("Clock skew detected with `%s'", file); eerrorn("Adjusting mtime of `" RC_DEPTREE_CACHE "' to %s", ctime(&t)); fp = fopen(RC_DEPTREE_SKEWED, "w"); if (fp != NULL) { fprintf(fp, "%s\n", file); fclose(fp); } ut.actime = t; ut.modtime = t; utime(RC_DEPTREE_CACHE, &ut); } else { if (exists(RC_DEPTREE_SKEWED)) unlink(RC_DEPTREE_SKEWED); } } if (force == -1 && regen != NULL) *regen = retval; } return rc_deptree_load(); }
int main(int argc, char const *argv[]) { if (argc == 1) eerror("Usage: ./service <fifo-prefix>"); char buf[1024], tmp[128]; sprintf(buf, "%sr.dat", argv[1]); int fd_w = open(buf, O_WRONLY); if(fd_w == -1) eerror("WERROR !!"); sprintf(tmp, "%sw.dat", argv[1]); int fd_r = open(tmp, O_RDONLY); if(fd_r == -1) eerror("RERROR !!"); printf("You are good to go.\n"); while(1){ memset(buf, 128, '\0'); if(read(fd_r, buf, 128) == 0){ sleep(0.5); continue; } if(strcmp(buf, "exit") == 0) { printf("Service 1 Exiting . . .\n"); exit(127); } if(fork() > 0) { wait(); } else { memset(buf, 1024, '\0'); write(fd_w, buf, 1024); // for clearing fifo buffers execlp("ls", "ls", buf, NULL); eerror("execlp error"); } } return 0; }
int main() { char buf[30]; printf("this is s1.c\n"); int fd = open(ssfifo,O_WRONLY); if(fd == -1) eerror("ssfifo error"); printf("ssfifo opened in write mode\n"); dup2(fd,1); execlp("ls", "ls", NULL); return 0; }
/* * Read the context from the given context file. context must be free'd by the user. */ static int read_context_file(const char *filename, char **context) { int ret = -1; FILE *fp; char filepath[PATH_MAX]; char *line = NULL; char *p; char *p2; size_t len = 0; ssize_t read; memset(filepath, '\0', PATH_MAX); snprintf(filepath, PATH_MAX - 1, "%s/%s", selinux_contexts_path(), filename); fp = fopen(filepath, "r"); if (fp == NULL) { eerror("Failed to open context file: %s", filename); return -1; } while ((read = getline(&line, &len, fp)) != -1) { /* cut off spaces before the string */ p = line; while (isspace(*p) && *p != '\0') p++; /* empty string, skip */ if (*p == '\0') continue; /* cut off spaces after the string */ p2 = p; while (!isspace(*p2) && *p2 != '\0') p2++; *p2 = '\0'; *context = xstrdup(p); ret = 0; break; } free(line); fclose(fp); return ret; }
static void run_program(const char *prog) { struct sigaction sa; sigset_t full; sigset_t old; pid_t pid; /* We need to block signals until we have forked */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sigemptyset(&sa.sa_mask); sigfillset(&full); sigprocmask(SIG_SETMASK, &full, &old); pid = fork(); if (pid == -1) eerrorx("%s: fork: %s", applet, strerror(errno)); if (pid == 0) { /* Restore default handlers */ sigaction(SIGCHLD, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sigaction(SIGINT, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGUSR1, &sa, NULL); sigaction(SIGWINCH, &sa, NULL); /* Unmask signals */ sigprocmask(SIG_SETMASK, &old, NULL); if (termios_orig) tcsetattr(STDIN_FILENO, TCSANOW, termios_orig); execl(prog, prog, (char *)NULL); eerror("%s: unable to exec `%s': %s", applet, prog, strerror(errno)); _exit(EXIT_FAILURE); } /* Unmask signals and wait for child */ sigprocmask(SIG_SETMASK, &old, NULL); if (rc_waitpid(pid) == -1) eerrorx("%s: failed to exec `%s'", applet, prog); }
void rc_logger_close(void) { int sig = SIGTERM; if (signal_pipe[1] > -1) { if (write(signal_pipe[1], &sig, sizeof(sig)) == -1) eerror("write: %s", strerror(errno)); close(signal_pipe[1]); signal_pipe[1] = -1; } if (rc_logger_pid > 0) waitpid(rc_logger_pid, 0, 0); if (fd_stdout > -1) dup2(fd_stdout, STDOUT_FILENO); if (fd_stderr > -1) dup2(fd_stderr, STDERR_FILENO); }
static size_t get_krunlevel(char *buffer, int buffer_len) { FILE *fp; size_t i = 0; if (!exists(RC_KRUNLEVEL)) return 0; if (!(fp = fopen(RC_KRUNLEVEL, "r"))) { eerror("fopen `%s': %s", RC_KRUNLEVEL, strerror(errno)); return 0; } if (fgets(buffer, buffer_len, fp)) { i = strlen(buffer); if (buffer[i - 1] == '\n') buffer[i - 1] = 0; } fclose(fp); return i; }