File: ionrestart.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,768 kB
  • ctags: 11,049
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (375 lines) | stat: -rw-r--r-- 6,985 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
	ionrestart.c:	Stops ION, re-initializes all volatile
			databases, then restarts ION.  The purpose
			of this utility is to wipe out potentially
			corrupt volatile database information in the
			event that an ION transaction is reversed:
			transaction reversal protects non-volatile
			database integrity but does not realign the
			volatile database with the repaired heap.
									*/
/*									*/
/*	Copyright (c) 2012, California Institute of Technology.		*/
/*	All rights reserved.						*/
/*	Author: Scott Burleigh, Jet Propulsion Laboratory		*/
/*									*/

#include "platform.h"
#include "sdrP.h"
#include "rfx.h"
#include "ltpP.h"
#include "bpP.h"
#include "cgr.h"
#ifndef NASA_PROTECTED_FLIGHT_CODE
#include "cfdpP.h"
#endif

#ifndef RESTART_GRACE_PERIOD
#define	RESTART_GRACE_PERIOD	3
#endif

#define	RESTART_LOOP_INTERVAL	(RESTART_GRACE_PERIOD * 5)

extern void	ionDropVdb();
extern void	ionRaiseVdb();

static void	restartION(Sdr sdrv, char *utaCmd)
{
	int	i;
	int	restart_bp = 1;
	int	restart_ltp = 1;
#ifndef NASA_PROTECTED_FLIGHT_CODE
	int	restart_cfdp = 1;
#endif
	time_t	prevRestartTime;

	/*	Stop all tasks.						*/
#ifndef NASA_PROTECTED_FLIGHT_CODE
	if (cfdpAttach() < 0)
	{
		restart_cfdp = 0;
		writeMemo("[!] ionrestart can't attach to CFDP.");
	}
	else
	{
		cfdpStop();
		for (i = 0; i < 5; i++)
		{
			if (cfdp_entity_is_started())
			{
				snooze(1);
				continue;	/*	Not stopped.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: CFDP not stopped.");
		}
		else
		{
			writeMemo("[i] ionrestart: CFDP stopped.");
		}
	}
#endif
	if (bpAttach() < 0)
	{
		restart_bp = 0;
		writeMemo("[!] ionrestart can't attach to BP.");
	}
	else
	{
		bpStop();
		for (i = 0; i < 5; i++)
		{
			if (bp_agent_is_started())
			{
				snooze(1);
				continue;	/*	Not stopped.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: BP not stopped.");
		}
		else
		{
			writeMemo("[i] ionrestart: BP stopped.");
		}
	}

	if (ltpAttach() < 0)
	{
		restart_ltp = 0;
		writeMemo("[!] ionrestart can't attach to LTP.");
	}
	else
	{
		ltpStop();
		for (i = 0; i < 5; i++)
		{
			if (ltp_engine_is_started())
			{
				snooze(1);
				continue;	/*	Not stopped.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: LTP not stopped.");
		}
		else
		{
			writeMemo("[i] ionrestart: LTP stopped.");
		}
	}

	rfx_stop();
	for (i = 0; i < 5; i++)
	{
		if (rfx_system_is_started())
		{
			snooze(1);
			continue;		/*	Not stopped.	*/
		}

		break;
	}

	if (i == 5)
	{
		writeMemo("[!] ionrestart: rfxclock not stopped.");
	}
	else
	{
		writeMemo("[i] ionrestart: rfxclock stopped.");
	}

	/*	Terminate all remaining tasks by ending the
	 *	transaction semaphore.					*/

	sm_SemEnd(sdrv->sdr->sdrSemaphore);

	/*	Drop all volatile databases.				*/

#ifndef NASA_PROTECTED_FLIGHT_CODE
	if (restart_cfdp)
	{
		cfdpDropVdb();
		writeMemo("[i] ionrestart: CFDP volatile database dropped.");
	}
#endif
	cgr_stop();
	if (restart_bp)
	{
		bpDropVdb();
		writeMemo("[i] ionrestart: BP volatile database dropped.");
	}

	if (restart_ltp)
	{
		ltpDropVdb();
		writeMemo("[i] ionrestart: LTP volatile database dropped.");
	}

	ionDropVdb();
	writeMemo("[i] ionrestart: ION volatile database dropped.");

	/*	Un-end the transaction semaphore.			*/

	sm_SemUnend(sdrv->sdr->sdrSemaphore);

	/*	Now re-create all of the volatile databases.		*/

	ionRaiseVdb();
	writeMemo("[i] ionrestart: ION volatile database raised.");
	if (restart_ltp)
	{
		ltpRaiseVdb();
		writeMemo("[i] ionrestart: LTP volatile database raised.");
	}

	if (restart_bp)
	{
		bpRaiseVdb();
		writeMemo("[i] ionrestart: BP volatile database raised.");
	}

	cgr_start();
#ifndef NASA_PROTECTED_FLIGHT_CODE
	if (restart_cfdp)
	{
		cfdpRaiseVdb();
		writeMemo("[i] ionrestart: CFDP volatile database raised.");
	}
#endif
	/*	If it's safe, restart all ION tasks.			*/

	prevRestartTime = sdrv->sdr->restartTime;
	sdrv->sdr->restartTime = getUTCTime();
	if ((sdrv->sdr->restartTime - prevRestartTime) < RESTART_LOOP_INTERVAL)
	{
		writeMemo("[!] Inferred restart loop.  Tasks not restarted.");
		return;
	}

	rfx_start();
	for (i = 0; i < 5; i++)
	{
		if (!rfx_system_is_started())
		{
			snooze(1);
			continue;		/*	Not started.	*/
		}

		break;
	}

	if (i == 5)
	{
		writeMemo("[!] ionrestart: rfxclock not restarted.");
	}
	else
	{
		writeMemo("[i] ionrestart: rfxclock restarted.");
	}

	if (restart_ltp)
	{
		ltpStart();
		for (i = 0; i < 5; i++)
		{
			if (!ltp_engine_is_started())
			{
				snooze(1);
				continue;	/*	Not started.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: LTP not restarted.");
		}
		else
		{
			writeMemo("[i] ionrestart: LTP restarted.");
		}
	}

	if (restart_bp)
	{
		bpStart();
		for (i = 0; i < 5; i++)
		{
			if (!bp_agent_is_started())
			{
				snooze(1);
				continue;	/*	Not started.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: BP not restarted.");
		}
		else
		{
			writeMemo("[i] ionrestart: BP restarted.");
		}
	}

#ifndef NASA_PROTECTED_FLIGHT_CODE
	if (restart_cfdp)
	{
		cfdpStart(utaCmd);
		for (i = 0; i < 5; i++)
		{
			if (!cfdp_entity_is_started())
			{
				snooze(1);
				continue;	/*	Not started.	*/
			}

			break;
		}

		if (i == 5)
		{
			writeMemo("[!] ionrestart: CFDP not restarted.");
		}
		else
		{
			writeMemo("[i] ionrestart: CFDP restarted.");
		}
	}
#endif
}

#if defined (VXWORKS) || defined (RTEMS) || defined (bionic)
int	ionrestart(int a1, int a2, int a3, int a4, int a5,
		int a6, int a7, int a8, int a9, int a10)
{
	char		*utaCmd = a1 ? (char *) a1 : "bputa";
#else
int	main(int argc, char **argv)
{
	char		*utaCmd = argc > 1 ? argv[1] : "bputa";
#endif
	Sdr		sdrv;
	sm_SemId	sdrSemaphore;

	if (ionAttach() < 0)
	{
		putErrmsg("ionrestart can't attach to ION.", NULL);
		return 1;
	}

	/*	Hijack the current transaction, i.e., impersonate
	 *	the current owner of the ION mutex.
	 *
	 *	ionAttach() entails calling sdr_start_using, which
	 *	gives the SDR semaphore and thereby would enable the
	 *	failing task to begin new transactions before exiting.
	 *	These new transactions would interfere with recovery
	 *	from the current failed transaction, so they must be
	 *	prevented.  For this purpose, we must temporarily set
	 *	the SDR semaphore to -1, restoring it when we are
	 *	confident that the failing task has terminated.		*/

	sdrv = getIonsdr();
	sdrv->sdr->sdrOwnerTask = sm_TaskIdSelf();
	sdrv->sdr->sdrOwnerThread = pthread_self();
	sdrSemaphore = sdrv->sdr->sdrSemaphore;
	sdrv->sdr->sdrSemaphore = -1;

	/*	Wait for the failing task to terminate, then re-enable
	 *	transactions and perform the restart.			*/

	snooze(RESTART_GRACE_PERIOD);
     	sdrv->sdr->sdrSemaphore = sdrSemaphore;
	restartION(sdrv, utaCmd);

	/*	Close out the hijacked transaction.			*/

	sdrv->sdr->xnDepth = 1;
	sdrv->modified = 0;
	sdr_exit_xn(sdrv);

	/*	Terminate.						*/

	ionDetach();
	writeMemo("[i] ionrestart: finished restarting ION.");
	return 0;
}