summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.lib/mipagent/snmp_appl.c
blob: a28dd89456b82c9da84aebdbda028a6fb25c2428 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * file: mipagentsnmp_appl.c
 *
 * This file contains the main SNMP routines used for
 * initialization and shutdown.
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdlib.h>
#include <syslog.h>

#include <impl.h>
#include <pagent.h>

#include "snmp_stub.h"
#include "mip.h"

/* GLOBAL VARIABLES */

#ifndef lint
char default_config_file[] = "/etc/snmp/conf/mipagent.reg";
char default_sec_config_file[] = "/etc/snmp/conf/mipagent.acl";
char default_error_file[] = "/var/snmp/mipagent.log";
#endif

static 	pthread_t snmpThreadId = 0;

typedef struct {
	int argc;
	char *argv;
} StartupArgs;

static void mipagent_snmp_thread(StartupArgs *args);

#define	DEFAULT_SNMP_PORT	161

extern ipaddr_t subagent_addr; /* this was set in setup.c */

extern int SSASubagentOpen(int, char *);
extern int SSARegSubagent(Agent *);
extern int SSAMain(int, char **);

/*
 * Function: register_with_master
 *
 * Arguments:	subagent_addr - Sub-Agent Address
 *
 * Description: This function will register the SNMP
 *		sub-agent with the master SNMP agent.
 *
 * Returns: int, 0 if successful
 */
static int
/* LINTED E_FUNC_ARG_UNUSED */
register_with_master(ipaddr_t subagent_addr)
{
	/* LINTED E_FUNC_VAR_UNUSED */
	Agent subagent;

	return (0);

#if 0
	/* NOTREACHED */

	agentid = SSASubagentOpen(0, "mipagent");
	(void) memset(&subagent, 0, sizeof (Agent));
	subagent.agent_id = agentid;

	/*
	 * Tell master we are ready
	 */
	subagent.agent_status = SSA_OPER_STATUS_ACTIVE;
	subagent.address.sin_family = AF_INET;
	subagent.address.sin_port = DEFAULT_SNMP_PORT;
	subagent.address.sin_addr.s_addr = subagent_addr;
	if ((SSARegSubagent(&subagent)) == 0) {
		return (-1);
	}
	return (0);
#endif
}

/*
 * Function: deregister_with_master
 *
 * Arguments:	subagent_addr - Sub-Agent Address
 *
 * Description: This function will inform the
 *		SNMP Master agent that we are
 *		going away.
 *
 * Returns: int, 0 if successful
 */
static int
/* LINTED E_FUNC_ARG_UNUSED */
deregister_with_master(ipaddr_t subagent_addr)
{
	/* LINTED E_FUNC_VAR_UNUSED */
	Agent subagent;

	return (0);
#if 0
	/* NOTREACHED */

	if (agentid) {
		(void) memset(&subagent, 0, sizeof (Agent));
		subagent.agent_id = agentid;

		/*
		 * Tell master we are going away
		 */
		subagent.agent_status = SSA_OPER_STATUS_DESTROY;
		subagent.address.sin_family = AF_INET;
		subagent.address.sin_port = DEFAULT_SNMP_PORT;
		subagent.address.sin_addr.s_addr = subagent_addr;
		if ((SSARegSubagent(&subagent)) == 0) {
			return (-1);
		}

		agentid = 0;
	}
	return (0);
#endif
}

#ifndef lint
/*
 * Function: agent_init
 *
 * Arguments:
 *
 * Description: Stub provided for the SNMP Sub-Agent
 *		initialization. We do not have anything
 *		to do here.
 *
 * Returns:
 */
void
agent_init()
{
}


/*
 * Function: agent_end
 *
 * Arguments:
 *
 * Description: Stub provided for the SNMP Sub-Agent
 *		shutdown. We do not have anything
 *		to do here.
 *
 * Returns:
 */
void
agent_end()
{
}


/*
 * Function: agent_loop
 *
 * Arguments:
 *
 * Description: The SNMP Loop function, which is
 *		provided because the sub-agent needs it.
 *
 * Returns:
 */
void
agent_loop()
{
}


/*
 * Function: agent_select_info
 *
 * Arguments:	fdset - File Descriptor set
 *		numfds - number of file descriptors
 *
 * Description: Another stub provided for the SNMP
 *		sub-agent.
 *
 * Returns:
 */
/* ARGSUSED */
void
agent_select_info(fd_set *fdset, int *numfds)
{
}


/*
 * Function: agent_select_callback
 *
 * Arguments:	fdset - File Descriptor set
 *
 * Description: Callback routine for the SNMP Sub-Agent,
 *		which we are providing as a stub.
 *
 * Returns:
 */
/* ARGSUSED */
void
agent_select_callback(fd_set *fdset)
{
}
#endif
/*
 * Function: startSNMPTaskThread
 *
 * Arguments: none
 *
 * Description: This function will start the SNMP
 *		sub-agent thread, and register with
 *		the Master SNMP thread.
 *
 * Returns: int, 0 if successful
 */
int
startSNMPTaskThread(void)
{
	StartupArgs *args;
	static char *all_args[3];
	static char arg0[] = "mipagent";
	static char arg1[] = "-d";
	static char arg2[] = "1";
	pthread_attr_t pthreadAttribute;
	int result;

	result = pthread_attr_init(&pthreadAttribute);
	if (result) {
		syslog(LOG_CRIT, "Error Initializing pthread.");
		return (-1);
	}

	args = (StartupArgs *) malloc(sizeof (StartupArgs));
	if (args == NULL) {
		syslog(LOG_CRIT, "Unable to allocate memory.");
		return (-1);
	}

	/*
	 * Call the SSAMain() with "-d 1" argument, so that it doesn't
	 * daemonize the process. "-d 1" attempts to generate debug output.
	 * When mipagent is daemonized, since the stdout is closed, this
	 * will have no effect. When mipagent is not deamonized for debugging
	 * purposes, this will generate useful debug info for SNMP.
	 */
	all_args[0] = (char *)arg0;
	all_args[1] = (char *)arg1;
	all_args[2] = (char *)arg2;

	args->argc = 3;
	args->argv = (char *)all_args;

	/*
	 * The thread is then started  It is mandatory that all
	 * applications have a slaveThread() function declared,
	 * which will be called with the TCB.
	 */
	result = pthread_create(&snmpThreadId, &pthreadAttribute,
	    (void *(*)()) mipagent_snmp_thread,
	    (void *)args);

	if (result) {
		syslog(LOG_CRIT, "pthread_create() failed.");
		free(args);
		return (-1);
	}

	/*
	 * In order for system resources the be properly cleaned up,
	 * we need to detach the thread. Otherwise, we need to wait for
	 * a pthread_join(), which we do not want.
	 */
	result = pthread_detach(snmpThreadId);

	if (result) {
		syslog(LOG_CRIT, "pthread_detach() failed.");
		free(args);
		return (-1);
	}

	return (0);

}

/*
 * Function: killSNMPTaskThread
 *
 * Arguments:
 *
 * Description: This function is used to shutdown the
 *		SNMP Sub-Agent thread.
 *
 * Returns:
 */
int
killSNMPTaskThread()
{
	int result;

	if (snmpThreadId) {
		if (deregister_with_master(subagent_addr)) {
			syslog(LOG_CRIT, "Unable to deregister with sub-agent");
		}

		/*
		 * Next we need to kill the dispatching thread.
		 */
		result = pthread_cancel(snmpThreadId);

		if (result) {
			/*
			 * Well, there's not much we can do here..
			 */
			syslog(LOG_CRIT, "Unable to kill snmp thread");
		}
	}

	return (0);
}

/*
 * Function: mipagent_snmp_thread
 *
 * Arguments:	args - Command Line Arguments
 *
 * Description: This function is the main SNMP Sub-Agent
 *		thread. The function will call SSAMain, which
 *		never returns (unless an error occured).
 *
 *		This thread will remain active until either
 *		the SNMP Sub-Agent main routines returns or
 *		the killSNMPTaskThread() function is called.
 *
 * Returns:
 */
static void
mipagent_snmp_thread(StartupArgs *args)
{
	int argc;
	int result;
	char **argv;

	/*
	 * Register ourselves as a sub-agent with the master SNMP
	 * agent.
	 */
	result = register_with_master(subagent_addr);

	if (result) {
		syslog(LOG_CRIT, "Could not register with master SNMP agent.");
		free(args);
		pthread_exit(NULL);
	}

	/*
	 * Let's do the SSAMain thingy... Note that this function should,
	 * in theory, never return. This function will interface with the
	 * SNMP Master Agent.
	 */
	argc = args->argc;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	argv = (char **)args->argv;

	SSAMain(argc, argv);

	free(args);

	if (deregister_with_master(subagent_addr)) {
		syslog(LOG_CRIT, "Unable to deregister with sub-agent");
	}

	pthread_exit(NULL);
}