Пример #1
0
int main(int argc, char **argv)
{
    int fd, sockfd = -1, i;

    if (argc != 2)
        errx(1, "Wrong arguments");
    fd = atoi(argv[1]);

    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);

    /* receive the number of services and the server socket from zookld */
    if ((recvfd(fd, &nsvcs, sizeof(nsvcs), &sockfd) <= 0) || sockfd < 0)
        err(1, "recvfd sockfd");
    --nsvcs;
    warnx("Start with %d service(s)", nsvcs);

    /* receive url patterns of all services */
    for (i = 0; i != nsvcs; ++i)
    {
        char url[1024], regexp[1024];
        if (recvfd(fd, url, sizeof(url), &svcfds[i]) <= 0)
            err(1, "recvfd svc %d", i + 1);
        snprintf(regexp, sizeof(regexp), "^%s$", url);
        if (regcomp(&svcurls[i], regexp, REG_EXTENDED | REG_NOSUB))
            errx(1, "Bad url for service %d: %s", i + 1, url);
        warnx("Dispatch %s for service %d", regexp, i + 1);
    }

    close(fd);

    for (;;)
    {
        int cltfd = accept(sockfd, NULL, NULL);
        if (cltfd < 0)
            err(1, "accept");
        switch (fork())
        {
        case -1: /* error */
            err(1, "fork");
        case 0:  /* child */
            process_client(cltfd);
            return 0;
        default: /* parent */
            close(cltfd);
            break;
        }
    }
}
Пример #2
0
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);
		}
		}
	}
}
Пример #3
0
int
suidgetfd_required (str prog)
{
  int fds[2];
  if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
    fatal ("socketpair: %m\n");
  close_on_exec (fds[0]);

  str path = fix_exec_path ("suidconnect");
  char *av[] = { "suidconnect", const_cast<char *> (prog.cstr ()), NULL };
  if (spawn (path, av, fds[1]) == -1)
    fatal << path << ": " << strerror (errno) << "\n";
  close (fds[1]);
  
  int fd = recvfd (fds[0]);
  if (fd < 0) {
    struct stat sb;
    if (!runinplace && !stat (path, &sb)
	&& (sb.st_gid != sfs_gid || !(sb.st_mode & 02000))) {
      if (struct group *gr = getgrgid (sfs_gid))
	warn << path << " should be setgid to group " << gr->gr_name << "\n";
      else
	warn << path << " should be setgid to group " << sfs_gid << "\n";
    }
    else {
      warn << "have you launched the appropriate daemon (sfscd or sfssd)?\n";
      warn << "have subsidiary daemons died (check your system logs)?\n";
    }
    fatal ("could not suidconnect for %s\n", prog.cstr ());
  }
  close (fds[0]);
  return fd;
}
Пример #4
0
static PyObject *p_recvfd(PyObject *self, PyObject *args)
{
    int fd, ret;
    char *data;
    size_t dlen;
    PyObject *ro;
    
    fd = 0;
    if(!PyArg_ParseTuple(args, "|i", &fd))
	return(NULL);
    while(1) {
	Py_BEGIN_ALLOW_THREADS;
	ret = recvfd(fd, &data, &dlen);
	Py_END_ALLOW_THREADS;
	if(ret < 0) {
	    if(errno == 0)
		return(Py_BuildValue("OO", Py_None, Py_None));
	    if(errno == EINTR) {
		if(PyErr_CheckSignals())
		    return(NULL);
		continue;
	    }
	    PyErr_SetFromErrno(PyExc_OSError);
	    return(NULL);
	}
	ro = Py_BuildValue("Ni", PyString_FromStringAndSize(data, dlen), ret);
	free(data);
	return(ro);
    }
}
Пример #5
0
int main(int argc, char **argv)
{
    int fd, sockfd = -1, i;

    if (argc != 2)
        errx(1, "Wrong arguments");
    fd = atoi(argv[1]);

    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);

    /* receive the number of services and the server socket from zookld */
    if ((recvfd(fd, &nsvcs, sizeof(nsvcs), &sockfd) <= 0) || sockfd < 0)
        err(1, "recvfd sockfd");
    --nsvcs;
    warnx("Start with %d service(s)", nsvcs);

    /* receive url patterns of all services */
    for (i = 0; i != nsvcs; ++i)
    {
        char url[1024], regexp[1024];
        if (recvfd(fd, url, sizeof(url), &svcfds[i]) <= 0)
            err(1, "recvfd svc %d", i + 1);
	/* parens are necessary here so that regexes like a|b get
	   parsed properly and not as (^a)|(b$) */
        snprintf(regexp, sizeof(regexp), "^(%s)$", url);
        if (regcomp(&svcurls[i], regexp, REG_EXTENDED | REG_NOSUB))
            errx(1, "Bad url for service %d: %s", i + 1, url);
        warnx("Dispatch %s for service %d", regexp, i + 1);
    }

    close(fd);

    for (;;)
    {
        int cltfd = accept(sockfd, NULL, NULL);
        if (cltfd < 0)
            err(1, "accept");
        process_client(cltfd);
    }
}
Пример #6
0
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);

  while (1){
    char uri_str[1024];
    int sockfd;

    recvfd (pipefd, uri_str, sizeof(uri_str), &sockfd);
    if (DEBUG){
      printf("uri=[%s]\n", uri_str);
      printf ("banksv client recieves a sockfd = %d\n", sockfd);
    }

    if(fork() == 0){
      int ruid, euid, suid;
      
      getresuid(&ruid, &euid, &suid);
      if (DEBUG)
	printf("ruid=[%d], euid=[%d], suid=[%d]\n"
	       , ruid
	       , euid
	       , suid);

      ReqLine_t reqline = ReqLine_new(REQ_KIND_POST
				      , uri_str
				      , HTTP_ONE_ONE);
      tree = Parse_parse(sockfd, 0);
      tree->reqLine = reqline;
      //response 
      Handle_main (sockfd, tree);
      
      close(sockfd);
      exit(0);
    }    
    close(sockfd);
  }
  return 0;
}
Пример #7
0
int main(int argc, char **argv)
{
    int fd;
    if (argc != 2 && argc != 4)
        errx(1, "Wrong arguments");
    fd = atoi(argv[1]);

    if (argc == 4) {
        int uid = atoi(argv[2]);
        int gid = atoi(argv[3]);
        warnx("cgi uid %d, gid %d", uid, gid);
        http_set_executable_uid_gid(uid, gid);
    }

    signal(SIGPIPE, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);

    for (;;)
    {
        char envp[8192];
        int sockfd = -1;
        const char *errmsg;

        /* receive socket and envp from zookd */
        if ((recvfd(fd, envp, sizeof(envp), &sockfd) <= 0) || sockfd < 0)
            err(1, "recvfd");

        switch (fork())
        {
        case -1: /* error */
            err(1, "fork");
        case 0:  /* child */
            /* set envp */
            env_deserialize(envp, sizeof(envp));
            /* get all headers */
            if ((errmsg = http_request_headers(sockfd)))
                http_err(sockfd, 500, "http_request_headers: %s", errmsg);
            else
                warnx("REQUEST_URI: %s", getenv("REQUEST_URI"));
                http_serve(sockfd, getenv("REQUEST_URI"));
            return 0;
        default: /* parent */
            close(sockfd);
            break;
        }
    }
}
Пример #8
0
int main (int argc, char **argv)
{
  int pipefd;
  Http_t tree;
  if (argc<2)
    die ("server bug");

  pipefd = atoi (argv[1]);
  if (DEBUG)
    printf ("pipefd = %d\n", pipefd);//4
  signal(SIGCHLD, SIG_IGN);

  while (1){
    char sockfd_str[32];
    int sockfd;
    recvfd (pipefd, sockfd_str, sizeof(sockfd_str),&sockfd);
    printf("str = %s\n",sockfd_str);
    if (DEBUG)
      printf ("client recieves a sockfd = %d\n", sockfd);

    int pid;
    if ((pid=fork())==0){
      // parse the http requst
      printf("\n\t\t\tnew child httpd!\t\t\n");
      tree = Parse_parse(sockfd);
      
      if (DEBUG){
	printf ("parse success"
		"the http tree is:\n");
      }
      
      // response
      Handle_main (sockfd, tree);
      
      close(sockfd);
      
      if (DEBUG)
	printf("socket closed..");
      exit (0);
    }
     close(sockfd);
  }
  return 0;
}
Пример #9
0
int recvreq(int sock, struct hthead **reqp)
{
    int fd;
    struct charbuf buf;
    char *p;
    size_t l;
    char *name, *val;
    struct hthead *req;
    
    if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
	return(-1);
    }
    fcntl(fd, F_SETFD, FD_CLOEXEC);
    buf.s = buf.d;
    p = buf.b;
    l = buf.d;
    
    *reqp = omalloc(req);
    if((req->method = sstrdup(decstr(&p, &l))) == NULL)
	goto fail;
    if((req->url = sstrdup(decstr(&p, &l))) == NULL)
	goto fail;
    if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
	goto fail;
    if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
	goto fail;
    
    while(1) {
	if(!*(name = decstr(&p, &l)))
	    break;
	val = decstr(&p, &l);
	headappheader(req, name, val);
    }
    
    buffree(buf);
    return(fd);
    
fail:
    close(fd);
    freehthead(req);
    errno = EPROTO;
    return(-1);
}
Пример #10
0
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);

}
Пример #11
0
int main(int argc,char **argv)
{
	if(argc < 2) {
		printf("Usage: ./service <temperory_sock_path>\n");
		exit(1);
	}
	int usfd = init_sockconnect(argv[1]);
	if(usfd < 0){
		printf("sock() error\n");
		exit(1);
	}
	int fd,n; char buf[256];
	printf("Process 2 Started !!\n");
	int i=0;
	while(1) 
	{
		fd = recvfd(usfd);
		if(fd < 0) {
			perror("recv_fd() ");
			exit(1);
		}
		int pid=fork();
		if(pid == 0)
		{
			while(1)
			{
				bzero(buf,sizeof(buf));
				n=read(fd,buf,100);
				if(n > 0)
				{
					printf("Client:%d %s\n",i,buf);
				}
			}
		}
		else
		{
			close(fd);
			i++;
		}
	}
	return 0;
}
Пример #12
0
int
suidgetfd (str prog)
{
  int fds[2];
  if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
    return -1;
  close_on_exec (fds[0]);

  str path = fix_exec_path ("suidconnect");
  char *av[] = { "suidconnect", "-q",
		 const_cast<char *> (prog.cstr ()), NULL };
  if (spawn (path, av, fds[1]) == -1) {
    close (fds[0]);
    close (fds[1]);
    return -1;
  }
  close (fds[1]);
  
  int fd = recvfd (fds[0]);
  close (fds[0]);
  return fd;
}
Пример #13
0
int main(int argc,char** argv)
{
	pthread_t t[10];int i=0;
	
	sockaddr_un myaddr,servaddr;
	bzero(&servaddr,sizeof(servaddr));
	bzero(&myaddr,sizeof(myaddr));
	myaddr.sun_family=AF_UNIX;
	strcpy(myaddr.sun_path,"soc2.socket");
	unlink("soc2.socket");
	servaddr.sun_family=AF_UNIX;
	strcpy(servaddr.sun_path,"soc.socket");
	int usfd=socket(AF_UNIX,SOCK_STREAM,0);
	socklen_t len=sizeof(sa_family_t)+strlen(myaddr.sun_path);
	bind(usfd,(const struct sockaddr*)&myaddr,len);
	 len=sizeof(sa_family_t)+strlen(servaddr.sun_path);
	 int re=-1;
	 while(re<0)
	 {
	 re=connect(usfd,(struct sockaddr*)&servaddr,len);
	printf("connect%d\n",re);
		sleep(1);
	}
	
	while(1)
	{
		int nsfd=recvfd(usfd);
		/****5*/
		data* td=new data();
		td->nsfd=nsfd;
		printf("another accpeted %d\n",nsfd);
		pthread_create(&t[i++],NULL,&service,td);
	}
	
	
	
	
	
}
Пример #14
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);
		}
		
	}
}
Пример #15
0
int main(int argc, char *argv[])
{
    int parentid=atoi(argv[2]);
    int platform_no=atoi(argv[4]);
     int sfd,nsfd,n,portno;
     socklen_t clientn;
     char buffer[100];
     struct clientthreadarg temp;
    temp.portno=atoi(argv[3]);
    temp.protocol=146;
    pthread_create(&cabletv,NULL,clientthreadfunc,&temp);
    int usfd = init_sockconnectunix(argv[1]);
    if(usfd < 0){
        printf("sock() error\n");
        exit(1);
    }
    int service=0;
    int shmid1;
    key_t key1;
    key1=300;
    int size=1000;
    struct services *shm;
    if ((shmid1 = shmget (key1, size, IPC_CREAT | 0666)) == -1) {
       perror("shmget: shmget failed"); exit(1); } else {
       (void) fprintf(stderr, "shmget: shmget returned %d\n", shmid1);
    }
    if((shm = (struct services *)shmat(shmid1, NULL, 0))<0)
    {
        perror("Failed to shmat:");
        exit(1);
    }
    int i;
    /*for(i=0;i<2;i++)
        printf("SHM port value: %d\t SHM platform status: %d",shm->portno[i],shm->p[i]);*/

    while(1)
    {
        nsfd = recvfd(usfd);
        /*printf("Recieved fd: \n");*/
        if(nsfd < 0) {
            perror("recv_fd() ");
            exit(1);
        }
        int pid=fork();
        if(pid == 0){
            while(1){
                bzero(buffer,250);
                n=read(nsfd,buffer,100);
                if(n > 0)
                {
                    if(strcmp(buffer,"exit\n") == 0)
                    {
                        printf("Exiting...............\n");
                        shm->p[platform_no]=0;
                        close(nsfd);
                         printf("Exiting......sending signal to %d\n",parentid);
                        kill(parentid,SIGUSR1);
                        exit(0);
                    }
                    printf("Train: %s\n",buffer);
                }
            }
        }
        else if(pid > 0){
            close(nsfd);
            continue;
        }
    }
    

}
Пример #16
0
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);
}
Пример #17
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
}
Пример #18
0
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);
}
Пример #19
0
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;
}
Пример #20
0
/**
 * qemuCreateInBridgePortWithHelper:
 * @cfg: the configuration object in which the helper name is looked up
 * @brname: the bridge name
 * @ifname: the returned interface name
 * @macaddr: the returned MAC address
 * @tapfd: file descriptor return value for the new tap device
 * @flags: OR of virNetDevTapCreateFlags:

 *   VIR_NETDEV_TAP_CREATE_VNET_HDR
 *     - Enable IFF_VNET_HDR on the tap device
 *
 * This function creates a new tap device on a bridge using an external
 * helper.  The final name for the bridge will be stored in @ifname.
 *
 * Returns 0 in case of success or -1 on failure
 */
static int
qemuCreateInBridgePortWithHelper(virQEMUDriverConfigPtr cfg,
                                 const char *brname,
                                 char **ifname,
                                 int *tapfd,
                                 unsigned int flags)
{
    virCommandPtr cmd;
    char *errbuf = NULL, *cmdstr = NULL;
    int pair[2] = { -1, -1 };

    if ((flags & ~VIR_NETDEV_TAP_CREATE_VNET_HDR) != VIR_NETDEV_TAP_CREATE_IFUP)
        return -1;

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) < 0) {
        virReportSystemError(errno, "%s", _("failed to create socket"));
        return -1;
    }

    if (!virFileIsExecutable(cfg->bridgeHelperName)) {
        virReportSystemError(errno, _("'%s' is not a suitable bridge helper"),
                             cfg->bridgeHelperName);
        return -1;
    }

    cmd = virCommandNew(cfg->bridgeHelperName);
    if (flags & VIR_NETDEV_TAP_CREATE_VNET_HDR)
        virCommandAddArgFormat(cmd, "--use-vnet");
    virCommandAddArgFormat(cmd, "--br=%s", brname);
    virCommandAddArgFormat(cmd, "--fd=%d", pair[1]);
    virCommandSetErrorBuffer(cmd, &errbuf);
    virCommandDoAsyncIO(cmd);
    virCommandPassFD(cmd, pair[1],
                     VIR_COMMAND_PASS_FD_CLOSE_PARENT);
    virCommandClearCaps(cmd);
#ifdef CAP_NET_ADMIN
    virCommandAllowCap(cmd, CAP_NET_ADMIN);
#endif
    if (virCommandRunAsync(cmd, NULL) < 0) {
        *tapfd = -1;
        goto cleanup;
    }

    do {
        *tapfd = recvfd(pair[0], 0);
    } while (*tapfd < 0 && errno == EINTR);

    if (*tapfd < 0) {
        char ebuf[1024];
        char *errstr = NULL;

        if (!(cmdstr = virCommandToString(cmd)))
            goto cleanup;
        virCommandAbort(cmd);

        if (errbuf && *errbuf &&
            virAsprintf(&errstr, "\nstderr=%s", errbuf) < 0)
            goto cleanup;

        virReportError(VIR_ERR_INTERNAL_ERROR,
            _("%s: failed to communicate with bridge helper: %s%s"),
            cmdstr, virStrerror(errno, ebuf, sizeof(ebuf)),
            errstr ? errstr : "");
        VIR_FREE(errstr);
        goto cleanup;
    }

    if (virNetDevTapGetName(*tapfd, ifname) < 0 ||
        virCommandWait(cmd, NULL) < 0) {
        VIR_FORCE_CLOSE(*tapfd);
        *tapfd = -1;
    }

 cleanup:
    VIR_FREE(cmdstr);
    VIR_FREE(errbuf);
    virCommandFree(cmd);
    VIR_FORCE_CLOSE(pair[0]);
    return *tapfd < 0 ? -1 : 0;
}
Пример #21
0
static gboolean
pipe_reader (gpointer data)
{
  GstElement *pipeline = data;
  GstElement *ipcpipelinesrc, *mq, *decodebin;
  GstPad *rpad, *sink_pad;
  int fd;
  fd_set set;
  struct timeval tv;
  int ret;
  static int idx = 0;
  char name[32];

  FD_ZERO (&set);
  FD_SET (pipes[0], &set);
  tv.tv_sec = tv.tv_usec = 0;
  ret = select (pipes[0] + 1, &set, NULL, NULL, &tv);
  if (ret < 0) {
    fprintf (stderr, "Failed to select: %s\n", strerror (errno));
    return TRUE;
  }
  if (!FD_ISSET (pipes[0], &set))
    return TRUE;

  fd = recvfd (pipes[0]);
  ipcpipelinesrc = gst_element_factory_make ("ipcpipelinesrc", NULL);
  gst_bin_add (GST_BIN (pipeline), ipcpipelinesrc);
  g_object_set (ipcpipelinesrc, "fdin", fd, "fdout", fd, NULL);


  mq = gst_bin_get_by_name (GST_BIN (pipeline), "mq");
  if (!mq) {
    fprintf (stderr, "Failed to get mq\n");
    return TRUE;
  }
  if (!gst_element_link (ipcpipelinesrc, mq)) {
    fprintf (stderr, "Failed to link ipcpipelinesrc and mq\n");
    return TRUE;
  }

  snprintf (name, sizeof (name), "src_%u", idx++);
  rpad = gst_element_get_static_pad (mq, name);
  if (!rpad) {
    fprintf (stderr, "Failed to get mq request pad\n");
    return TRUE;
  }

  decodebin = gst_element_factory_make ("decodebin", NULL);
  gst_bin_add (GST_BIN (pipeline), decodebin);
  sink_pad = gst_element_get_static_pad (decodebin, "sink");
  gst_pad_link (rpad, sink_pad);
  gst_object_unref (sink_pad);

  g_signal_connect (decodebin, "pad-added", G_CALLBACK (on_decoded_pad_added),
      pipeline);

  /* dynamically added elements should be synced manually
   * to the state of the slave pipeline */
  gst_element_sync_state_with_parent (ipcpipelinesrc);
  gst_element_sync_state_with_parent (decodebin);

  GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
      GST_DEBUG_GRAPH_SHOW_ALL, "ipc.slave.added");
  gst_object_unref (mq);

  return TRUE;
}