linktest.c 4.67 KB
Newer Older
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2004 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * All rights reserved.
 */

#include <stdio.h>
#include <ctype.h>
#include <netdb.h>
#include <unistd.h>
#include <paths.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pwd.h>
#include <time.h>
#include "tbdefs.h"
#include "log.h"
#include "event.h"
22
#include "linktest.h"
23

24
static int	debug;
25
26
27
static void	callback(event_handle_t handle,
			 event_notification_t notification, void *data);

28
static void
29
start_linktest(char *args, int);
30
31
32
33
34

void
usage(char *progname)
{
	fprintf(stderr,
35
		"Usage: %s [-d] "
36
		"[-s server] [-p port] [-k keyfile] [-l logfile] -e pid/eid\n",
37
38
39
40
		progname);
	exit(-1);
}

41
42
int
main(int argc, char **argv) {
43
44
45
46
	event_handle_t handle;
	address_tuple_t	tuple;
	char *server = NULL;
	char *port = NULL;
47
	char *keyfile = NULL;
48
	char *pideid = NULL;
49
	char *pidfile = NULL;
50
51
52
53
54
55
	char *logfile = NULL;
	char *progname;
	char c;
	char buf[BUFSIZ];
	
	progname = argv[0];
56

57
	while ((c = getopt(argc, argv, "s:p:e:l:dk:i:")) != -1) {
58
	  switch (c) {
59
60
61
	  case 'd':
	    debug++;
	    break;
62
63
64
65
66
67
68
69
70
	  case 's':
	    server = optarg;
	    break;
	  case 'p':
	    port = optarg;
	    break;
	  case 'e':
	    pideid = optarg;
	    break;
71
72
73
	  case 'i':
	    pidfile = optarg;
	    break;
74
75
76
	  case 'l':
	    logfile = optarg;
	    break;
77
78
79
	  case 'k':
	    keyfile = optarg;
	    break;
80
81
82
83
84
85
86
87
	  default:
	    usage(progname);
	  }
	}

	if (!pideid)
	  usage(progname);

88
89
90
91
92
93
94
95
96
	if (debug)
		loginit(0, 0);
	else {
		if (logfile)
			loginit(0, logfile);
		else
			loginit(1, "linktest");
		/* See below for daemonization */
	}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

	/*
	 * Convert server/port to elvin thing.
	 *
	 * XXX This elvin string stuff should be moved down a layer. 
	 */
	if (server) {
		snprintf(buf, sizeof(buf), "elvin://%s%s%s",
			 server,
			 (port ? ":"  : ""),
			 (port ? port : ""));
		server = buf;
	}

	/*
	 * Construct an address tuple for subscribing to events for
	 * this node.
	 */
	tuple = address_tuple_alloc();
	if (tuple == NULL) {
		fatal("could not allocate an address tuple");
	}
	/*
120
	 * Ask for just the events we care about. 
121
122
123
124
125
126
127
128
	 */
	tuple->expt      = pideid;
	tuple->objtype   = TBDB_OBJECTTYPE_LINKTEST;
	tuple->eventtype = ADDRESSTUPLE_ANY;

	/*
	 * Register with the event system. 
	 */
129
	handle = event_register_withkeyfile(server, 0, keyfile);
130
	if (handle == NULL) {
131
	  fatal("could not register with event system");
132
133
134
135
136
137
138
139
	}
	
	/*
	 * Subscribe to the event we specified above.
	 */
	if (! event_subscribe(handle, callback, tuple, NULL)) {
		fatal("could not subscribe to event");
	}
140
141
142
143
144
145

	/*
	 * Do this now, once we have had a chance to fail on the above
	 * event system calls.
	 */
	if (!debug)
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
		daemon(0, 1);

	/*
	 * Write out a pidfile if root (after we daemonize).
	 */
	if (!getuid()) {
		FILE *fp;
		
		if (pidfile)
			strcpy(buf, pidfile);
		else
			sprintf(buf, "%s/linktest.pid", _PATH_VARRUN);
		fp = fopen(buf, "w");
		if (fp != NULL) {
			fprintf(fp, "%d\n", getpid());
			(void) fclose(fp);
		}
	}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	/*
	 * Begin the event loop, waiting to receive event notifications:
	 */
	event_main(handle);

	/*
	 * Unregister with the event system:
	 */
	if (event_unregister(handle) == 0) {
		fatal("could not unregister with event system");
	}

	return 0;
}
/*
 * Handle the events.
 */
static void
callback(event_handle_t handle, event_notification_t notification, void *data)
{
	char		objname[TBDB_FLEN_EVOBJTYPE];
	char		event[TBDB_FLEN_EVEVENTTYPE];
	char		args[BUFSIZ];
	struct timeval	now;

	gettimeofday(&now, NULL);
	
	if (! event_notification_get_objname(handle, notification,
					     objname, sizeof(objname))) {
		error("Could not get objname from notification!\n");
		return;
	}

	if (! event_notification_get_eventtype(handle, notification,
					       event, sizeof(event))) {
		error("Could not get event from notification!\n");
		return;
	}

	event_notification_get_arguments(handle,
					 notification, args, sizeof(args));

207
208
/*        info("Event: %lu:%d %s %s %s\n", now.tv_sec, now.tv_usec,
	     objname, event, args);*/
209
210
211
	/*
	 * Dispatch the event. 
	 */
212
	if (strcmp(event, TBDB_EVENTTYPE_START) == 0) {
213
          start_linktest(args, sizeof(args));
214
	}
215
216

}
217

218
/* convert arguments from the event into a form for Linktest.
219
220
221
222
223
224
225
 */
static void
start_linktest(char *args, int buflen) {
  pid_t lt_pid;
  int status;
  char *word;
  char *argv[MAX_ARGS];
226
227
  int i=1;
  
228
229
230
231
232
233
  word = strtok(args," \t");
  do {
    argv[i++] = word;
  } while ((word = strtok(NULL," \t"))
           && (i<MAX_ARGS));
  argv[i] = NULL;
234
  argv[0] = LINKTEST_SCRIPT;
235
/*  info("starting linktest.\n");*/
236
237
238
239
240
241
  
  lt_pid = fork();
  if(!lt_pid) {
    execv( LINKTEST_SCRIPT,argv);
  }
  waitpid(lt_pid, &status, 0);
242
/*  info("linktest completed.\n");*/
243
}
244