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)
43 xlog_open ("statd simulator");
46 if (!strcasecmp (*argv, "crash"))
47 simulate_crash (*(&argv[1]));
50 if (!strcasecmp (*argv, "stat"))
51 simulate_stat (*(&argv[1]), *(&argv[2]));
54 if (!strcasecmp (*argv, "unmon_all"))
55 simulate_unmon_all (*(&argv[1]), *(&argv[2]), *(&argv[3]));
58 if (!strcasecmp (*argv, "unmon"))
59 simulate_unmon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]));
62 if (!strcasecmp (*argv, "mon"))
63 simulate_mon (*(&argv[1]), *(&argv[2]), *(&argv[3]), *(&argv[4]),
66 xlog_err ("WTF? Give me something I can use!");
70 simulate_mon (char *calling, char *monitoring, char *as, char *proggy,
77 xlog (D_GENERAL, "Calling %s (as %s) to monitor %s", calling, as,
80 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
81 xlog_err ("%s", clnt_spcreateerror ("clnt_create"));
83 memcpy (mon.priv, fool, SM_PRIV_SIZE);
84 mon.mon_id.my_id.my_name = xstrdup (as);
85 sim_port = atoi (proggy) * SIM_SM_PROG;
86 mon.mon_id.my_id.my_prog = sim_port; /* Pseudo-dummy */
87 mon.mon_id.my_id.my_vers = SIM_SM_VERS;
88 mon.mon_id.my_id.my_proc = SIM_SM_MON;
89 mon.mon_id.mon_name = monitoring;
91 if (!(result = sm_mon_1 (&mon, client)))
92 xlog_err ("%s", clnt_sperror (client, "sm_mon_1"));
94 free (mon.mon_id.my_id.my_name);
96 if (result->res_stat != STAT_SUCC) {
97 xlog_err ("SM_MON request failed, state: %d", result->state);
99 xlog (D_GENERAL, "SM_MON result successful, state: %d\n", result->state);
100 xlog (D_GENERAL, "Waiting for callback");
107 simulate_unmon (char *calling, char *unmonitoring, char *as, char *proggy)
113 xlog (D_GENERAL, "Calling %s (as %s) to unmonitor %s", calling, as,
116 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
117 xlog_err ("%s", clnt_spcreateerror ("clnt_create"));
119 mon_id.my_id.my_name = xstrdup (as);
120 mon_id.my_id.my_prog = atoi (proggy) * SIM_SM_PROG;
121 mon_id.my_id.my_vers = SIM_SM_VERS;
122 mon_id.my_id.my_proc = SIM_SM_MON;
123 mon_id.mon_name = unmonitoring;
125 if (!(result = sm_unmon_1 (&mon_id, client)))
126 xlog_err ("%s", clnt_sperror (client, "sm_unmon_1"));
128 free (mon_id.my_id.my_name);
129 xlog (D_GENERAL, "SM_UNMON request returned state: %d\n", result->state);
134 simulate_unmon_all (char *calling, char *as, char *proggy)
140 xlog (D_GENERAL, "Calling %s (as %s) to unmonitor all hosts", calling, as);
142 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
143 xlog_err ("%s", clnt_spcreateerror ("clnt_create"));
145 my_id.my_name = xstrdup (as);
146 my_id.my_prog = atoi (proggy) * SIM_SM_PROG;
147 my_id.my_vers = SIM_SM_VERS;
148 my_id.my_proc = SIM_SM_MON;
150 if (!(result = sm_unmon_all_1 (&my_id, client)))
151 xlog_err ("%s", clnt_sperror (client, "sm_unmon_all_1"));
153 free (my_id.my_name);
154 xlog (D_GENERAL, "SM_UNMON_ALL request returned state: %d\n", result->state);
159 simulate_crash (char *host)
163 if ((client = clnt_create (host, SM_PROG, SM_VERS, "udp")) == NULL)
164 xlog_err ("%s", clnt_spcreateerror ("clnt_create"));
166 if (!sm_simu_crash_1 (NULL, client))
167 xlog_err ("%s", clnt_sperror (client, "sm_simu_crash_1"));
173 simulate_stat (char *calling, char *monitoring)
179 if ((client = clnt_create (calling, SM_PROG, SM_VERS, "udp")) == NULL)
180 xlog_err ("%s", clnt_spcreateerror ("clnt_create"));
182 checking.mon_name = monitoring;
184 if (!(result = sm_stat_1 (&checking, client)))
185 xlog_err ("%s", clnt_sperror (client, "sm_stat_1"));
187 if (result->res_stat == STAT_SUCC)
188 xlog (D_GENERAL, "STAT_SUCC from %s for %s, state: %d", calling,
189 monitoring, result->state);
191 xlog (D_GENERAL, "STAT_FAIL from %s for %s, state: %d", calling,
192 monitoring, result->state);
200 pmap_unset (sim_port, SIM_SM_VERS);
201 xlog_err ("Simulator caught signal %d, un-registering and exiting", sig);
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 xlog (D_GENERAL, "Recieved state %d for mon_name %s (opaque \"%s\")",
223 argp->state, argp->mon_name, argp->priv);
225 return ((void *)&result);