2 * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
9 # error How the hell did we get here?
12 /* If we're running the simulator, we're debugging. Pretty simple. */
20 #include <rpc/pmap_clnt.h>
23 #include "sim_sm_inter.h"
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 *);
33 static int sim_port = 0;
35 extern void sim_sm_prog_1 (struct svc_req *, register SVCXPRT);
36 extern void svc_exit (void);
39 simulator (int argc, char **argv)
44 if (!strcasecmp (*argv, "crash"))
45 simulate_crash (*(&argv[1]));
48 if (!strcasecmp (*argv, "stat"))
49 simulate_stat (*(&argv[1]), *(&argv[2]));
52 if (!strcasecmp (*argv, "unmon_all"))
53 simulate_unmon_all (*(&argv[1]), *(&argv[2]), *(&argv[3]));
56 if (!strcasecmp (*argv, "unmon"))
57 simulate_unmon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]));
60 if (!strcasecmp (*argv, "mon"))
61 simulate_mon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]),
64 die ("WTF? Give me something I can use!");
68 simulate_mon (char *calling, char *monitoring, char *as, char *proggy,
75 dprintf (N_DEBUG, "Calling %s (as %s) to monitor %s", calling, as,
78 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
79 die ("%s", clnt_spcreateerror ("clnt_create"));
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;
89 if (!(result = sm_mon_1 (&mon, client)))
90 die ("%s", clnt_sperror (client, "sm_mon_1"));
92 free (mon.mon_id.my_id.my_name);
94 if (result->res_stat != STAT_SUCC) {
95 note (N_FATAL, "SM_MON request failed, state: %d", result->state);
98 dprintf (N_DEBUG, "SM_MON result successful, state: %d\n", result->state);
99 dprintf (N_DEBUG, "Waiting for callback.");
106 simulate_unmon (char *calling, char *unmonitoring, char *as, char *proggy)
112 dprintf (N_DEBUG, "Calling %s (as %s) to unmonitor %s", calling, as,
115 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
116 die ("%s", clnt_spcreateerror ("clnt_create"));
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;
124 if (!(result = sm_unmon_1 (&mon_id, client)))
125 die ("%s", clnt_sperror (client, "sm_unmon_1"));
127 free (mon_id.my_id.my_name);
128 dprintf (N_DEBUG, "SM_UNMON request returned state: %d\n", result->state);
133 simulate_unmon_all (char *calling, char *as, char *proggy)
139 dprintf (N_DEBUG, "Calling %s (as %s) to unmonitor all hosts", calling, as);
141 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
142 die ("%s", clnt_spcreateerror ("clnt_create"));
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;
149 if (!(result = sm_unmon_all_1 (&my_id, client)))
150 die ("%s", clnt_sperror (client, "sm_unmon_all_1"));
152 free (my_id.my_name);
153 dprintf (N_DEBUG, "SM_UNMON_ALL request returned state: %d\n", result->state);
158 simulate_crash (char *host)
162 if ((client = clnt_create (host, SM_PROG, SM_VERS, "udp")) == NULL)
163 die ("%s", clnt_spcreateerror ("clnt_create"));
165 if (!sm_simu_crash_1 (NULL, client))
166 die ("%s", clnt_sperror (client, "sm_simu_crash_1"));
172 simulate_stat (char *calling, char *monitoring)
178 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
179 die ("%s", clnt_spcreateerror ("clnt_create"));
181 checking.mon_name = monitoring;
183 if (!(result = sm_stat_1 (&checking, client)))
184 die ("%s", clnt_sperror (client, "sm_stat_1"));
186 if (result->res_stat == STAT_SUCC)
187 dprintf (N_DEBUG, "STAT_SUCC from %s for %s, state: %d", calling,
188 monitoring, result->state);
190 dprintf (N_DEBUG, "STAT_FAIL from %s for %s, state: %d", calling,
191 monitoring, result->state);
199 note (N_FATAL, "Simulator caught signal %d, un-registering and exiting.", sig);
200 pmap_unset (sim_port, SIM_SM_VERS);
205 daemon_simulator (void)
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);
214 pmap_unset (sim_port, SIM_SM_VERS);
218 sim_sm_mon_1_svc (struct status *argp, struct svc_req *rqstp)
222 dprintf (N_DEBUG, "Recieved state %d for mon_name %s (opaque \"%s\")",
223 argp->state, argp->mon_name, argp->priv);
225 return ((void *)&result);