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