File: control.c

package info (click to toggle)
koala 0.3.2a-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,036 kB
  • ctags: 581
  • sloc: ansic: 29,450; makefile: 1,664; xml: 11
file content (276 lines) | stat: -rw-r--r-- 6,901 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
/* @(#) control.c 1.18 @(#) */
/***************************************************************\
*	Copyright (c) 1999 First Step Internet Services, Inc.
*		All Rights Reserved
*	Distributed under the BSD Licenese
*
*	Module: COMMANDS
\***************************************************************/

#define _KOALAMUD_COMMANDINFO_C "@(#) nitehawk@paranor.1ststep.net|lib/basecommand/control.c|20001105192643|10446 @(#)"

#include "autoconf.h"

#include "version.h"
#include "koalatypes.h"
#include "log.h"
#include "network.h"
#include "buffer.h"
#include "commands.h"
#include "uplinkprotocol.h"
#include "memory.h"
#include "llist.h"
#include "module.h"
#include "kparser.h"

/* Quit Command */
koalaerror do_quit(pdescriptor desc, argument *arglist[])
{
	char *quitmsg = "Thank you for trying KoalaMud\r\n";

	buffer_queue(desc, quitmsg, strlen(quitmsg));
	/* Make sure the buffer is emptied */
	buffer_sendbytes(desc, OUTRINGSIZE);

	desc->status = STATUS_CLOSE;

	return KESUCCESS;
}

/* Reboot command */
/* FIXME: we should accept a timer and wait until that time to send the
 * shutdown message to the appropriate node or set the shutdown flag
 */
koalaerror do_reboot(pdescriptor desc, argument *arglist[])
{
	char **ap, *argv[10], *line;
	int len = 120;
	koalaerror readerr;
	int argc = 0;
	int timer = 0;
	unsigned int destnode = 0;
	message_data msgdat;
	listnodeptr tmplist;
	pdescriptor tmpdesc;

	/* Read the rest of the line from the buffer */
	{
		/* Allocate memory */
		line = kmalloc(len, ALLOC_TEMP);
		if (!line)
		{
			logmsg(LOGCRIT, "Unable to allocate memory for line buffer");
			return KENOMEM;
		}

		/* Read data from the buffer */
		readerr = buffer_readline(desc, line, len);
		if (readerr == KENOTENOUGH)
		{
			kmfree(line, ALLOC_TEMP);
			return KENOTENOUGH;
		}
		if (readerr == KENOMEM)
		{
			buffer_flushline(desc);
		}
		
		/* Separate the input into words */
		for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
		{
			if (**ap != '\0')
			{
				if (++ap >= &argv[10])
				{
					break;
				}
				++argc;
			}
		}
	}

	/* We now have up to 10 arguments. argv[0] is the timer, argv[1] is the
	 * destination node to shutdown */
	if (argc == 0)
	{
		// No arguments, just shutdown current node right now
		kstate.running = DSTATE_REBOOT;
		kmfree(line, ALLOC_TEMP);
		return KESUCCESS;
	}
	else if (argc == 1)
	{
		destnode = (unsigned int)strtoul(argv[0], NULL, 10);
	}
	else if (argc >= 2)
	{
		timer = atoi(argv[0]);
		destnode = (unsigned int)strtoul(argv[1], NULL, 10);
	}

	if (destnode != 0)
	{
		tmplist = getdescriptorlist();
		for (;tmplist; tmplist = listnextnode(tmplist))
		{
			tmpdesc = tmplist->data.desc;
			
			if (tmpdesc->type == DESCRIPTOR_HUBSRV ||
					tmpdesc->type == DESCRIPTOR_ZONESRV ||
					tmpdesc->type == DESCRIPTOR_CLIENTSRV)
			{
				msgdat.data = NULL;
				uplinksendmessage(tmpdesc, destnode, MSGTYPE_REBOOT, &msgdat,
						0);
				kmfree(line, ALLOC_TEMP);
				return KESUCCESS;
			}
		}
	}

	/* We don't handle the timer yet */
	kstate.running = DSTATE_REBOOT;
	kmfree(line, ALLOC_TEMP);

	return KESUCCESS;
}

/* Shutdown command */
/* FIXME: we should accept a timer and wait until that time to send the
 * shutdown message to the appropriate node or set the shutdown flag
 */
koalaerror do_shutdown(pdescriptor desc, argument *arglist[])
{
	char **ap, *argv[10], *line;
	int len = 120;
	koalaerror readerr;
	int argc = 0;
	int timer = 0;
	unsigned int destnode = 0;
	message_data msgdat;
	listnodeptr tmplist;
	pdescriptor tmpdesc;

	/* Read the rest of the line from the buffer */
	{
		/* Allocate memory */
		line = kmalloc(len, ALLOC_TEMP);
		if (!line)
		{
			logmsg(LOGCRIT, "Unable to allocate memory for line buffer");
			return KENOMEM;
		}

		/* Read data from the buffer */
		readerr = buffer_readline(desc, line, len);
		if (readerr == KENOTENOUGH)
		{
			kmfree(line, ALLOC_TEMP);
			return KENOTENOUGH;
		}
		if (readerr == KENOMEM)
		{
			buffer_flushline(desc);
		}
		
		/* Separate the input into words */
		for (ap = argv; (*ap = strsep(&line, " \t")) != NULL;)
		{
			if (**ap != '\0')
			{
				if (++ap >= &argv[10])
				{
					break;
				}
				++argc;
			}
		}
	}

	/* We now have up to 10 arguments. argv[0] is the timer, argv[1] is the
	 * destination node to shutdown */

	if (argc >= 2)
	{
		timer = atoi(argv[0]);
		destnode = (unsigned int)strtoul(argv[1], NULL, 10);
	}
	else if (argc == 1)
	{
		destnode = (unsigned int)strtoul(argv[0], NULL, 10);
	}
	else if (argc == 0)
	{
		// No arguments, just shutdown current node right now
		kstate.running = DSTATE_SHUTDOWN;
		kmfree(line, ALLOC_TEMP);
		return KESUCCESS;
	}

	if (destnode != 0)
	{
		tmplist = getdescriptorlist();
		for (;tmplist; tmplist = listnextnode(tmplist))
		{
			tmpdesc = tmplist->data.desc;
			
			if (tmpdesc->type == DESCRIPTOR_HUBSRV ||
					tmpdesc->type == DESCRIPTOR_ZONESRV ||
					tmpdesc->type == DESCRIPTOR_CLIENTSRV)
			{
				msgdat.data = NULL;
				uplinksendmessage(tmpdesc, destnode, MSGTYPE_SHUTDOWN, &msgdat,
						0);
				kmfree(line, ALLOC_TEMP);
				return KESUCCESS;
			}
		}
	}

	/* We don't handle the timer yet */
	kstate.running = DSTATE_SHUTDOWN;
	kmfree(line, ALLOC_TEMP);

	return KESUCCESS;
}

/* Display memory status */
koalaerror do_memstat(pdescriptor desc, argument *arglist[])
{
	char buf[120];

	snprintf(buf, 120, "Current memory allocation (note, only the object "
		"counts are accurate.  The bytes remaining are\r\n"
		"not currently tracked precisely)\r\n");
	buffer_queue(desc, buf, strlen(buf));

	snprintf(buf, 120, "Generic memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_GENERIC].allocsize,
			memstate[ALLOC_GENERIC].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf, 120, "Database memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_DATABASE].allocsize,
			memstate[ALLOC_DATABASE].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf,120, "Descriptor memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_DESCRIPTOR].allocsize,
			memstate[ALLOC_DESCRIPTOR].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf, 120, "Temporary memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_TEMP].allocsize,
			memstate[ALLOC_TEMP].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf,120,"Linked List memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_LLIST].allocsize,
			memstate[ALLOC_LLIST].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf,120,"Command Table memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_CMDTABLE].allocsize,
			memstate[ALLOC_CMDTABLE].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	snprintf(buf,120,"String memory allocated: %d bytes in %d objects\r\n",
			memstate[ALLOC_STRING].allocsize,
			memstate[ALLOC_STRING].numobjects);
	buffer_queue(desc, buf, strlen(buf));
	return KESUCCESS;
}