Back to index

glibc  2.9
thrsvc.c
Go to the documentation of this file.
00001 #include <pthread.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 #include <rpc/rpc.h>
00006 #include <arpa/inet.h>
00007 
00008 #define PROGNUM 1234
00009 #define VERSNUM 1
00010 #define PROCNUM 1
00011 #define PROCQUIT 2
00012 
00013 static int exitcode;
00014 
00015 struct rpc_arg
00016 {
00017   CLIENT *client;
00018   u_long proc;
00019 };
00020 
00021 static void
00022 dispatch(struct svc_req *request, SVCXPRT *xprt)
00023 {
00024   svc_sendreply(xprt, (xdrproc_t)xdr_void, 0);
00025   if (request->rq_proc == PROCQUIT)
00026     exit (0);
00027 }
00028 
00029 static void
00030 test_one_call (struct rpc_arg *a)
00031 {
00032   struct timeval tout = { 60, 0 };
00033   enum clnt_stat result;
00034 
00035   printf ("test_one_call: ");
00036   result = clnt_call (a->client, a->proc,
00037                     (xdrproc_t) xdr_void, 0,
00038                     (xdrproc_t) xdr_void, 0, tout);
00039   if (result == RPC_SUCCESS)
00040     puts ("success");
00041   else
00042     {
00043       clnt_perrno (result);
00044       putchar ('\n');
00045       exitcode = 1;
00046     }
00047 }
00048 
00049 static void *
00050 thread_wrapper (void *arg)
00051 {
00052   struct rpc_arg a;
00053 
00054   a.client = (CLIENT *)arg;
00055   a.proc = PROCNUM;
00056   test_one_call (&a);
00057   a.client = (CLIENT *)arg;
00058   a.proc = PROCQUIT;
00059   test_one_call (&a);
00060   return 0;
00061 }
00062 
00063 int
00064 main (void)
00065 {
00066   pthread_t tid;
00067   pid_t pid;
00068   int err;
00069   SVCXPRT *svx;
00070   CLIENT *clnt;
00071   struct sockaddr_in sin;
00072   struct timeval wait = { 5, 0 };
00073   int sock = RPC_ANYSOCK;
00074   struct rpc_arg a;
00075 
00076   svx = svcudp_create (RPC_ANYSOCK);
00077   svc_register (svx, PROGNUM, VERSNUM, dispatch, 0);
00078 
00079   pid = fork ();
00080   if (pid == -1)
00081     {
00082       perror ("fork");
00083       return 1;
00084     }
00085   if (pid == 0)
00086     svc_run ();
00087 
00088   inet_aton ("127.0.0.1", &sin.sin_addr);
00089   sin.sin_port = htons (svx->xp_port);
00090   sin.sin_family = AF_INET;
00091 
00092   clnt = clntudp_create (&sin, PROGNUM, VERSNUM, wait, &sock);
00093 
00094   a.client = clnt;
00095   a.proc = PROCNUM;
00096 
00097   /* Test in this thread */
00098   test_one_call (&a);
00099 
00100   /* Test in a child thread */
00101   err = pthread_create (&tid, 0, thread_wrapper, (void *) clnt);
00102   if (err)
00103     fprintf (stderr, "pthread_create: %s\n", strerror (err));
00104   err = pthread_join (tid, 0);
00105   if (err)
00106     fprintf (stderr, "pthread_join: %s\n", strerror (err));
00107 
00108   return exitcode;
00109 }