File: foo.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (128 lines) | stat: -rw-r--r-- 3,247 bytes parent folder | download
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
/*
 * Copyright (c) 1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * This is a shared object. It is linked as an independent module that
 * is loaded into its own "address space" in an oskit kernel. All of
 * its external interfaces come through COM objects that are given to
 * it when the oskit kernel loads it. In other words, it is not linked
 * against the kernel when it is loaded, but contains its own C
 * library, Posix library, etc. The process runs in its own thread
 * context, although at this point the object itself is single threaded
 * since the kernel does not export the pthread interface. Signals don't
 * work either since the oskit kernel does not yet have a proper notion of
 * process context (and signals contain a lot "per-process" goop).
 */

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

/* dummy() is defined in a shared object. */
void	dummy(int);

/* Defined is dols.c */
void	dols(char *name, int lslong);

/* Simple network test */
int	resolve(char *b);

int
main(int argc, char **argv)
{
	int		i;
	void		(*func)(char *);
	void	       *handle;

	/*
	 * Print out our argument strings.
	 */
	for (i = 0; i < argc; i++) 
		printf("%s: arg:%d - %s\n", argv[0], i, argv[i]);

	/*
	 * Make sure dummy.so was loaded properly, since that was specified
	 * as a "NEEDED" object when the foo.so was linked. 
	 */
	dummy(69);

	/*
	 * Now load another .so file.
	 */
	if ((handle = dlopen("dopey.so", RTLD_NOW)) == NULL) {
		printf("dlopen failed: %s\n", dlerror());
		goto skip_dopey;
	}

	/*
	 * Find the symbol we want.
	 */
	if ((func = dlsym(handle, "dopey")) == NULL) {
		printf("dlsym failed: %s\n", dlerror());
		exit(1);
	}

	/*
	 * And call it.
	 */
	func("This is dopey");
	
	/*
	 * Lets unload it too.
	 */
	if (dlclose(handle) != NULL) {
		printf("dlclose failed: %s\n", dlerror());
		exit(1);
	}
 skip_dopey:
	
	/*
	 * Test out the filesystem interfaces.
	 */
	dols(".", 1);

	/*
	 * Test out the network interfaces.
	 */
	resolve("fast.cs.utah.edu");
	resolve("www.usps.gov");
	resolve("prep.ai.mit.edu");
	resolve("jackal");
	resolve("localhost");
	resolve("155.99.214.165");

	return 69;
}

#include <netdb.h>

int
resolve(char *b)
{
	struct hostent *he;
	printf("resolving %s...", b);
	fflush(stdout);
	he = gethostbyname(b);
	if (he) {
		printf("done.\n");
		printf("%s -> %s\n", b, inet_ntoa(*((long *)he->h_addr)));
		return 0;
	} else {
		printf("failed.\n");
		return -1;
	}
}