]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/simulate.c
Use 65534 for anon uid/gid rather than -2
[nfs-utils.git] / utils / statd / simulate.c
1 /*
2  * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3  *
4  * NSM for Linux.
5  */
6
7 #include "config.h"
8 #ifndef SIMULATIONS
9 # error How the hell did we get here?
10 #endif
11
12 /* If we're running the simulator, we're debugging.  Pretty simple. */
13 #ifndef DEBUG
14 # define DEBUG
15 #endif
16
17 #include <signal.h>
18 #include <string.h>
19 #include <rpc/rpc.h>
20 #include <rpc/pmap_clnt.h>
21 #include <rpcmisc.h>
22 #include "statd.h"
23 #include "sim_sm_inter.h"
24
25 static void daemon_simulator (void);
26 static void sim_killer (int sig);
27 static void simulate_crash (char *);
28 static void simulate_mon (char *, char *, char *, char *, char *);
29 static void simulate_stat (char *, char *);
30 static void simulate_unmon (char *, char *, char *, char *);
31 static void simulate_unmon_all (char *, char *, char *);
32
33 static int sim_port = 0;
34
35 extern void sim_sm_prog_1 (struct svc_req *, register SVCXPRT);
36 extern void svc_exit (void);
37
38 void
39 simulator (int argc, char **argv)
40 {
41   log_enable (1);
42
43   if (argc == 2)
44     if (!strcasecmp (*argv, "crash"))
45       simulate_crash (*(&argv[1]));
46
47   if (argc == 3) {
48     if (!strcasecmp (*argv, "stat"))
49       simulate_stat (*(&argv[1]), *(&argv[2]));
50   }
51   if (argc == 4) {
52     if (!strcasecmp (*argv, "unmon_all"))
53       simulate_unmon_all (*(&argv[1]), *(&argv[2]), *(&argv[3]));
54   }
55   if (argc == 5) {
56     if (!strcasecmp (*argv, "unmon"))
57       simulate_unmon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]));
58   }
59   if (argc == 6) {
60     if (!strcasecmp (*argv, "mon"))
61       simulate_mon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]),
62                     *(&argv[5]));
63   }
64   die ("WTF?  Give me something I can use!");
65 }
66
67 static void
68 simulate_mon (char *calling, char *monitoring, char *as, char *proggy,
69               char *fool)
70 {
71   CLIENT *client;
72   sm_stat_res *result;
73   mon mon;
74
75   dprintf (N_DEBUG, "Calling %s (as %s) to monitor %s", calling, as,
76            monitoring);
77
78   if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
79     die ("%s", clnt_spcreateerror ("clnt_create"));
80
81   memcpy (mon.priv, fool, SM_PRIV_SIZE);
82   mon.mon_id.my_id.my_name = xstrdup (as);
83   sim_port = atoi (proggy) * SIM_SM_PROG;
84   mon.mon_id.my_id.my_prog = sim_port; /* Pseudo-dummy */
85   mon.mon_id.my_id.my_vers = SIM_SM_VERS;
86   mon.mon_id.my_id.my_proc = SIM_SM_MON;
87   mon.mon_id.mon_name = monitoring;
88
89   if (!(result = sm_mon_1 (&mon, client)))
90     die ("%s", clnt_sperror (client, "sm_mon_1"));
91
92   free (mon.mon_id.my_id.my_name);
93
94   if (result->res_stat != STAT_SUCC) {
95     note (N_FATAL, "SM_MON request failed, state: %d", result->state);
96     exit (0);
97   } else {
98     dprintf (N_DEBUG, "SM_MON result successful, state: %d\n", result->state);
99     dprintf (N_DEBUG, "Waiting for callback.");
100     daemon_simulator ();
101     exit (0);
102   }
103 }
104
105 static void
106 simulate_unmon (char *calling, char *unmonitoring, char *as, char *proggy)
107 {
108   CLIENT *client;
109   sm_stat *result;
110   mon_id mon_id;
111
112   dprintf (N_DEBUG, "Calling %s (as %s) to unmonitor %s", calling, as,
113            unmonitoring);
114
115   if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
116     die ("%s", clnt_spcreateerror ("clnt_create"));
117
118   mon_id.my_id.my_name = xstrdup (as);
119   mon_id.my_id.my_prog = atoi (proggy) * SIM_SM_PROG;
120   mon_id.my_id.my_vers = SIM_SM_VERS;
121   mon_id.my_id.my_proc = SIM_SM_MON;
122   mon_id.mon_name = unmonitoring;
123
124   if (!(result = sm_unmon_1 (&mon_id, client)))
125     die ("%s", clnt_sperror (client, "sm_unmon_1"));
126
127   free (mon_id.my_id.my_name);
128   dprintf (N_DEBUG, "SM_UNMON request returned state: %d\n", result->state);
129   exit (0);
130 }
131
132 static void
133 simulate_unmon_all (char *calling, char *as, char *proggy)
134 {
135   CLIENT *client;
136   sm_stat *result;
137   my_id my_id;
138
139   dprintf (N_DEBUG, "Calling %s (as %s) to unmonitor all hosts", calling, as);
140
141   if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
142     die ("%s", clnt_spcreateerror ("clnt_create"));
143
144   my_id.my_name = xstrdup (as);
145   my_id.my_prog = atoi (proggy) * SIM_SM_PROG;
146   my_id.my_vers = SIM_SM_VERS;
147   my_id.my_proc = SIM_SM_MON;
148
149   if (!(result = sm_unmon_all_1 (&my_id, client)))
150     die ("%s", clnt_sperror (client, "sm_unmon_all_1"));
151
152   free (my_id.my_name);
153   dprintf (N_DEBUG, "SM_UNMON_ALL request returned state: %d\n", result->state);
154   exit (0);
155 }
156
157 static void
158 simulate_crash (char *host)
159 {
160   CLIENT *client;
161
162   if ((client = clnt_create (host, SM_PROG, SM_VERS, "udp")) == NULL)
163     die ("%s", clnt_spcreateerror ("clnt_create"));
164
165   if (!sm_simu_crash_1 (NULL, client))
166     die ("%s", clnt_sperror (client, "sm_simu_crash_1"));
167
168   exit (0);
169 }
170
171 static void
172 simulate_stat (char *calling, char *monitoring)
173 {
174   CLIENT *client;
175   sm_name checking;
176   sm_stat_res *result;
177   
178   if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
179     die ("%s", clnt_spcreateerror ("clnt_create"));
180
181   checking.mon_name = monitoring;
182
183   if (!(result = sm_stat_1 (&checking, client)))
184     die ("%s", clnt_sperror (client, "sm_stat_1"));
185
186   if (result->res_stat == STAT_SUCC)
187     dprintf (N_DEBUG, "STAT_SUCC from %s for %s, state: %d", calling,
188              monitoring, result->state);
189   else
190     dprintf (N_DEBUG, "STAT_FAIL from %s for %s, state: %d", calling,
191              monitoring, result->state);
192
193   exit (0);
194 }
195
196 static void
197 sim_killer (int sig)
198 {
199   note (N_FATAL, "Simulator caught signal %d, un-registering and exiting.", sig);
200   pmap_unset (sim_port, SIM_SM_VERS);
201   exit (0);
202 }
203
204 static void
205 daemon_simulator (void)
206 {
207   signal (SIGHUP, sim_killer);
208   signal (SIGINT, sim_killer);
209   signal (SIGTERM, sim_killer);
210   pmap_unset (sim_port, SIM_SM_VERS);
211   /* this registers both UDP and TCP services */
212   rpc_init("statd", sim_port, SIM_SM_VERS, sim_sm_prog_1, 0);
213   svc_run ();
214   pmap_unset (sim_port, SIM_SM_VERS);
215 }
216
217 void *
218 sim_sm_mon_1_svc (struct status *argp, struct svc_req *rqstp)
219 {
220   static char *result;
221
222   dprintf (N_DEBUG, "Recieved state %d for mon_name %s (opaque \"%s\")",
223            argp->state, argp->mon_name, argp->priv);
224   svc_exit ();
225   return ((void *)&result);
226 }