• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sys/ioctl.h>
2 #include <stdio.h>
3 #include <netdb.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "rpc.h"
10 #include "librpc01.h"
11 
12 int debug = 0;
13 int program = 2000333;
14 int version = 10;
15 
16 void breakpoint(void);
17 void service_request(struct svc_req *rqstp, SVCXPRT * transp);
18 
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 	SVCXPRT *transp;
22 	int i, n;
23 
24 	for (i = 1; i < argc; i++) {
25 		if (!strcmp(argv[i], "-d"))
26 			debug = 1;
27 		if (!strcmp(argv[i], "-p")) {
28 			if (++i >= argc) {
29 				fprintf(stderr, "%s: -p requires an argument\n",
30 					argv[0]);
31 				exit(1);
32 			}
33 			n = sscanf(argv[i], "%d", &program);
34 			if (n != 1) {
35 				fprintf(stderr,
36 					"%s: -p requires an numeric argument\n",
37 					argv[0]);
38 				exit(1);
39 			}
40 		}
41 		if (!strcmp(argv[i], "-v")) {
42 			if (++i >= argc) {
43 				fprintf(stderr, "%s: -v requires an argument\n",
44 					argv[0]);
45 				exit(1);
46 			}
47 			n = sscanf(argv[i], "%d", &version);
48 			if (n != 1) {
49 				fprintf(stderr,
50 					"%s: -v requires an numeric argument\n",
51 					argv[0]);
52 				exit(1);
53 			}
54 		}
55 	}
56 
57 	if (!debug) {
58 		if ((n = fork()) < 0) {
59 			fprintf(stderr, "%s: Can't fork\n", argv[0]);
60 			exit(1);
61 		}
62 
63 		if (n > 0)
64 			exit(0);
65 
66 		i = 50;
67 		while (--i >= 0)
68 			close(i);
69 		open("/dev/null", O_RDONLY);
70 		open("/dev/null", O_WRONLY);
71 		i = dup(1);
72 
73 		if ((i = open("/dev/tty", O_RDWR)) >= 0) {
74 			ioctl(i, TIOCNOTTY, 0);
75 			close(i);
76 		}
77 	}
78 
79 	pmap_unset(program, version);
80 	transp = svcudp_create(RPC_ANYSOCK);
81 	svc_register(transp, program, version, service_request, IPPROTO_UDP);
82 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
83 	svc_register(transp, program, version, service_request, IPPROTO_TCP);
84 	svc_run();
85 	exit(1);
86 }
87 
service_request(struct svc_req * rqstp,SVCXPRT * transp)88 void service_request(struct svc_req *rqstp, SVCXPRT * transp)
89 {
90 	struct data *buffer;
91 
92 	switch (rqstp->rq_proc) {
93 	case 0:
94 		svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
95 		breakpoint();
96 		return;
97 
98 	case 99:
99 		exit(0);
100 
101 	case 1:
102 		svc_getargs(transp, (xdrproc_t)xdr_receive_data,
103 				(char *)&buffer);
104 		svc_sendreply(transp, (xdrproc_t)xdr_send_data,
105 				(char *)buffer);
106 		free(buffer->data);
107 		free(buffer);
108 		return;
109 
110 	default:
111 		svcerr_noproc(transp);
112 		return;
113 	}
114 }
115 
breakpoint(void)116 void breakpoint(void)
117 {
118 	if (debug)
119 		printf("breakpoint\n");
120 }
121