Home | History | Annotate | Line # | Download | only in librumpclient
h_exec.c revision 1.1
      1 /*	$NetBSD: h_exec.c,v 1.1 2011/02/15 15:16:46 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/types.h>
     31 #include <sys/socket.h>
     32 
     33 #include <netinet/in.h>
     34 
     35 #include <err.h>
     36 #include <errno.h>
     37 #include <fcntl.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 
     43 /*
     44  * Uses rumphijack because it's convenient with exec.  XXX: fixme to
     45  * have sensible exec support in rumpclient.
     46  */
     47 
     48 int
     49 main(int argc, char *argv[])
     50 {
     51 	struct sockaddr_in sin;
     52 	socklen_t slen;
     53 	int s1, s2;
     54 	char buf[12];
     55 
     56 	if (argc > 1) {
     57 		if (strcmp(argv[1], "_didexec") == 0) {
     58 			daemon(0, 0); /* detach-me-notnot ergo detach */
     59 			s1 = atoi(argv[2]);
     60 			slen = sizeof(sin);
     61 			/* see below */
     62 			accept(s1, (struct sockaddr *)&sin, &slen);
     63 		}
     64 	}
     65 
     66 	/* open and listenize two TCP4 suckets */
     67 	if ((s1 = socket(PF_INET, SOCK_STREAM, 0)) == -1)
     68 		err(1, "socket 1");
     69 	if ((s2 = socket(PF_INET, SOCK_STREAM, 0)) == -1)
     70 		err(1, "socket 2");
     71 
     72 	memset(&sin, 0, sizeof(sin));
     73 	sin.sin_len = sizeof(sin);
     74 	sin.sin_family = AF_INET;
     75 	sin.sin_port = htons(1234);
     76 
     77 	if (bind(s1, (struct sockaddr *)&sin, sizeof(sin)) == -1)
     78 		err(1, "bind1");
     79 	sin.sin_port = htons(2345);
     80 	if (bind(s2, (struct sockaddr *)&sin, sizeof(sin)) == -1)
     81 		err(1, "bind2");
     82 
     83 	if (listen(s1, 1) == -1)
     84 		err(1, "listen1");
     85 	if (listen(s2, 1) == -1)
     86 		err(1, "listen2");
     87 
     88 	if (argc == 1) {
     89 		daemon(0, 0);
     90 		slen = sizeof(sin);
     91 		/*
     92 		 * "pause()", but conveniently gets rid of this helper
     93 		 * since we were called with RUMPCLIENT_RETRYCONN_DIE set
     94 		 */
     95 		accept(s1, (struct sockaddr *)&sin, &slen);
     96 	}
     97 
     98 	/* omstart! */
     99 	sprintf(buf, "%d", s1);
    100 	if (execl(argv[1], "h_ution", "_didexec", buf, NULL) == -1)
    101 		err(1, "exec");
    102 }
    103