static void recursion(void) { int fd[2], ff[2]; int inflight, openfiles, deferred, deferred1; test = "recursion"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); deferred = getdeferred(); my_socketpair(fd); for (;;) { if (socketpair(PF_UNIX, SOCK_STREAM, 0, ff) == -1) { if (errno == EMFILE || errno == ENFILE) break; err(-1, "socketpair"); } sendfd(ff[0], fd[0]); sendfd(ff[0], fd[1]); close2(fd[1], fd[0]); fd[0] = ff[0]; fd[1] = ff[1]; } close2(fd[0], fd[1]); sleep(1); test_sysctls(inflight, openfiles); deferred1 = getdeferred(); if (deferred != deferred1) errx(-1, "recursion: deferred before %d after %d", deferred, deferred1); }
int main(int argc,char* argv[]) { int usfd=unix_socket("soc.socket"); int sfd=inet_socket(atoi(argv[1])); sockaddr_in cliaddr; sockaddr_un cliuaddr; socklen_t clen,cen; int pid; int nsfd,nusfd; int flag=1; while(1) { //sleep(2); printf("accepting\n"); nsfd=accept(sfd,NULL,NULL); if(flag==1) { flag=0; pid=fork(); if(pid==0) { close(sfd); close(usfd); execlp("./s1","./s1",(char*)0); } else { nusfd=accept(usfd,NULL,NULL); printf("nusfd%d\n",nusfd); sendfd(nusfd,nsfd); close(nsfd); } } else { sendfd(nusfd,nsfd); close(nsfd); } } }
static void process_client(int fd) { static char env[8192]; /* static variables are not on the stack */ static size_t env_len; char reqpath[2048]; const char *errmsg; int i; /* get the request line */ if ((errmsg = http_request_line(fd, reqpath, env, &env_len))) return http_err(fd, 500, "http_request_line: %s", errmsg); for (i = 0; i < nsvcs; ++i) { if (!regexec(&svcurls[i], reqpath, 0, 0, 0)) { warnx("Forward %s to service %d", reqpath, i + 1); break; } } if (i == nsvcs) return http_err(fd, 500, "Error dispatching request: %s", reqpath); if (sendfd(svcfds[i], env, env_len, fd) <= 0) return http_err(fd, 500, "Error forwarding request: %s", reqpath); close(fd); }
void* fdreceiver(void* arg) { fds* f=(fds*)arg; while(1) { int fd=recvfd(f->usfd1); if(fd>=0) { if(n<limit ) { pthread_mutex_lock(&mut); printf("one entering mall2 from mal12\n"); n++; pthread_mutex_unlock(&mut); pthread_t t; data td; td.nsfd=fd; pthread_create(&t,NULL,&service,&td); } else { printf("mall2 full\n"); sendfd(f->usfd2,fd); } } } }
static PyObject *p_sendfd(PyObject *self, PyObject *args) { int sock, fd, ret; PyObject *data; if(!PyArg_ParseTuple(args, "iiO", &sock, &fd, &data)) return(NULL); if(!PyString_Check(data)) { PyErr_SetString(PyExc_TypeError, "datagram must be a string"); return(NULL); } while(1) { Py_BEGIN_ALLOW_THREADS; ret = sendfd(sock, fd, PyString_AsString(data), PyString_Size(data)); Py_END_ALLOW_THREADS; if(ret < 0) { if(errno == EINTR) { if(PyErr_CheckSignals()) return(NULL); continue; } PyErr_SetFromErrno(PyExc_OSError); return(NULL); } Py_RETURN_NONE; } }
static void listen_connect_drop(void) { struct sockaddr_in sin; int slisten, sconnect, sv[2]; int inflight, openfiles; socklen_t len; test = "listen_connect_drop"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); slisten = my_socket(PF_INET, SOCK_STREAM, 0); my_bind(slisten, (struct sockaddr *)&sin, sizeof(sin)); my_listen(slisten, -1); my_socketpair(sv); len = sizeof(sin); my_getsockname(slisten, (struct sockaddr *)&sin, &len); sconnect = my_socket(PF_INET, SOCK_STREAM, 0); setnonblock(sconnect); my_connect(sconnect, (struct sockaddr *)&sin, len); sleep(1); sendfd(sv[0], slisten); close3(slisten, sv[0], sv[1]); sleep(1); close(sconnect); test_sysctls(inflight, openfiles); }
static void on_pad_added (GstElement * element, GstPad * pad, GstElement * pipeline) { GstElement *ipcpipelinesink; GstPad *sinkpad; GstCaps *caps; const GstStructure *structure; GstIterator *it; GValue elem = G_VALUE_INIT; int sockets[2]; gboolean create_sockets; caps = gst_pad_get_current_caps (pad); structure = gst_caps_get_structure (caps, 0); it = gst_bin_iterate_sinks (GST_BIN (pipeline)); if (gst_iterator_find_custom (it, find_ipcpipelinesink, &elem, (gpointer) gst_structure_get_name (structure))) { ipcpipelinesink = g_value_get_object (&elem); create_sockets = FALSE; g_value_reset (&elem); } else { ipcpipelinesink = gst_element_factory_make ("ipcpipelinesink", NULL); gst_bin_add (GST_BIN (pipeline), ipcpipelinesink); create_sockets = TRUE; } sinkpad = gst_element_get_static_pad (ipcpipelinesink, "sink"); if (gst_pad_link (pad, sinkpad) != GST_PAD_LINK_OK) { fprintf (stderr, "Failed to link ipcpipelinesink\n"); exit (1); } gst_object_unref (sinkpad); g_signal_connect (pad, "unlinked", (GCallback) on_pad_unlinked, pipeline); if (create_sockets) { if (socketpair (AF_UNIX, SOCK_STREAM, 0, sockets)) { fprintf (stderr, "Error creating sockets: %s\n", strerror (errno)); exit (1); } if (fcntl (sockets[0], F_SETFL, O_NONBLOCK) < 0 || fcntl (sockets[1], F_SETFL, O_NONBLOCK) < 0) { fprintf (stderr, "Error setting O_NONBLOCK on sockets: %s\n", strerror (errno)); exit (1); } g_object_set (ipcpipelinesink, "fdin", sockets[0], "fdout", sockets[0], NULL); printf ("new socket %d\n", sockets[1]); sendfd (pipes[1], sockets[1]); } gst_element_set_state (ipcpipelinesink, GST_STATE_PLAYING); GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pad.added"); }
int main() { int id=atoi(argv[1]); limit=atoi(argv[2]); int sfd=inet_socket(atoi(argv[3])); int usfd1,usfd2; if(id>1) usfd1=unix_socket(argv[4]);//serv sock if(id==1) { usfd2=socket(AF_UNIX,SOCK_STREAM,0); struct sockaddr_un servaddr; servaddr.sun_family=AF_UNIX; strcpy(servaddr.sun_path,argv[5]); int len=sizeof(sa_familt_t)+strlen(servaddr.sun_path); int re=connect(usfd2,(struct sockaddr*)&servaddr,len); printf("res %d\n",re); } nusfd=accept(usfd1,NULL,NULL); pthread_create(&t,NULL,fdreceiver,&usfd); while(1) { nsfd=accept(nsfd,NULL,NULL); n++; if(n>=limit) { sendfd(nusfd,nsfd); close(nsfd); } if(id==3 && n==limit) { kill(getppid(),SIGUSR1); } else { sendfd(usfd,nsfd); } } }
int main(int argc,char* argv[]) { n=0;limit=1; pthread_mutex_init(&mut,NULL); int sfd=inet_socket(atoi(argv[1])); int usfd1,usfd2; usfd1=unix_socket("soc2");//serv sock int nusfd=accept(usfd1,NULL,NULL); usfd2=socket(AF_UNIX,SOCK_STREAM,0); //cli sock struct sockaddr_un servaddr; servaddr.sun_family=AF_UNIX; strcpy(servaddr.sun_path,"soc3"); int len=sizeof(sa_family_t)+strlen(servaddr.sun_path); int re=connect(usfd2,(struct sockaddr*)&servaddr,len); printf("res %d\n",re); // sleep(2); pthread_t t; fds f; f.usfd1=nusfd; f.usfd2=usfd2; pthread_create(&t,NULL,fdreceiver,&f); while(1) { int nsfd=accept(sfd,NULL,NULL); pthread_mutex_lock(&mut); printf("one entering mall 2\n"); n++; pthread_mutex_unlock(&mut); if(n>limit) { printf("m2 full\n"); sendfd(usfd2,nsfd); close(nsfd); } else { data td; td.nsfd=nsfd; pthread_create(&t,NULL,&service,&td); } } }
int main(int argc,char* argv[]) { pthread_mutex_init(&mut,NULL); n=0;limit=1; int usfd; usfd=socket(AF_UNIX,SOCK_STREAM,0); struct sockaddr_un servaddr; servaddr.sun_family=AF_UNIX; strcpy(servaddr.sun_path,"soc2"); int len=sizeof(sa_family_t)+strlen(servaddr.sun_path); int re=-1; int sfd=inet_socket(atoi(argv[1])); re=connect(usfd,(struct sockaddr*)&servaddr,len); printf("res1 %d\n",re); if(re==-1) { re=connect(usfd,(struct sockaddr*)&servaddr,len); printf("res1 %d\n",re); } while(1) { int nsfd=accept(sfd,NULL,NULL); pthread_mutex_lock(&mut); n++; pthread_mutex_unlock(&mut); printf("one is entring mall1\n"); if(n>limit) { printf("m1 full\n"); sendfd(usfd,nsfd); close(nsfd); } else { data td; td.nsfd=nsfd; pthread_t t; pthread_create(&t,NULL,&service,&td); } } }
static void fivesome_drop_work(const char *testname, int close_spare_after_send, int close_sva_after_send) { int inflight, openfiles; int spare, sva[2], svb[2]; printf("%s\n", testname); test = testname; save_sysctls(&inflight, &openfiles); alloc5fds(&spare, sva, svb); /* * Send spare over sva. */ sendfd(sva[0], spare); if (close_spare_after_send) close(spare); /* * Send sva over svb. */ sendfd(svb[0], sva[0]); sendfd(svb[0], sva[1]); if (close_sva_after_send) close2(sva[0], sva[1]); close2(svb[0], svb[1]); if (!close_sva_after_send) close2(sva[0], sva[1]); if (!close_spare_after_send) close(spare); test_sysctls(inflight, openfiles); }
int main(int argc, char **argv) { if (argc != 3) { err_quite("usage: ./myopenfile <unpath ><filepath>"); } int fd = open(argv[2], O_RDONLY, MODE); if (fd < 0) { err_quite("open file error!"); } struct sockaddr_un unaddr; memset(&unaddr, 0, ADDSIZE); unaddr.sun_family = AF_UNIX; strncpy(unaddr.sun_path, argv[1], sizeof(unaddr.sun_path)); sendfd((struct sockaddr *)&unaddr, fd); exit(0); }
/* * Using a socket pair, send various endpoints over the pair and close in * various orders. */ static void twosome_drop_work(const char *testname, int sendvia, int tosend, int closefirst) { int inflight, openfiles; int sv[2]; printf("%s\n", testname); test = testname; save_sysctls(&inflight, &openfiles); my_socketpair(sv); sendfd(sv[sendvia], sv[tosend]); if (closefirst == 0) close2(sv[0], sv[1]); else close2(sv[1], sv[0]); test_sysctls(inflight, openfiles); }
int main(void) { pid_t pid; int s[2], status, ecode; socketpair(AF_UNIX, SOCK_STREAM, 0, s); pid = fork(); if (pid < 0) { err(1, "fork failed"); } else if (pid == 0) { close(0); close(1); /* close(2); */ int mysecretfd = recvfd(s[0]); write(mysecretfd, "hello secret\n", 14); char fd[8]; snprintf(fd, sizeof(fd), "%d", mysecretfd); if (execl(CHECKFD_PATH, CHECKFD_CMD, fd, NULL) < 0) err(3, "execl failed"); } else { sendfd(s[1], 1); } if (waitpid(pid, &status, 0) < 0) err(1, "waitpid failed"); if (!WIFEXITED(status)) errx(1, CHECKFD_CMD " did not exit"); ecode = WEXITSTATUS(status); if (ecode != 0) { warnx("exit code %d", ecode); abort(); } fprintf(stderr, "passed\n"); exit(0); }
void* fdreceiver(void* arg) { int* usfd=(int*)arg; while(1) { int fd=recvfd(usfd); if(n<limit) { pthread_mutex_lock(&mut); n++; pthread_mutex_unlock(&mut); pthread_t t; pthread_create(&t,NULL,service,&fd); } else { if(id==3) kill(getppid(),SIGUSR1); else sendfd(usfd,fd); } } }
/* * Create a somewhat nasty dual-socket socket intended to upset the garbage * collector if mark-and-sweep is wrong. */ static void complex_cycles(void) { int inflight, openfiles; int spare, sva[2], svb[2]; test = "complex_cycles"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc5fds(&spare, sva, svb); sendfd(sva[0], svb[0]); sendfd(sva[0], svb[1]); sendfd(svb[0], sva[0]); sendfd(svb[0], sva[1]); sendfd(svb[0], spare); sendfd(sva[0], spare); close5(spare, sva[0], sva[1], svb[0], svb[1]); test_sysctls(inflight, openfiles); }
int main() { int fds[2]; int fd = -1; int sock; struct sockaddr_in saddr; int salen; int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); pid_t pid; if (rc) { perror("socketpair"); return 1; } signal(SIGCHLD, sigchld); switch (pid = fork()) { case 0: // standby process close(fds[1]); while (1) { fd = recvfd(fds[0]); if (fd < 0) { perror("recvfd"); exit(0); } printf("recvfd %d\n", fd); rc = write(fd, "[SBY take over]\n", sizeof("[SBY take over]\n")); if (rc < 0) { perror("write"); exit(0); } close(fd); printf("done\n"); } _exit(0); case -1: perror("fork"); return 1; default: ; } sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("socket"); exit(1); } salen = sizeof(saddr); memset(&saddr, 0, salen); rc = bind(sock, &saddr, salen); if (rc < 0) { perror("bind"); exit(1); } rc = getsockname(sock, &saddr, &salen); if (rc < 0) { perror("getsockname"); exit(1); } printf("listening on %d\n", ntohs(saddr.sin_port)); rc = listen(sock, 5); if (rc < 0) { perror("listen"); exit(1); } while (1) { int csock; char buf[512]; int bsiz; csock = accept(sock, &saddr, &salen); if (csock < 0) { perror("accept"); exit(1); } switch (pid = fork()) { case 0: /* active process */ while ((bsiz = read(csock, buf, sizeof(buf))) > 0) { if (bsiz == 0 || buf[0] == '\n' || buf[0] == '\r') { break; } if (buf[0] == '!') { rc = write(csock, "[ACT pass]\n", sizeof("[ACT pass]\n")); if (rc < 0) { perror("write"); exit(1); } /* hand over to standby process */ rc = sendfd(fds[1], csock); if (rc < 0) { perror("sendfd"); exit(1); } printf("[swtich %d]\n", csock); exit(0); } else { rc = write(csock, buf, bsiz); if (rc < 0) { perror("write"); exit(1); } } } case -1: perror("fork"); exit(1); default: close(csock); } } exit(0); }
int main(int argc,char* argv[]) { char buffer[100]; int n=0; struct sigaction s; s.sa_sigaction = &handler; sigemptyset(&s.sa_mask); s.sa_flags =SA_SIGINFO; sigaction (SIGUSR1, &s, NULL); //create platforms for(int i=0;i<3;i++) { pids[i]=fork(); if(pids[i]==0) { char buf[24]; sprintf(buf,"%d",32004+i); execlp("./pf","./pf",buf,(char*)NULL); } } //initially each platform is free for(int i=0;i<3;i++) { idle[i]=1; } int rsfd=raw_socket(143); printf("SM - rsfd:%d\n",rsfd); int sfd[3]; //accept unix domain socket conn from platforms int usfd=unix_socket("soc.socket"); for(int i=0;i<3;i++) { nusfd[i]=accept(usfd,NULL,NULL); printf("nusfd %d : %d\n",i,nusfd[i]); } //tracks for different directions are laid int p[3]={32001,32002,32003}; int maxfd=-1; struct sockaddr_in myaddr[3]; fd_set readset; FD_ZERO(&readset); for(int i=0;i<3;i++) { sfd[i]=inet_serv_socket(p[i]); if(maxfd<sfd[i]) { maxfd=sfd[i]; } } sockaddr_in cliaddr; socklen_t clen; int pid; int nsfd; int pos=-1; while(1) { //poll on tracks from differnt directions for incoming trains FD_ZERO(&readset); for(int i=0;i<3;i++) { if(sfd[i]!=-1) FD_SET(sfd[i],&readset); } select(maxfd+1,&readset,NULL,NULL,NULL); pos=-1; for(int i=0;i<3;i++) { if(FD_ISSET(sfd[i],&readset)) { pos=i; break; } } printf("all running\n"); //TODO:pass annoncements to all platforms //get free platform int r=getfree(); if(r>=0) { int nsfd=accept(sfd[pos],NULL,NULL); //send announcements to all platforms char buff[1000];char tmp[20]; struct sockaddr_in servaddr; servaddr.sin_family=AF_INET; servaddr.sin_port=htons(0); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bzero(buff,1000); bzero(tmp,20); sprintf(tmp,"%d\n",r); strcpy(buff,"a train arriving at platform "); strcat(buff,tmp); sendto(rsfd,buff,1000,0,(struct sockaddr*)&servaddr,sizeof(servaddr)); //mark platform selected busy idle[r]=0; //pass fd to selected platform sendfd(nusfd[r],nsfd); close(nsfd); } } return 0; }
int main () { #if HAVE_SOCKETPAIR int pair[2]; int ret; pid_t pid; int status; int fdnull; int fd; struct stat st; # if HAVE_DECL_ALARM /* Avoid hanging on failure. */ int alarm_value = 5; signal (SIGALRM, SIG_DFL); alarm (alarm_value); # endif fdnull = open ("/dev/null", O_RDWR); if (fdnull < 0) { perror ("Could not open /dev/null"); return 1; } ret = socketpair (AF_UNIX, SOCK_STREAM, 0, pair); if (ret < 0) { perror ("socket pair failed"); return 2; } pid = fork (); if (pid == -1) { perror ("fork"); return 3; } if (pid == 0) { ret = sendfd (pair[1], fdnull); if (ret == -1) { perror ("sendfd"); return 64; } return 0; } /* father */ else { ASSERT (close (pair[1]) == 0); fd = recvfd (pair[0], 0); if (fd == -1) { perror ("recvfd"); return 16; } ret = waitpid (pid, &status, 0); if (ret == -1) { perror ("waitpid"); return 17; } ASSERT (ret == pid); if (!WIFEXITED (status)) { fprintf (stderr, "Child does not normally exit\n"); return 65; } ret = WEXITSTATUS (status); if (ret != 0) { fprintf (stderr, "Send fd fail\n"); return ret; } /* try to stat new fd */ ret = fstat (fd, &st); if (ret < 0) { perror ("fstat"); return 80; } /* Check behavior when sender no longer around */ errno = 0; fd = recvfd (pair[0], 0); ASSERT (fd == -1); ASSERT (errno == ENOTCONN); return 0; } #else errno = 0; ASSERT(sendfd (0, 0) == -1); ASSERT(errno == ENOSYS); errno = 0; ASSERT(recvfd (0, 0) == -1); ASSERT(errno == ENOSYS); fputs ("skipping test: socketpair not supported on this system\n", stderr); return 77; #endif }
int main (int argc, char **argv) { Http_t tree; if (argc<2) die ("server bug"); signal(SIGCHLD, SIG_IGN); // get the pipe fd int pipefd = atoi (argv[1]); if (DEBUG) printf ("pipefd = %d\n", pipefd); // read the two fds from the pipe (sent from the // server) char temp[32]; int file_fd; if (-1==recvfd(pipefd, temp, sizeof(temp), &file_fd)) die ("recv fd"); if (DEBUG) printf ("file_fd = %d\n", file_fd); int bank_fd; char temp2[32]; if (-1==recvfd(pipefd, temp2, sizeof(temp2), &bank_fd)) die ("recv fd"); if (DEBUG) printf ("bank_fd = %d\n", bank_fd); while (1){ char sockfd_str[32]; int sockfd; if (-1==recvfd (pipefd, sockfd_str, sizeof(sockfd_str), &sockfd)) die ("recv fd"); printf ("str=%s\n", sockfd_str); if (DEBUG) printf ("httpd service recieves a sockfd =[%d]\n", sockfd); if(fork() == 0){ // parse the http request ReqLine_t reqline = Parse_parse(sockfd, 1); if (DEBUG) printf ("child-httpd parse reqline:[%s]\n", reqline->uri); // dispatch to various services switch (reqline->kind){ case REQ_KIND_GET:{ sendfd(file_fd , reqline->uri , strlen(reqline->uri)+1 , sockfd); break; } case REQ_KIND_HEAD:{ // unhandled fprintf (stderr, "to do\n"); return; } case REQ_KIND_POST:{ if (DEBUG) printf("%s\n", "httpd dispatch POST."); sendfd(bank_fd , reqline->uri , strlen(reqline->uri)+1 , sockfd); break; } default:{ fprintf (stderr, "error\n"); return; } } exit(0); } // will be closed in various services close(sockfd); } return 0; }
int main(int argc, char ** argv) { struct sockaddr_un address; int socket_fd, connection_fd; socklen_t address_length; int pipefd[2]; int pipe_in[2]; char buffer[256]; char * msg = "Howdy!"; if (forkSingle() < 0) { perror("Didn't fork."); return 1; } // create domain socked socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); if (socket_fd < 0) { perror("Socket creation failed."); return 1; } // should delete previous remnants unlink("./fd_server"); /* start with a clean address structure */ memset(&address, 0, sizeof(struct sockaddr_un)); address.sun_family = AF_UNIX; snprintf(address.sun_path, UNIX_PATH_MAX, "./fd_server"); if(bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) { printf("bind() failed\n"); return 1; } // This program will listen to connections on this socket if(listen(socket_fd, 5) != 0) { printf("listen() failed\n"); return 1; } if ((connection_fd = accept(socket_fd, (struct sockaddr *) &address, &address_length)) > -1) { printf("connection made!\n"); // create a pipe pipe(pipefd); pipe(pipe_in); sendfd(connection_fd, pipefd[0], pipe_in[1]); // send read end to client } printf("Writing to pipe!\n"); // read / write / be happy write(pipefd[1], msg, 7); read(pipe_in[0], buffer, 256); printf("Voter recieved back: %s\n", buffer); close(socket_fd); unlink("./fd_server"); return 0; }
/* * threesome_drop: create a pair and a spare, send the spare over the pair, and * close in various orders and make sure all the fds went away. */ static void threesome_drop(void) { int inflight, openfiles; int s, sv[2]; /* * threesome_drop1: close sent send receive */ test = "threesome_drop1"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); sendfd(sv[0], s); close3(s, sv[0], sv[1]); test_sysctls(inflight, openfiles); /* * threesome_drop2: close sent receive send */ test = "threesome_drop2"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); sendfd(sv[0], s); close3(s, sv[1], sv[0]); test_sysctls(inflight, openfiles); /* * threesome_drop3: close receive sent send */ test = "threesome_drop3"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); sendfd(sv[0], s); close3(sv[1], s, sv[0]); test_sysctls(inflight, openfiles); /* * threesome_drop4: close receive send sent */ test = "threesome_drop4"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); sendfd(sv[0], s); close3(sv[1], sv[0], s); test_sysctls(inflight, openfiles); /* * threesome_drop5: close send receive sent */ test = "threesome_drop5"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); sendfd(sv[0], s); close3(sv[0], sv[1], s); test_sysctls(inflight, openfiles); /* * threesome_drop6: close send sent receive */ test = "threesome_drop6"; printf("%s\n", test); save_sysctls(&inflight, &openfiles); alloc3fds(&s, sv); close3(sv[0], s, sv[1]); test_sysctls(inflight, openfiles); }
int main(int argc, char **argv) { char *filename = ZOOK_CONF; CONF *conf; long eline = 0; char *portstr, *svcs; int sockfd; pid_t disppid; int i, status; /* read configuration http://linux.die.net/man/5/config http://www.openssl.org/docs/apps/config.html */ if (argc > 1) filename = argv[1]; conf = NCONF_new(NULL); if (!NCONF_load(conf, filename, &eline)) { if (eline) errx(1, "Failed parsing %s:%ld", filename, eline); else errx(1, "Failed opening %s", filename); } /* http server port, default 80 */ if (!(portstr = NCONF_get_string(conf, "zook", "port"))) portstr = "80"; sockfd = start_server(portstr); warnx("Listening on port %s", portstr); signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); /* launch the dispatch daemon */ disppid = launch_svc(conf, "zookd"); /* launch http services */ if ((svcs = NCONF_get_string(conf, "zook", "http_svcs"))) CONF_parse_list(svcs, ',', 1, &service_parse_cb, conf); /* send the server socket to zookd */ if (sendfd(svcfds[0], &nsvcs, sizeof(nsvcs), sockfd) < 0) err(1, "sendfd to zookd"); close(sockfd); /* send all svc sockets with their url patterns to http services */ for (i = 1; i < nsvcs; ++i) { char *url = NCONF_get_string(conf, svcnames[i], "url"); if (!url) url = ".*"; sendfd(svcfds[0], url, strlen(url) + 1, svcfds[i]); close(svcfds[i]); } close(svcfds[0]); /* launch non-http services */ if ((svcs = NCONF_get_string(conf, "zook", "extra_svcs"))) CONF_parse_list(svcs, ',', 1, &service_parse_cb, conf); NCONF_free(conf); /* wait for zookd */ waitpid(disppid, &status, 0); }
int main() { pthread_t t[1000]; sfd=socket(AF_INET,SOCK_STREAM,0); unfd=socket(AF_LOCAL,SOCK_STREAM,0); struct sockaddr_in saddr; struct sockaddr_un uaddr,my; saddr.sin_family=AF_INET; saddr.sin_addr.s_addr=INADDR_ANY; saddr.sin_port=htons(3500); if(bind(sfd,(struct sockaddr*) &saddr,sizeof saddr)<0) { printf("Bind error\n"); exit(0); } listen(sfd,10); uaddr.sun_family=AF_LOCAL; strncpy(uaddr.sun_path,"abc",sizeof(uaddr.sun_path)-1); if(connect(unfd,(struct sockaddr*) &uaddr,sizeof uaddr)<0) { printf("unix connect error\n"); exit(0); } while(iter<3) { struct sockaddr_in addr; socklen_t len=16; fds[iter]=accept(sfd,(struct sockaddr*)&addr,&len); pthread_create(&t[iter],NULL,service,(void*)&fds[iter]); iter++; } printf("server is down send to other server\n"); int i=0; for(i=0;i<iter;i++) { /*struct msghdr msg={0};//clear the msghdr char buf[CMSG_SPACE(sizeof(int))];//allocate needed space msg.msg_control=buf;//allocate buffer to msghdr msg.msg_controllen=sizeof buf;//length of buffer char p[1]={'a'}; struct iovec base; base.iov_base=p; base.iov_len=1; msg.msg_iov=&base; msg.msg_iovlen=1; struct cmsghdr* cmsg=CMSG_FIRSTHDR(&msg);//get first CMSG_HDR cmsg->cmsg_level=SOL_SOCKET; cmsg->cmsg_type=SCM_RIGHTS;//passing fd cmsg->cmsg_len=CMSG_LEN(sizeof(int));//size of one fd memcpy(CMSG_DATA(cmsg),&fds[i],sizeof fds[i]);//copy argument fd into cmsg data msg.msg_controllen=cmsg->cmsg_len;//length reset for sending int x=sendmsg(unfd,&msg,0); //sendmsg returning 0...thus msg not being sent if(x<0) { printf("send error.\n"); exit(0); }*/ sendfd(unfd,fds[i]); } exit(0); }
int main (int argc, char **argv) { int sockfd, client_sockfd; int host_port; int yes; char buffer[BUF_SIZE]; struct sockaddr_in host_addr, client_addr; host_port = HOST_PORT; if (argc>1) host_port = atoi(argv[1]); if ((sockfd = socket (PF_INET, SOCK_STREAM, 0))==-1){ DIE("creating a socket"); } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) DIE("setting socket option SO_REUSEADDR"); memset(&(host_addr), '\0', sizeof(host_addr)); // zero off the structure host_addr.sin_family = AF_INET; host_addr.sin_port = htons(host_port); host_addr.sin_addr.s_addr = 0; if (bind(sockfd, (struct sockaddr *)&host_addr , sizeof(struct sockaddr)) == -1) DIE("binding to socket"); if (listen(sockfd, 1000) == -1) DIE("listening on socket"); signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); int pid; int fds[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) DIE("socketpair"); if ((pid=fork())==0){ close (fds[1]); char pipe_fd_str[32] = {'\0'}; char *child_name = "./httpd"; sprintf (pipe_fd_str, "%d", fds[0]); char *child_argv[] = {child_name , pipe_fd_str , 0}; if (DEBUG) printf ("%s\n", pipe_fd_str); if (execv (child_name, child_argv)==-1) DIE("execve"); } close (fds[0]); while (1){ int size; if ((client_sockfd = accept (sockfd, (struct sockaddr *)&client_addr, &size))==-1) DIE("accepting client connection"); printf("1\n"); printf("Parent process client_sockfd = %d\n",client_sockfd); if (DEBUG) printf ("server: accepting a client from %s port %d\n" , inet_ntoa (client_addr.sin_addr) , ntohs (client_addr.sin_port)); char sock_fd_str[32] = {'\0'}; sprintf (sock_fd_str, "%d", client_sockfd); if (DEBUG){ printf ("sending a socket to the client:\n"); } sendfd(fds[1],sock_fd_str, strlen(sock_fd_str)+1,client_sockfd); close(client_sockfd); } return 0; }
void main(int argc, char **argv) { HConnect *c; Dir *dir; Hio *hin, *hout; char *s, *t, *fn; int n, nfn, datafd, htmlfd; c = init(argc, argv); if(dangerous(c->req.uri)){ hfail(c, HSyntax); exits("failed"); } if(hparseheaders(c, HSTIMEOUT) < 0) exits("failed"); hout = &c->hout; if(c->head.expectother){ hfail(c, HExpectFail, nil); exits("failed"); } if(c->head.expectcont){ hprint(hout, "100 Continue\r\n"); hprint(hout, "\r\n"); hflush(hout); } s = nil; if(strcmp(c->req.meth, "POST") == 0){ hin = hbodypush(&c->hin, c->head.contlen, c->head.transenc); if(hin != nil){ alarm(HSTIMEOUT); s = hreadbuf(hin, hin->pos); alarm(0); } if(s == nil){ hfail(c, HBadReq, nil); exits("failed"); } t = strchr(s, '\n'); if(t != nil) *t = '\0'; }else if(strcmp(c->req.meth, "GET") != 0 && strcmp(c->req.meth, "HEAD") != 0){ hunallowed(c, "GET, HEAD, PUT"); exits("unallowed"); }else s = c->req.search; if(s == nil){ hfail(c, HNoData, "save"); exits("failed"); } if(strlen(s) > MaxLog) s[MaxLog] = '\0'; n = snprint(c->xferbuf, HBufSize, "at %ld %s\n", time(0), s); nfn = strlen(c->req.uri) + 64; fn = halloc(c, nfn); /* * open file descriptors & write log line */ snprint(fn, nfn, "/usr/web/save/%s.html", c->req.uri); htmlfd = open(fn, OREAD); if(htmlfd < 0 || (dir = dirfstat(htmlfd)) == nil){ hfail(c, HNotFound, c->req.uri); exits("failed"); return; } snprint(fn, nfn, "/usr/web/save/%s.data", c->req.uri); datafd = openLocked(fn, OWRITE); if(datafd < 0){ errstr(c->xferbuf, sizeof c->xferbuf); if(strstr(c->xferbuf, "locked") != nil) hfail(c, HTempFail, c->req.uri); else hfail(c, HNotFound, c->req.uri); exits("failed"); } seek(datafd, 0, 2); write(datafd, c->xferbuf, n); close(datafd); sendfd(c, htmlfd, dir, hmkcontent(c, "text", "html", nil), nil); exits(nil); }
int main(int argc, char *argv[]) { struct stat putfd_1_stat, putfd_2_stat, getfd_1_stat, getfd_2_stat; int fd[2], putfd_1, putfd_2, getfd_1, getfd_2; const char *test; /* * First test: put a temporary file into a UNIX domain socket, then * take it out and make sure it's the same file. First time around, * don't close the reference after sending. */ test = "test1-simplesendfd"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); dofstat(test, putfd_1, &putfd_1_stat); sendfd(test, fd[0], putfd_1); recvfd(test, fd[1], &getfd_1); dofstat(test, getfd_1, &getfd_1_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); close(putfd_1); close(getfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Second test: same as first, only close the file reference after * sending, so that the only reference is the descriptor in the UNIX * domain socket buffer. */ test = "test2-sendandclose"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); dofstat(test, putfd_1, &putfd_1_stat); sendfd(test, fd[0], putfd_1); close(putfd_1); recvfd(test, fd[1], &getfd_1); dofstat(test, getfd_1, &getfd_1_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); close(getfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Third test: put a temporary file into a UNIX domain socket, then * close both endpoints causing garbage collection to kick off. */ test = "test3-sendandcancel"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); sendfd(test, fd[0], putfd_1); close(putfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Send two files. Then receive them. Make sure they are returned * in the right order, and both get there. */ test = "test4-twofile"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); tempfile(test, &putfd_2); dofstat(test, putfd_1, &putfd_1_stat); dofstat(test, putfd_2, &putfd_2_stat); sendfd(test, fd[0], putfd_1); sendfd(test, fd[0], putfd_2); close(putfd_1); close(putfd_2); recvfd(test, fd[1], &getfd_1); recvfd(test, fd[1], &getfd_2); dofstat(test, getfd_1, &getfd_1_stat); dofstat(test, getfd_2, &getfd_2_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); samefile(test, &putfd_2_stat, &getfd_2_stat); close(getfd_1); close(getfd_2); closesocketpair(fd); printf("%s passed\n", test); /* * Big bundling test. Send an endpoint of the UNIX domain socket * over itself, closing the door behind it. */ test = "test5-bundle"; printf("beginning %s\n", test); domainsocketpair(test, fd); sendfd(test, fd[0], fd[0]); close(fd[0]); recvfd(test, fd[1], &getfd_1); close(getfd_1); close(fd[1]); printf("%s passed\n", test); /* * Big bundling test part two: Send an endpoint of the UNIX domain * socket over itself, close the door behind it, and never remove it * from the other end. */ test = "test6-bundlecancel"; printf("beginning %s\n", test); domainsocketpair(test, fd); sendfd(test, fd[0], fd[0]); sendfd(test, fd[1], fd[0]); closesocketpair(fd); printf("%s passed\n", test); /* * Test for PR 151758: Send an character device over the UNIX * domain socket and then close both sockets to orphan the * device. */ test = "test7-devfsorphan"; printf("beginning %s\n", test); domainsocketpair(test, fd); devnull(test, &putfd_1); sendfd(test, fd[0], putfd_1); close(putfd_1); closesocketpair(fd); printf("%s passed\n", test); return (0); }
/* * Send a listen UDP socket over a UNIX domain socket. * * Send a listen TCP socket over a UNIX domain socket. * * Do each twice, with closing of the listen socket vs. socketpair in * different orders. */ static void listen_drop(void) { struct sockaddr_un sun; struct sockaddr_in sin; int inflight, openfiles; int s, sv[2]; bzero(&sun, sizeof(sun)); sun.sun_family = AF_LOCAL; sun.sun_len = sizeof(sun); /* * Close listen socket first. */ test = "listen_drop_unp1"; printf("%s\n", test); snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dpath, test); save_sysctls(&inflight, &openfiles); s = my_socket(PF_LOCAL, SOCK_STREAM, 0); my_bind(s, (struct sockaddr *)&sun, sizeof(sun)); my_listen(s, -1); my_socketpair(sv); sendfd(sv[0], s); close3(s, sv[0], sv[1]); test_sysctls(inflight, openfiles); /* * Close socketpair first. */ test = "listen_drop_unp2"; printf("%s\n", test); snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dpath, test); save_sysctls(&inflight, &openfiles); s = my_socket(PF_LOCAL, SOCK_STREAM, 0); my_bind(s, (struct sockaddr *)&sun, sizeof(sun)); my_listen(s, -1); my_socketpair(sv); sendfd(sv[0], s); close3(sv[0], sv[1], s); test_sysctls(inflight, openfiles); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); sin.sin_port = htons(0); /* * Close listen socket first. */ test = "listen_drop_inet1"; printf("%s\n", test); bzero(&sun, sizeof(sun)); save_sysctls(&inflight, &openfiles); s = my_socket(PF_INET, SOCK_STREAM, 0); my_bind(s, (struct sockaddr *)&sin, sizeof(sin)); my_listen(s, -1); my_socketpair(sv); sendfd(sv[0], s); close3(s, sv[0], sv[1]); test_sysctls(inflight, openfiles); /* * Close socketpair first. */ test = "listen_drop_inet2"; printf("%s\n", test); bzero(&sun, sizeof(sun)); save_sysctls(&inflight, &openfiles); s = my_socket(PF_INET, SOCK_STREAM, 0); my_bind(s, (struct sockaddr *)&sin, sizeof(sin)); my_listen(s, -1); my_socketpair(sv); sendfd(sv[0], s); close3(sv[0], sv[1], s); test_sysctls(inflight, openfiles); }