void mkfifo_in_kernel(fifoStruct * param){ /*TODO*/ /* open 2 file * create 2 semathore * return param * */ int fd1, fd2; fd1 = create_fifo("fifoa"); fd2 = create_fifo("fifob"); param->fd1 = fd1;/*create files / use param->path*/ param->fd2 = fd2; }
bool init_fifos() { if (!create_fifo(arguments->rng_fifo, 0640)) return false; if (!create_fifo(arguments->nrf_fifo, 0640)) return false; if (!create_fifo(arguments->wdt_fifo, 0640)) return false; if (!create_fifo(COMMAND_FIFO, 0644)) return false; return true; }
int main(int argc, char * argv[]) { create_fifo(); say_hello(); fd_set *input_fd; FD_ZERO(input_fd); FD_SET(3,input_fd); FD_SET(4,input_fd); int val; while (true) { select(2,input_fd,NULL,NULL,NULL); if (read(3,&val,4)==4) { printf("%d", val); } else if (read(4,&val,4)==4) { printf("%d",val); } else { break; } } }
int test_pipe_init(void) { if (create_file(LVL_LOW, log_low, NULL) != 0) { return -1; } if (create_file(LVL_HIGH, log_high, NULL) != 0) { return -1; } unlink(low_pipe); unlink(high_pipe); if (create_fifo(LVL_LOW, low_pipe) != 0) { return -1; } if (create_fifo(LVL_HIGH, high_pipe) != 0) { return -1; } }
bool try_create_fifo(const std::string& path) { try { create_fifo(path); return true; } catch (...) { return false; } }
static int run_walkdir(const char *topdir) { int rv; if (create_fifo() == -1) { return (-1); } rv = walkdir(topdir); destroy_fifo(); return (rv); }
void* controller(void* arg){ int fd, n; char* fifoName; info_t* vehicle; pthread_t carParkID; fifoName = (char *) arg; // Creates FIFO if ( (fd = create_fifo(fifoName, O_RDONLY)) == -1 ){ perror(fifoName); pthread_exit(NULL); } printf("Controller %s open!\n", fifoName); while (1) { vehicle = (info_t *) malloc(sizeof(info_t)); // Reads FIFO n = read(fd, vehicle, sizeof(* vehicle)); // If a vehicle is received if (n > 0) { if (vehicle->stopVehicle == 1){ // Stop vehicle --> close controller printf("%s\n", "stopVehicle"); break; } // Creates a new thread for each car to park it if possible else pthread_create(&carParkID, NULL, car_park, vehicle); } } // If an error has ocurred reading the FIFO if (n < 0) perror(fifoName); close(fd); printf("Controller %s closed!\n", fifoName); if (unlink(fifoName) == -1) perror(strcat("Erro unlinking fifo ", fifoName)); pthread_exit(NULL); }
shdata *joinroom(char *name, char *room, int room_size, int *shmid, int *ownNUMBER, char *ownFIFO, int *fd_read, int *isDealer) { shdata *addr; key_t key; int fifo; char fifoplayer0[40] = "FIFO"; key = ftok(room, 0); *shmid = shmget(key, 0, 0); if (*shmid == -1) { printf("\n...Creating...\n"); *isDealer = 1; *shmid = shmget(key, sizeof(shdata), IPC_CREAT | IPC_EXCL | SHM_R | SHM_W); addr = (shdata*) shmat(*shmid, 0, 0); strcpy(addr[0].room, room); addr[0].roomsize = room_size; initalize_data(addr, room_size); strcat(fifoplayer0, name); mkfifo(fifoplayer0, 0666); (*fd_read) = open(fifoplayer0, O_RDONLY | O_NONBLOCK); } else { *isDealer = 0; addr = (shdata*) shmat(*shmid, 0, 0); printf("\n...Joining...\n"); if (addr[0].in == addr[0].nplayers) { addr[0].failed = 1; return addr; } } add_player_to_shdata(addr, name, ownNUMBER); if (*isDealer == 0) fifo = create_fifo(addr, ownFIFO, fd_read); if (fifo == -1) { addr[0].failed = 2; return addr; } return addr; }
int main(int argc, char **argv) { struct sigaction sa; #ifndef __gnu_hurd__ int fd; #endif get_options(argc, argv); sa.sa_flags = 0; sa.sa_handler = handle_signal; (void)sigemptyset(&sa.sa_mask); xsigaction(SIGINT, &sa, NULL); xsigaction(SIGTERM, &sa, NULL); xsigaction(SIGALRM, &sa, NULL); (void)alarm(TIMEOUT); if (access(CLIENT_CONF_FILE, F_OK) == -1) write_file(CLIENT_CONF_FILE, DEFAULT_CLIENT_CONF); if (access(SERVER_CONF_FILE, F_OK) == -1) write_file(SERVER_CONF_FILE, DEFAULT_SERVER_CONF); create_fifo(); xatexit(remove_fifo); /* * Make sure there's a FIFO reader when the server starts up, in order * to avoid having to wait ten seconds until the server notices that we * opened the FIFO for reading (in the cat_fifo() function). GNU Hurd * doesn't like this trick, though. */ #ifndef __gnu_hurd__ fd = open(COMMAND_FILE, O_RDONLY | O_NONBLOCK); #endif run_command(join(SERVER_COMMAND_LINE, getenv("NSCA_SERVER_FLAGS"))); xatexit(kill_server); run_command(join(CLIENT_COMMAND_LINE, getenv("NSCA_CLIENT_FLAGS"))); cat_fifo(expected_num_lines); #ifndef __gnu_hurd__ (void)close(fd); #endif return EXIT_SUCCESS; }
int main(int argc, char **argv) { int childpid; int sfd = -1; int fd; char *p, *ptyslave=NULL; int i = 1; int off_argv; int calculated_pipename = 0; int highest_pipe_num = 0; program_name = argv[0]; if(argc<4) { usage(argv[0]); exit(1); } init_outbuf(); if (!strcmp(argv[1],"-daemon")) { daemon_init(); ++i; } off_argv = i; strn_cpy(pipename, sizeof(pipename), argv[i++]); strn_cpy(log_dir, sizeof(log_dir), argv[i]); strn_cpy(statusfile, sizeof(statusfile), log_dir); strn_cat(statusfile, sizeof(statusfile), STATUSFILENAME); #ifdef DEBUG status("%s: pid is : %d\n", argv[0], getpid()); #endif /* Get values for LOG file handling from the environment */ if ((p = getenv("RUN_ERL_LOG_ALIVE_MINUTES"))) { log_alive_minutes = atoi(p); if (!log_alive_minutes) { ERROR1(LOG_ERR,"Minimum value for RUN_ERL_LOG_ALIVE_MINUTES is 1 " "(current value is %s)",p); } log_activity_minutes = log_alive_minutes / 3; if (!log_activity_minutes) { ++log_activity_minutes; } } if ((p = getenv("RUN_ERL_LOG_ACTIVITY_MINUTES"))) { log_activity_minutes = atoi(p); if (!log_activity_minutes) { ERROR1(LOG_ERR,"Minimum value for RUN_ERL_LOG_ACTIVITY_MINUTES is 1 " "(current value is %s)",p); } } if ((p = getenv("RUN_ERL_LOG_ALIVE_FORMAT"))) { if (strlen(p) > ALIVE_BUFFSIZ) { ERROR1(LOG_ERR, "RUN_ERL_LOG_ALIVE_FORMAT can contain a maximum of " "%d characters", ALIVE_BUFFSIZ); } strn_cpy(log_alive_format, sizeof(log_alive_format), p); } else { strn_cpy(log_alive_format, sizeof(log_alive_format), DEFAULT_LOG_ALIVE_FORMAT); } if ((p = getenv("RUN_ERL_LOG_ALIVE_IN_UTC")) && strcmp(p,"0")) { ++log_alive_in_gmt; } if ((p = getenv("RUN_ERL_LOG_GENERATIONS"))) { log_generations = atoi(p); if (log_generations < LOG_MIN_GENERATIONS) ERROR1(LOG_ERR,"Minimum RUN_ERL_LOG_GENERATIONS is %d", LOG_MIN_GENERATIONS); if (log_generations > LOG_MAX_GENERATIONS) ERROR1(LOG_ERR,"Maximum RUN_ERL_LOG_GENERATIONS is %d", LOG_MAX_GENERATIONS); } if ((p = getenv("RUN_ERL_LOG_MAXSIZE"))) { log_maxsize = atoi(p); if (log_maxsize < LOG_MIN_MAXSIZE) ERROR1(LOG_ERR,"Minimum RUN_ERL_LOG_MAXSIZE is %d", LOG_MIN_MAXSIZE); } /* * Create FIFOs and open them */ if(*pipename && pipename[strlen(pipename)-1] == '/') { /* The user wishes us to find a unique pipe name in the specified */ /* directory */ DIR *dirp; struct dirent *direntp; calculated_pipename = 1; dirp = opendir(pipename); if(!dirp) { ERRNO_ERR1(LOG_ERR,"Can't access pipe directory '%s'.", pipename); exit(1); } /* Check the directory for existing pipes */ while((direntp=readdir(dirp)) != NULL) { if(strncmp(direntp->d_name,PIPE_STUBNAME,PIPE_STUBLEN)==0) { int num = atoi(direntp->d_name+PIPE_STUBLEN+1); if(num > highest_pipe_num) highest_pipe_num = num; } } closedir(dirp); strn_catf(pipename, sizeof(pipename), "%s.%d", PIPE_STUBNAME, highest_pipe_num+1); } /* if */ for(;;) { /* write FIFO - is read FIFO for `to_erl' program */ strn_cpy(fifo1, sizeof(fifo1), pipename); strn_cat(fifo1, sizeof(fifo1), ".r"); if (create_fifo(fifo1, PERM) < 0) { ERRNO_ERR1(LOG_ERR,"Cannot create FIFO %s for writing.", fifo1); exit(1); } /* read FIFO - is write FIFO for `to_erl' program */ strn_cpy(fifo2, sizeof(fifo2), pipename); strn_cat(fifo2, sizeof(fifo2), ".w"); /* Check that nobody is running run_erl already */ if ((fd = sf_open(fifo2, O_WRONLY|DONT_BLOCK_PLEASE, 0)) >= 0) { /* Open as client succeeded -- run_erl is already running! */ sf_close(fd); if (calculated_pipename) { ++highest_pipe_num; strn_catf(pipename, sizeof(pipename), "%s.%d", PIPE_STUBNAME, highest_pipe_num+1); continue; } fprintf(stderr, "Erlang already running on pipe %s.\n", pipename); exit(1); } if (create_fifo(fifo2, PERM) < 0) { ERRNO_ERR1(LOG_ERR,"Cannot create FIFO %s for reading.", fifo2); exit(1); } break; } /* * Open master pseudo-terminal */ if ((mfd = open_pty_master(&ptyslave, &sfd)) < 0) { ERRNO_ERR0(LOG_ERR,"Could not open pty master"); exit(1); } /* * Now create a child process */ if ((childpid = fork()) < 0) { ERRNO_ERR0(LOG_ERR,"Cannot fork"); exit(1); } if (childpid == 0) { /* Child */ sf_close(mfd); /* disassociate from control terminal */ #ifdef USE_SETPGRP_NOARGS /* SysV */ setpgrp(); #elif defined(USE_SETPGRP) /* BSD */ setpgrp(0,getpid()); #else /* POSIX */ setsid(); #endif /* Open the slave pty */ if (sfd < 0) { /* not allocated by open_pty_master */ if ((sfd = open_pty_slave(ptyslave)) < 0) { ERRNO_ERR1(LOG_ERR,"Could not open pty slave '%s'", ptyslave); exit(1); } /* But sfd may be one of the stdio fd's now, and we should be unmodern and not use dup2... */ /* easiest to dup it up... */ while (sfd < 3) { sfd = dup(sfd); } } #if defined(HAVE_OPENPTY) && defined(TIOCSCTTY) else { /* sfd is from open_pty_master * openpty -> fork -> login_tty (forkpty) * * It would be preferable to implement a portable * forkpty instead of open_pty_master / open_pty_slave */ /* login_tty(sfd); <- FAIL */ ioctl(sfd, TIOCSCTTY, (char *)NULL); } #endif #ifndef NO_SYSLOG /* Before fiddling with file descriptors we make sure syslog is turned off or "closed". In the single case where we might want it again, we will open it again instead. Would not want syslog to go to some other fd... */ if (run_daemon) { closelog(); } #endif /* Close stdio */ sf_close(0); sf_close(1); sf_close(2); if (dup(sfd) != 0 || dup(sfd) != 1 || dup(sfd) != 2) { status("Cannot dup\n"); } sf_close(sfd); exec_shell(argv+off_argv); /* exec_shell expects argv[2] to be */ /* the command name, so we have to */ /* adjust. */ } else { /* Parent */ /* Ignore the SIGPIPE signal, write() will return errno=EPIPE */ struct sigaction sig_act; sigemptyset(&sig_act.sa_mask); sig_act.sa_flags = 0; sig_act.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sig_act, (struct sigaction *)NULL); sigemptyset(&sig_act.sa_mask); sig_act.sa_flags = SA_NOCLDSTOP; sig_act.sa_handler = catch_sigchild; sigaction(SIGCHLD, &sig_act, (struct sigaction *)NULL); /* * read and write: enter the workloop */ pass_on(childpid); } return 0; } /* main() */
rtems_task Init( rtems_task_argument argument ) { void *alloc_ptr = (void *)0; int num_opens = 0; int index = 0; TEST_BEGIN(); puts( "Creating all barriers" ); create_all_barriers(); puts( "Creating all semaphores" ); create_all_semaphores(); puts( "Creating FIFO" ); create_fifo(); puts( "Opening FIFO.. expect ENOMEM (semaphore for pipe could not be created)" ); open_fifo(ENOMEM, O_RDWR); delete_semaphore(); alloc_ptr = malloc( malloc_free_space() - 4 ); puts("Opening FIFO.. expect ENOMEM since no memory is available"); open_fifo(ENOMEM, O_RDWR); free(alloc_ptr); puts( "Opening FIFO.. expect ENOMEM (barrier-1 for pipe could not be created)" ); open_fifo(ENOMEM, O_RDWR); delete_barrier(); puts( "Opening FIFO.. expect ENOMEM (barrier-2 for pipe could not be created" ); open_fifo(ENOMEM, O_RDWR); delete_barrier(); puts( "Opening FIFO.. expect ENOMEM (semaphore-1 for pipe could not be created" ); open_fifo(ENOMEM, O_RDWR); delete_semaphore(); puts( "Opening FIFO in RDWR mode. Expect OK" ); open_fifo(0, O_RDWR); ++num_opens; puts( "Opening FIFO in non blocking RDONLY mode. Expect OK"); open_fifo(0, O_RDONLY | O_NONBLOCK); ++num_opens; puts( "Opening FIFO in non blocking WRONLY mode. Expect ENXIO"); open_fifo(ENXIO, O_WRONLY | O_NONBLOCK); ++num_opens; puts("\nMultiple opens\n"); index = 0; do { printf("%d... ", index+1); open_fifo(0, O_RDONLY | O_NONBLOCK); ++index; } while ( index < NUM_OPEN_REQ - num_opens ); TEST_END(); rtems_test_exit(0); }
int caps_actually_set_test(void) { int whichcap, finalret = 0, ret; cap_t fcap, pcap, cap_fullpi; cap_value_t capvalue[1]; int i; fcap = cap_init(); pcap = cap_init(); if (!fcap || !pcap) { perror("cap_init"); exit(2); } create_fifo(); int num_caps; for (num_caps = 0;; num_caps++) { ret = prctl(PR_CAPBSET_READ, num_caps); /* * Break from the loop in this manner to avoid incrementing, * then having to decrement value. */ if (ret == -1) break; } /* first, try each bit in fP (forced) with fE on and off. */ for (whichcap = 0; whichcap < num_caps; whichcap++) { /* * fP=whichcap, fE=fI=0 * pP'=whichcap, pI'=pE'=0 */ capvalue[0] = whichcap; cap_clear(fcap); cap_set_flag(fcap, CAP_PERMITTED, 1, capvalue, CAP_SET); ret = cap_set_file(TSTPATH, fcap); if (ret) { tst_resm(TINFO, "%d\n", whichcap); continue; } ret = fork_drop_and_exec(DROP_PERMS, fcap); if (ret) { tst_resm(TINFO, "Failed CAP_PERMITTED=%d CAP_EFFECTIVE=0\n", whichcap); if (!finalret) finalret = ret; } /* SERGE here */ /* * fP = fE = whichcap, fI = 0 * pP = pE = whichcap, pI = 0 */ cap_clear(fcap); cap_set_flag(fcap, CAP_PERMITTED, 1, capvalue, CAP_SET); cap_set_flag(fcap, CAP_EFFECTIVE, 1, capvalue, CAP_SET); ret = cap_set_file(TSTPATH, fcap); if (ret) { tst_resm(TINFO, "%d\n", whichcap); continue; } ret = fork_drop_and_exec(DROP_PERMS, fcap); if (ret) { tst_resm(TINFO, "Failed CAP_PERMITTED=%d CAP_EFFECTIVE=1\n", whichcap); if (!finalret) finalret = ret; } } cap_free(pcap); cap_free(fcap); cap_fullpi = cap_init(); for (i = 0; i < num_caps; i++) { capvalue[0] = i; cap_set_flag(cap_fullpi, CAP_INHERITABLE, 1, capvalue, CAP_SET); } /* * For the inheritable tests, we want to make sure pI starts * filled. */ ret = cap_set_proc(cap_fullpi); if (ret) tst_resm(TINFO, "Could not fill pI. pI tests will fail.\n"); /* * next try each bit in fI * The first two attemps have the bit which is in fI in pI. * This should result in the bit being in pP'. * If fE was set then it should also be in pE'. * The last attempt starts with an empty pI. * This should result in empty capability, as there were * no bits to be inherited from the original process. */ for (whichcap = 0; whichcap < num_caps; whichcap++) { cap_t cmpcap; capvalue[0] = whichcap; /* * fI=whichcap, fP=fE=0 * pI=full * pI'=full, pP'=whichcap, pE'=0 */ /* fill pI' */ pcap = cap_dup(cap_fullpi); /* pP' = whichcap */ cap_set_flag(pcap, CAP_PERMITTED, 1, capvalue, CAP_SET); /* fI = whichcap */ fcap = cap_init(); cap_set_flag(fcap, CAP_INHERITABLE, 1, capvalue, CAP_SET); ret = cap_set_file(TSTPATH, fcap); if (ret) { tst_resm(TINFO, "%d\n", whichcap); continue; } ret = fork_drop_and_exec(KEEP_PERMS, pcap); if (ret) { tst_resm(TINFO, "Failed with_perms CAP_INHERITABLE=%d " "CAP_EFFECTIVE=0\n", whichcap); if (!finalret) finalret = ret; } /* * fI=fE=whichcap, fP=0 * pI=full * pI'=full, pP'=whichcap, pE'=whichcap * * Note that only fE and pE' change, so keep prior * fcap and pcap and set those bits. */ cap_set_flag(fcap, CAP_EFFECTIVE, 1, capvalue, CAP_SET); cap_set_flag(pcap, CAP_EFFECTIVE, 1, capvalue, CAP_SET); ret = cap_set_file(TSTPATH, fcap); if (ret) { tst_resm(TINFO, "%d\n", whichcap); continue; } /* The actual result will be a full pI, with * pE and pP containing just whichcap. */ cmpcap = cap_dup(cap_fullpi); cap_set_flag(cmpcap, CAP_PERMITTED, 1, capvalue, CAP_SET); cap_set_flag(cmpcap, CAP_EFFECTIVE, 1, capvalue, CAP_SET); ret = fork_drop_and_exec(KEEP_PERMS, cmpcap); cap_free(cmpcap); if (ret) { tst_resm(TINFO, "Failed with_perms CAP_INHERITABLE=%d " "CAP_EFFECTIVE=1\n", whichcap); if (!finalret) finalret = ret; } /* * fI=fE=whichcap, fP=0 (so fcap is same as before) * pI=0 (achieved using DROP_PERMS) * pI'=pP'=pE'=0 */ cap_clear(pcap); ret = fork_drop_and_exec(DROP_PERMS, pcap); if (ret) { tst_resm(TINFO, "Failed without_perms CAP_INHERITABLE=%d", whichcap); if (!finalret) finalret = ret; } cap_free(fcap); cap_free(pcap); } cap_free(cap_fullpi); return finalret; }
int main (int argc, char* argv[]) { gtk_init (&argc, &argv); if (!g_thread_supported ()) g_thread_init (NULL); printf("Uzbl start location: %s\n", argv[0]); strcpy(executable_path,argv[0]); strcat ((char *) XDG_CONFIG_HOME_default, getenv ("HOME")); strcat ((char *) XDG_CONFIG_HOME_default, "/.config"); GError *error = NULL; GOptionContext* context = g_option_context_new ("- some stuff here maybe someday"); g_option_context_add_main_entries (context, entries, NULL); g_option_context_add_group (context, gtk_get_option_group (TRUE)); g_option_context_parse (context, &argc, &argv, &error); /* initialize hash table */ bindings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_action); soup_session = webkit_get_default_session(); keycmd = g_string_new(""); settings_init (); commands_hash (); if (always_insert_mode) insert_mode = TRUE; GtkWidget* vbox = gtk_vbox_new (FALSE, 0); if (status_top) gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0); if (!status_top) gtk_box_pack_start (GTK_BOX (vbox), create_mainbar (), FALSE, TRUE, 0); main_window = create_window (); gtk_container_add (GTK_CONTAINER (main_window), vbox); load_uri (web_view, uri); gtk_widget_grab_focus (GTK_WIDGET (web_view)); gtk_widget_show_all (main_window); xwin = GDK_WINDOW_XID (GTK_WIDGET (main_window)->window); printf("window_id %i\n",(int) xwin); printf("pid %i\n", getpid ()); printf("name: %s\n", instance_name); scbar_v = (GtkScrollbar*) gtk_vscrollbar_new (NULL); bar_v = gtk_range_get_adjustment((GtkRange*) scbar_v); scbar_h = (GtkScrollbar*) gtk_hscrollbar_new (NULL); bar_h = gtk_range_get_adjustment((GtkRange*) scbar_h); gtk_widget_set_scroll_adjustments ((GtkWidget*) web_view, bar_h, bar_v); if (!show_status) gtk_widget_hide(mainbar); if (fifo_dir) create_fifo (); if (socket_dir) create_socket(); gtk_main (); g_string_free(keycmd, TRUE); if (fifo_dir) unlink (fifo_path); if (socket_dir) unlink (socket_path); g_hash_table_destroy(bindings); g_hash_table_destroy(commands); return 0; }
int proxy_server() { int rx_bytes = 0, post_data_size = 0; int lsock = 0, csock = 0, client_data_size = 0; int result_size = 0, err_code = 0, id = 0; int addrlen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; char *buffer = NULL, *post_data = NULL; char *host = NULL, *url = NULL; char *query = NULL, *fifo_file = NULL; char *get_data = NULL; char *client_data = NULL, *headers = NULL; char *server_ip = NULL; memset((void *) &clientaddr,0,addrlen); server_ip = config_get_server_ip(); /* Create TCP socket */ if((lsock = create_socket(server_ip,PROXY_PORT,SOCK_STREAM)) == SOCK_FAIL) { glog("Failed to create TCP socket for proxy server",LOG_ERROR_TYPE); if(server_ip) free(server_ip); return EXIT_FAILURE; } if(server_ip) free(server_ip); /* Infinite receive loop */ while(1) { /* Accept incoming connection */ if((csock = accept(lsock,(struct sockaddr *) &clientaddr,(socklen_t *) &addrlen)) < 0) { glog("Failed to accept TCP connection to proxy server",LOG_ERROR_TYPE); if(buffer) free(buffer); return EXIT_FAILURE; } if(!fork()) { /* Receive client request */ if((buffer = receive(lsock,SOCK_STREAM,&rx_bytes,csock,&clientaddr)) == NULL) { glog("Failed to read data from client request sent to proxy server",LOG_ERROR_TYPE); exit(EXIT_FAILURE); } if(is_using_proxy(buffer)) { /* Get the target's IP address */ host = get_host_name(buffer); /* Get the target URL path */ url = get_url(buffer); /* Get POST data, if any */ post_data = get_post_data(buffer,rx_bytes,&post_data_size); /* Get HTTP headers from request */ headers = get_headers(buffer); /* If the CONSOLE_HOST is requested, then display the Web console interface */ if(memcmp(host,CONSOLE_HOST,CONSOLE_HOST_SIZE) == 0) { show_web_ui(csock,url); close_socket(csock); } else { /* Make sure the requested host is in our clients list */ query = sqlite3_mprintf("SELECT id FROM %s WHERE strftime('%%s',callback_time) >= strftime('%%s','now') AND ip = %Q LIMIT 1",CLIENTS_TABLE,host); sql_exec(query,&result_size,&err_code); sqlite3_free(query); if(result_size > 0) { /* Don't allow requests for filtered file extensions */ if(!is_url_filtered(url)) { fifo_file = create_fifo(host); if(!fifo_file) { glog("Failed to create fifo file",LOG_ERROR_TYPE); } else { /* Insert query into queue table */ query = sqlite3_mprintf("INSERT INTO %s (fifo,host,url,headers,pdata,sent) VALUES (%Q,%Q,%Q,%Q,%Q,0)",QUEUE_TABLE,fifo_file,host,url,headers,post_data); sql_exec(query,&result_size,&err_code); sqlite3_free(query); if(err_code != SQLITE_OK) { sql_log_error(); } else { /* When the client data has returned, the callback server will write the ID of the callback to the FIFO */ id = read_from_fifo(fifo_file); /* Extract the data from the DB */ get_data = sqlite3_mprintf("SELECT rdata FROM %s WHERE id = '%d'",QUEUE_TABLE,id); client_data = sql_exec(get_data,&client_data_size,&err_code); sqlite3_free(get_data); if(err_code != SQLITE_OK) { sql_log_error(); } else { /* Write data to socket */ if(write(csock,client_data,client_data_size) != client_data_size) { glog("Proxy socket write failed",LOG_ERROR_TYPE); } } if(client_data) free(client_data); /* Make sure the fifo gets deleted */ destroy_fifo(fifo_file); } } } } } } /* Exit the child process */ close_socket(csock); if(fifo_file) free(fifo_file); if(buffer) free(buffer); if(host) free(host); if(url) free(url); if(post_data) free(post_data); if(headers) free(headers); exit(EXIT_SUCCESS); } } /* Close up shop */ close_socket(csock); close_socket(lsock); return EXIT_FAILURE; }
/* * w- and r_pipename have to be pre-allocated of atleast FILENAME_MAX size */ int erts_run_erl_open_fifo(char *pipename,char *w_pipename,char *r_pipename) { int calculated_pipename = 0; int highest_pipe_num = 0; int fd; /* * Create FIFOs and open them */ if(*pipename && pipename[strlen(pipename)-1] == '/') { /* The user wishes us to find a unique pipe name in the specified */ /* directory */ DIR *dirp; struct dirent *direntp; calculated_pipename = 1; dirp = opendir(pipename); if(!dirp) { ERRNO_ERR1(LOG_ERR,"Can't access pipe directory '%s'.", pipename); return 1; } /* Check the directory for existing pipes */ while((direntp=readdir(dirp)) != NULL) { if(strncmp(direntp->d_name,PIPE_STUBNAME,PIPE_STUBLEN)==0) { int num = atoi(direntp->d_name+PIPE_STUBLEN+1); if(num > highest_pipe_num) highest_pipe_num = num; } } closedir(dirp); strn_catf(pipename, BUFSIZ, "%s.%d", PIPE_STUBNAME, highest_pipe_num+1); } /* if */ for(;;) { /* write FIFO - is read FIFO for `to_erl' program */ strn_cpy(w_pipename, BUFSIZ, pipename); strn_cat(w_pipename, BUFSIZ, ".r"); if (create_fifo(w_pipename, PERM) < 0) { ERRNO_ERR1(LOG_ERR,"Cannot create FIFO %s for writing.", w_pipename); return 1; } /* read FIFO - is write FIFO for `to_erl' program */ strn_cpy(r_pipename, BUFSIZ, pipename); strn_cat(r_pipename, BUFSIZ, ".w"); /* Check that nobody is running run_erl already */ if ((fd = sf_open(r_pipename, O_WRONLY|DONT_BLOCK_PLEASE, 0)) >= 0) { /* Open as client succeeded -- run_erl is already running! */ sf_close(fd); if (calculated_pipename) { ++highest_pipe_num; strn_catf(pipename, BUFSIZ, "%s.%d", PIPE_STUBNAME, highest_pipe_num+1); continue; } ERROR1(LOG_ERR, "Erlang already running on pipe %s.\n", pipename); unlink(w_pipename); return 1; } if (create_fifo(r_pipename, PERM) < 0) { unlink(w_pipename); ERRNO_ERR1(LOG_ERR,"Cannot create FIFO %s for reading.", r_pipename); return 1; } break; } return 0; }
int main(int argc, char *argv[]) { /* MUTEX */ pthread_mutex_init(&mux, NULL); pthread_mutex_init(&active_thread_mux,NULL); /*SIGNALS*/ /*signal(SIGUSR1,treatment);*/ int old_cancel_type; sigset_t set; sigfillset (&set); sigprocmask(SIG_BLOCK, &set, NULL); /* FIFO */ create_fifo(&front_server, &back_server); fifo_count = 0; item_server * item; /* POOL MANAGER */ pthread_t poolman_t; /* SERVADMIN */ pthread_t servadmin_t; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&old_cancel_type); pthread_cleanup_push(mainthread_kill,(void*)&poolman_t); /* SOCKETS */ int sockfd, newsockfd, portno; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; int n; pid_t pid; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("ERROR on binding"); exit(-1); } listen(sockfd,2); /* 1000 means???*/ clilen = sizeof(cli_addr); threadpool(); /* creates threadpool */ mainthread = pthread_self(); pthread_create(&servadmin_t, NULL, servadmin, NULL); pthread_create(&poolman_t, NULL, manager, NULL); sem_init(&sem_fifo_used, 0, 0); sem_init(&sem_fifo_free, 0, MAX_FIFO); while(1){ sem_wait(&sem_fifo_free); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0){ perror("ERROR on accept"); } item = (item_server*)malloc(1*sizeof(item_server)); item->socket = newsockfd; item->time = time(NULL); pthread_mutex_lock(&mux); /* Entering Critical FIFO Region*/ queue (&front_server ,&back_server, item); sem_post(&sem_fifo_used); fifo_count++; /* Exiting Critical FIFO Region*/ pthread_mutex_unlock(&mux); } close(sockfd); pthread_cleanup_pop(0); return 0; }