bool Calendar::runExternalViewer() { Config* config = Config::getInstance(); size_t len = config->external_viewer.length(); if (len > 0) { int year, month, day; gtk_calendar_get_date((GtkCalendar*)widget, (guint*)&year, (guint*)&month, (guint*)&day); struct tm date; date.tm_year = year - 1900; date.tm_mon = month; date.tm_mday = day; date.tm_sec = date.tm_min = date.tm_hour = date.tm_wday = date.tm_yday = date.tm_isdst = 0; size_t buf_size = len + 64; char* cmd = new char[buf_size]; strftime(cmd, buf_size, config->external_viewer.c_str(), &date); fork_and_run(cmd); delete[] cmd; return true; } else { return false; } }
int main(int argc, char *argv[]) { key_t shmkey, semkey; unsigned short initsemval[1]; if(argc<2) { printf("Usage : %s shmkey semkey\n", argv[0]); exit(1); } shmkey = atoi(argv[1]); semkey = atoi(argv[2]); shmid = shmget(shmkey, 128, IPC_CREAT | 0600); if(shmid < 0 ) errquit("shmget fail"); shm_data = (char *)shmat(shmid, (void *)0, 0); if(shm_data == (char *)-1) errquit("shmat fail"); semid = semget(semkey, 1, IPC_CREAT | 0600); if(semid == -1) errquit("semget fail"); initsemval[0] = 1; semarg.array = initsemval; if(semctl(semid, 0, SETALL, semarg) == -1) errquit("semctl"); fork_and_run(); fork_and_run(); busy(); wait(NULL); wait(NULL); shmctl(shmid, IPC_RMID, 0); semctl(semid, 0, IPC_RMID, 0); return 0; }
int main(){ int prio; int fork_exit_pid[FORK_EXIT_CHILD],get_ioprio_pid[GET_IOPRIO_CHILD],fack_ioprio_pid[FACK_IOPRIO_CHILD]; int pipe_fd[2]; int i,val; if(pipe(pipe_fd) == -1){ perror("pipe"); return -1; } read_fd = pipe_fd[0]; write_fd = pipe_fd[1]; _ioprio_set(IOPRIO_WHO_PROCESS,0,0x6000); for(i = 0; i < FORK_EXIT_CHILD; i++) fork_exit_pid[i] = fork_and_run(FORK_EXIT_LOOP); for(i = 0; i < GET_IOPRIO_CHILD; i++) get_ioprio_pid[i] = fork_and_run(GET_IOPRIO_LOOP); for(i = 0; i < FACK_IOPRIO_CHILD; i++) fack_ioprio_pid[i] = fork_and_run(FACK_IOPRIO_LOOP); if(fork()==0){ time_control(); } read(read_fd,&val,sizeof(val)); printf("read pipe %d\n",val); if(val == 1){ printf("done! vul\n"); }else if(val == -1){ printf("done! no vul\n"); } for(i = 0; i < FORK_EXIT_CHILD; i++) kill(fork_exit_pid[i],SIGKILL); for(i = 0; i < GET_IOPRIO_CHILD; i++) kill(get_ioprio_pid[i],SIGKILL); for(i = 0; i < FACK_IOPRIO_CHILD; i++) kill(fack_ioprio_pid[i],SIGKILL); sleep(1); close(read_fd); close(write_fd); return 0; }
int main() { int p1[2]; if(pipe(p1)) { perror("pipe1"); exit(1); } fork_and_run(p1, 0); }
void fork_and_run(int pipe_in[2], int funcs) { void (*func_list[])(void) = { sed, tr, awk, sort, uniq }; int num_funcs = 4; int new_pipe[2]; if(pipe(new_pipe)) { perror("pipe1"); exit(1); } dup2(new_pipe[0], STDIN_FILENO); if (funcs == num_funcs) { dup2(new_pipe[1], STDOUT_FILENO); } //wait(NULL); if (funcs >= num_funcs) { return; } switch(fork()) { case -1: perror("Error forking"); exit(0); case 0: // child process // 0 is read 1 is write close(new_pipe[0]); dup2(new_pipe[1], STDOUT_FILENO); close(new_pipe[1]); func_list[funcs](); default: // parent process close(new_pipe[1]); if (funcs == num_funcs-4) dup2(new_pipe[0], STDIN_FILENO); close(new_pipe[0]); fork_and_run(new_pipe, ++funcs); break; } }
/* * Interpret the user input as either * a shell command or a program to be run */ void exec_command(char **tokens, int bg) { char *path, *first, **firstparsed, **checkenv, *pager; char *pathenv, *pathcp, **pathtokens; char *tmp_str; int i; pid_t parent; /* copy the first command string and handle * this case specially - it might be a shell command */ first = malloc(sizeof(char)*strlen(tokens[0])+1); strcpy(first, tokens[0]); firstparsed = tokenize(first, ' '); if (strcmp(firstparsed[0], "") == 0) { free(first); free(firstparsed); free(tokens); return; } if (strcmp(firstparsed[0], "exit") == 0) { free(first); free(firstparsed); free(tokens); /* try to terminate all childs before exiting */ /* send term to whole group, but ignore in parent */ register_sighandler(SIGTERM, SIG_IGN); parent = getpid(); sighold(SIGCHLD); /* give processes a chance to finish */ if (kill(-parent, SIGTERM) == -1) { printf("Error: %s\n", strerror(errno)); exit(1); } sleep(1); poll_childs(); sigrelse(SIGCHLD); printf("Bye!\n"); exit(0); } if (strcmp(firstparsed[0], "cd") == 0) { if (firstparsed[1] == '\0') { /* default dir to change to */ path = getenv("HOME"); } else { path = firstparsed[1]; } if (DEBUG) { printf("Change Directory to %s\n", path); } if (chdir(path) == -1) { fprintf(stderr, "Error: %s\n", strerror(errno)); } free(first); free(firstparsed); free(tokens); return; } if (strcmp(firstparsed[0], "checkEnv") == 0) { pager = getenv("PAGER"); if (pager == NULL) { pathenv = getenv("PATH"); pathcp = malloc(strlen(pathenv)+1); strcpy(pathcp, pathenv); pathtokens = tokenize(pathcp, ':'); for (i = 0; i < tokens_length(pathtokens); i++) { if((tmp_str = malloc(strlen(pathtokens[i])+strlen("/less")+1)) != NULL){ tmp_str[0] = '\0'; strcat(tmp_str,pathtokens[i]); strcat(tmp_str,"/less"); if (access(tmp_str, X_OK) != -1) { pager = "less"; free(tmp_str); break; } free(tmp_str); } } if (pager == NULL) pager = "more"; free(pathcp); free(pathtokens); } if (DEBUG) { printf("Pager: %s\n", pager); } /* As our fork_and_run function takes an array of commands, we prepare an array with these commands in the checkenv array */ /* tmp_str will become "grep <arguments>" if any arguments are passed to checkEnv */ tmp_str = malloc(sizeof(char*) * (LIMIT + 1)); if (tokens_length(firstparsed) > 1) { checkenv = malloc(sizeof(char*) * (4 + 1)); tmp_str[0] = '\0'; strcat(tmp_str, "grep "); for (i = 1; firstparsed[i] != NULL; i++) { strcat(tmp_str, firstparsed[i]); strcat(tmp_str, " "); } checkenv[0] = "printenv"; checkenv[1] = tmp_str; checkenv[2] = "sort"; checkenv[3] = pager; checkenv[4] = NULL; } else { checkenv = malloc(sizeof(char*) * (3 + 1)); checkenv[0] = "printenv"; checkenv[1] = "sort"; checkenv[2] = pager; checkenv[3] = NULL; } fork_and_run(checkenv, bg); free(tmp_str); free(checkenv); free(first); free(firstparsed); free(tokens); return; } /* what was wrote wasn't "cd", "exit" or "checkEnv". We still execute the command. */ free(first); free(firstparsed); fork_and_run(tokens, bg); free(tokens); }