Esempio n. 1
0
/*------------------------------------------------------------------------------------------------------------------
--      FUNCTION: init_transfer
--
--      DATE: Febuary 6 2014
--      REVISIONS: none
--
--      DESIGNER: Ramzi Chennafi
--      PROGRAMMER: Ramzi Chennafi
--
--      INTERFACE: void init_transfer(HWND hwnd), takes the parent window HWND.
--
--      RETURNS: void
--
--      NOTES:
--      Intializes a UDP or TCP transfer based on the current settings. Will generate dummy packets if the program is in
--		dummy packet transfer mode, otherwise reads a file and generates packets for the file. Passes the created packets
--		to their respective UDP/TCP functions.
----------------------------------------------------------------------------------------------------------------------*/
void init_transfer(HWND hwnd){

	SETTINGS * st = (SETTINGS*)GetClassLongPtr(hwnd, 0);
	int status;
	char msg[MAX_SIZE];
	HANDLE hf;
	DWORD numBytesRead = 0;
	int  totalBytesRead = 0;
	DWORD packetsizes[] = {1024, 4096, 20000, 60000};
	const int packet_size = packetsizes[st->packet_size];
	
	if (st->mode == FILEMODE){
		grab_file(hwnd, &hf);
	}

	activity("Sending data...\n", EB_STATUSBOX);
	WSABUF * wsaBuffers = (LPWSABUF)malloc(sizeof(WSABUF)* MAX_PACKETS);
	
	char * buff = (char*)malloc(sizeof(char)* packet_size);

	int i = 0, buffer_count = 0;
	while (1){
		if (st->mode == FILEMODE){
			status = ReadFile(hf, buff, packet_size, &numBytesRead, NULL);
			if (numBytesRead == 0){
				CloseHandle(hf);
				break;
			}
			wsaBuffers[i].buf = buff;
			wsaBuffers[i++].len = numBytesRead;
			totalBytesRead += numBytesRead;
			buffer_count++;
		}
		else{
			buffer_count = atoi(st->times_to_send);
			
			for (int g = 0; g < packet_size; g++){
				buff[g] = 'p';
			}
			buff[packet_size - 1] = '\0';

			for (int p = 0; p < buffer_count; p++){
				wsaBuffers[p].buf = buff;
				wsaBuffers[p].len = packet_size;
			}
			totalBytesRead = packet_size * buffer_count;
			break;
		}
	}
	
	sprintf_s(msg, "Sending %d bytes in %d packets.\n", totalBytesRead, buffer_count);
	activity(msg, EB_STATUSBOX);

	if(st->protocol == UDP)
		udp_deliver_packets(hwnd, totalBytesRead, packet_size, buffer_count, wsaBuffers);
	else
		tcp_deliver_packets(hwnd, wsaBuffers, st->client_socket, totalBytesRead, packet_size, buffer_count, st->mode);

	free(wsaBuffers);
}
Esempio n. 2
0
tx *get_tx(const bitcoin_txid &txid, bool must_exist)
{
	char filename[sizeof("txcache/01234567890123456789012345678901234567890123456789012345678901234567")] = "txcache/";
	char *txstring;
	const u8 *txbytes;
	u8 *bytes;
	size_t len;
	u64 fee;
	tx *t;

	txstring = filename + strlen("txcache/");
	if (!hex_encode(txid.shad.sha.u.u8, sizeof(txid.shad.sha.u.u8),
					txstring,
					sizeof(filename) - strlen("txcache/")))
		throw std::logic_error("txid doesn't fit in filename");

	txbytes = bytes = (u8 *)grab_file(NULL, filename);
	if (!txbytes) {
		if (must_exist)
			errx(1, "Could not find tx %s", txstring);
		return NULL;
	}

	len = tal_count(txbytes)-1;
	if (len < 8)
		errx(1, "Truncated %s", filename);
	memcpy(&fee, txbytes, 8);
	txbytes += 8;
	len -= 8;
	t = new tx(fee, new bitcoin_tx(&txbytes, &len));
	assert(t->txid == txid);
	tal_free(bytes);
	return t;
}
Esempio n. 3
0
//------------------------------------ main ------------------------------------
int main(int argc,char *argv[])
{
 int ret;
 unsigned long len;
 void* file;
 char* filename;
 char* progname=argv[0];
 char* options="";
 filename=argv[1];
 if(!filename)
  print_usage(progname);
 file=grab_file(filename,&len);
 if(!file)
 {
  exit(1);
 }
// DevInt3();
 ret=init_module(file,len,options);
 if(ret!=0)
 {
  exit(1);
 }
 exit(0);
 return 0;
}
Esempio n. 4
0
		list_for_each(list, i, list) {
			char *err, *output;
			const char *options;

			score->total++;
			options = concat(score,
					 per_file_options(&tests_pass_valgrind,
							  i));
			if (streq(options, "FAIL")) {
				i->leak_info = NULL;
				continue;
			}

			output = grab_file(i, i->valgrind_log);
			/* No valgrind errors? */
			if (!output || output[0] == '\0') {
				err = NULL;
				i->leak_info = NULL;
			} else {
				i->leak_info = analyze_output(output, &err);
			}
			if (err) {
				score_file_error(score, i, 0, "%s", err);
				score->pass = false;
			} else
				score->score++;
		}
Esempio n. 5
0
/* Be careful about trying to compile over running programs (parallel make).
 * temp_file helps here. */
char *compile_info(const void *ctx, const char *dir)
{
	char *info_c_file, *info, *compiled, *output;
	int fd;

	/* Copy it to a file with proper .c suffix. */
	info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
	if (!info)
		return NULL;

	info_c_file = temp_file(ctx, ".c", "_info");
	fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
	if (fd < 0)
		return NULL;
	if (!write_all(fd, info, tal_count(info)-1))
		return NULL;

	if (close(fd) != 0)
		return NULL;

	compiled = temp_file(ctx, "", "info");
	if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
			     compiler, cflags, "", compiled, &output))
		return compiled;
	return NULL;
}
Esempio n. 6
0
const char *get_ccan_file_contents(struct ccan_file *f)
{
	if (!f->contents) {
		f->contents = grab_file(f, f->fullname);
		if (!f->contents)
			err(1, "Reading file %s", f->fullname);
		f->contents_size = tal_count(f->contents) - 1;
	}
	return f->contents;
}
Esempio n. 7
0
int main(int argc, char *argv[])
{
	int i;
	long int ret;
	unsigned long len;
	void *file;
	char *filename, *options = strdup("");
	char *progname = argv[0];

	if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
		puts("klibc insmod");
		exit(0);
	}

	/* Ignore old options, for backwards compat. */
	while (argv[1] && (streq(argv[1], "-p")
			   || streq(argv[1], "-s")
			   || streq(argv[1], "-f"))) {
		argv++;
		argc--;
	}

	filename = argv[1];
	if (!filename)
		print_usage(progname);

	/* Rest is options */
	for (i = 2; i < argc; i++) {
		options = realloc(options,
				  strlen(options) + 2 + strlen(argv[i]) + 2);
		/* Spaces handled by "" pairs, but no way of escaping
		   quotes */
		if (strchr(argv[i], ' '))
			strcat(options, "\"");
		strcat(options, argv[i]);
		if (strchr(argv[i], ' '))
			strcat(options, "\"");
		strcat(options, " ");
	}

	file = grab_file(filename, &len);
	if (!file) {
		fprintf(stderr, "insmod: can't read '%s': %s\n",
			filename, strerror(errno));
		exit(1);
	}

	ret = init_module(file, len, options);
	if (ret != 0) {
		fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
			filename, ret, moderror(errno));
		exit(1);
	}
	exit(0);
}
Esempio n. 8
0
int main(int argc, char *argv[])
{
	unsigned int i;
	long int ret;
	unsigned long len;
	void *file;
	char *filename, *options = strdup("");
	char *progname = argv[0];

	if (strstr(argv[0], "insmod.static"))
		try_old_version("insmod.static", argv);
	else
		try_old_version("insmod", argv);

	if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
		puts(PACKAGE " version " VERSION);
		exit(0);
	}

	/* Ignore old options, for backwards compat. */
	while (argv[1] && (streq(argv[1], "-p")
			   || streq(argv[1], "-s")
			   || streq(argv[1], "-f"))) {
		argv++;
		argc--;
	}

	filename = argv[1];
	if (!filename)
		print_usage(progname);

	/* Rest is options */
	for (i = 2; i < argc; i++) {
		options = realloc(options,
				  strlen(options) + 1 + strlen(argv[i]) + 1);
		strcat(options, argv[i]);
		strcat(options, " ");
	}

	file = grab_file(filename, &len);
	if (!file) {
		fprintf(stderr, "insmod: can't read '%s': %s\n",
			filename, strerror(errno));
		exit(1);
	}

	ret = init_module(file, len, options);
	if (ret != 0) {
		fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
			filename, ret, moderror(errno));
		exit(1);
	}
	exit(0);
}
Esempio n. 9
0
bool move_file(const char *oldname, const char *newname)
{
	char *contents;
	size_t size;
	int fd;
	bool ret;

	if (tools_verbose)
		printf("Moving file %s to %s: ", oldname, newname);

	/* Simple case: rename works. */
	if (rename(oldname, newname) == 0) {
		if (tools_verbose)
			printf("rename worked\n");
		return true;
	}

	/* Try copy and delete: not atomic! */
	contents = grab_file(NULL, oldname, &size);
	if (!contents) {
		if (tools_verbose)
			printf("read failed: %s\n", strerror(errno));
		return false;
	}

	fd = open(newname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
	if (fd < 0) {
		if (tools_verbose)
			printf("output open failed: %s\n", strerror(errno));
		ret = false;
		goto free;
	}

	ret = write_all(fd, contents, size);
	if (close(fd) != 0)
		ret = false;

	if (ret) {
		if (tools_verbose)
			printf("copy worked\n");
		unlink(oldname);
	} else {
		if (tools_verbose)
			printf("write/close failed\n");
		unlink(newname);
	}

free:
	talloc_free(contents);
	return ret;
}
Esempio n. 10
0
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx, const char *filename)
{
	char *hex;
	struct bitcoin_tx *tx;

	/* Grabs file, add nul at end. */
	hex = grab_file(ctx, filename);
	if (!hex)
		err(1, "Opening %s", filename);

	tx = bitcoin_tx_from_hex(ctx, hex, strlen(hex));
	if (!tx)
		err(1, "Failed to decode tx '%s'", hex);
	tal_free(hex);
	return tx;
}
Esempio n. 11
0
static void setup_words(void)
{
	char *wordfile;

	/* read in the words */
	wordfile = grab_file(NULL, "test/api-lazytrie-words.txt");
	ok1(wordfile);
	wordarray = tal_strsplit(NULL, take(wordfile), "\n", STR_NO_EMPTY);
	ok1(wordarray);
	nwords = tal_count(wordarray) - 1;
	diag("lazytrie: %d words loaded", nwords);
	ok1(nwords == NWORDS);

	wordarrayb = tal_arr(NULL, char *, nwords);
	memcpy(wordarrayb, wordarray, tal_count(wordarrayb) * sizeof(char *));

	qsort(wordarrayb, nwords, sizeof(char *), lensort);
}
Esempio n. 12
0
char *read_config_header(const char *ccan_dir, bool verbose)
{
	char *fname = path_join(NULL, ccan_dir, "config.h");
	char **lines;
	unsigned int i;
	char *config_header;

	config_header = grab_file(NULL, fname);
	tal_free(fname);

	if (!config_header)
		return NULL;

	lines = tal_strsplit(config_header, config_header, "\n", STR_EMPTY_OK);
	for (i = 0; i < tal_count(lines) - 1; i++) {
		char *sym;
		const char **line = (const char **)&lines[i];

		if (!get_token(line, "#"))
			continue;
		if (!get_token(line, "define"))
			continue;
		sym = get_symbol_token(lines, line);
		if (streq(sym, "CCAN_COMPILER")) {
			compiler = demangle_string(lines[i]);
			if (!compiler)
				errx(1, "%s:%u:could not parse CCAN_COMPILER",
				     fname, i+1);
			if (verbose)
				printf("%s: compiler set to '%s'\n",
				       fname, compiler);
		} else if (streq(sym, "CCAN_CFLAGS")) {
			cflags = demangle_string(lines[i]);
			if (!cflags)
				errx(1, "%s:%u:could not parse CCAN_CFLAGS",
				     fname, i+1);
			if (verbose)
				printf("%s: compiler flags set to '%s'\n",
				       fname, cflags);
		}
	}
	return config_header;
}
Esempio n. 13
0
int 
main(int argc, char *argv[])
{
	unsigned int	i;
	char 		**split, *str;
	int 		length;
	struct 		stat st;

	str = grab_file(NULL, "ccan/grab_file/test/run-grab.c", NULL);
	split = strsplit(NULL, str, "\n", NULL);
	length = strlen(split[0]);
	ok1(streq(split[0], "/* This is test for grab_file() function"));
	for (i = 1; split[i]; i++)	
		length += strlen(split[i]);
	ok1(streq(split[i-1], "/* End of grab_file() test */"));
	if (stat("ccan/grab_file/test/run-grab.c", &st) != 0) 
		err(1, "Could not stat self");
	ok1(st.st_size == length + i);
	
	return 0;
}
Esempio n. 14
0
int insmod(const char *filename, const char *option)
{
	long int ret;
	unsigned long len;
	void *file;

	file = grab_file(filename, &len);
	if (!file) {
		fprintf(stderr, "insmod: can't read '%s': %s\n",
			filename, strerror(errno));
		return  0;
	}
	
	ret = init_module(file, len, option);
	if (ret != 0) {
		fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
			filename, ret, moderror(errno));
		return 0;
	}
	
	return 1;
}
Esempio n. 15
0
int main(int argc, char *argv[])
{
	unsigned int i;
	const char *type;
	const char *function = NULL;

	if (argc < 3)
		errx(1, "Usage: doc_extract [--function=<funcname>] TYPE <file>...\n"
		     "Where TYPE is functions|author|license|maintainer|summary|description|example|see_also|all");

	if (strstarts(argv[1], "--function=")) {
		function = argv[1] + strlen("--function=");
		argv++;
		argc--;
	}

	type = argv[1];
	for (i = 2; i < argc; i++) {
		char *file, **lines;
		struct list_head *list;
		struct doc_section *d;

		file = grab_file(NULL, argv[i], NULL);
		if (!file)
			err(1, "Reading file %s", argv[i]);
		lines = strsplit(file, file, "\n");

		list = extract_doc_sections(lines);
		if (list_empty(list))
			errx(1, "No documentation in file %s", argv[i]);
		talloc_free(file);

		if (streq(type, "functions")) {
			const char *last = NULL;
			list_for_each(list, d, list) {
				if (d->function) {
					if (!last || !streq(d->function, last))
						printf("%s\n", d->function);
					last = d->function;
				}
			}
		} else {
			unsigned int j;
			list_for_each(list, d, list) {
				if (function) {
					if (!d->function)
						continue;
					if (!streq(d->function, function))
						continue;
				}
				if (streq(type, "all"))
					printf("%s:\n", d->type);
				else if (!typematch(d->type, type))
					continue;

				for (j = 0; j < d->num_lines; j++)
					printf("%s\n", d->lines[j]);
			}
		}
		talloc_free(list);
	}
Esempio n. 16
0
/**
 * @brief Lynx-like driver installation routine.
 *
 * @param drvr_fn -- driver path (.ko object)
 * @param type    -- @ref BLOCKDRIVER or @ref CHARDRIVER\n
 *                   Special case is possible, where user can pass driver option
 *                   address (casted to @e int) in a string representation.
 *                   It will be passed to the init_module() system call as an
 *                   option to the driver installation (3-rd parameter) instead
 *                   of a standart one - which is a driver name.
 *
 * Will install the driver in the system. Supposed to be called @b ONLY once
 * during installation procedure.
 * @b NOTE If this routine will be called more then once during installation -
 *         then it will terminate installation programm
 *
 * @return driver ID (major number) - if successful
 * @return -1                       - if not
 */
int dr_install(char *drvr_fn, int type)
{
	char *drvr_nm = NULL;	/* extracted driver name */
	int rc = 0;		/* return code */
	char *node_nm = NULL;	/* driver node name */

	void *drvrfile = NULL; /* our .ko */
	FILE *procfd   = NULL;
	unsigned long len;
	char *options = NULL; /* driver option */
	long int ret;
	char buff[32], *bufPtr; /* for string parsing */
	int major_num = 0;
	int devn; /* major/minor device number */
	char *hotplug = NULL;

	drvr_nm = extract_driver_name(drvr_fn);
	if (!drvr_nm) {
		fprintf(stderr, "Wrong [.ko] object file name\n");
		return -1;
	}

	/* put .ko in the local buffer */
	if (!(drvrfile = grab_file(drvr_fn, &len))) {
		fprintf(stderr, "Can't read '%s': %s\n",
			drvr_fn, strerror(errno));
		rc = -1;
		goto dr_install_exit;
	}

	/* check for special case. Will overwrite standart one! */
	if (!WITHIN_RANGE(CHARDRIVER, type, BLOCKDRIVER))
		/* this IS a special case. Setup user-defined
		   extra options along with driver name */
		asprintf(&options, "dname=%s %s", drvr_nm, (char*)type);
	else {
		asprintf(&options, "dname=%s", drvr_nm);
	}

	disable_kernel_hotplug(&hotplug); /* move mdev out-of-the-way */

	/* insert module in the kernel */
	if ( (ret = init_module(drvrfile, len, options)) != 0 ) {
		fprintf(stderr, "Error inserting '%s' module: %li %s\n",
			drvr_fn, ret, moderror(errno));
		rc = -1;
		goto dr_install_exit;
	}

	/* check if he made it */
	if ( (procfd = fopen("/proc/devices", "r")) == NULL ) {
		mperr("Can't open /proc/devices");
		rc = -1;
		goto dr_install_exit;
	}

	/* search for the device driver entry */
	bufPtr = buff;
	while (fgets(buff, sizeof(buff), procfd))
		if (strstr(buff, drvr_nm)) { /* bingo! */
			major_num = atoi(strsep(&bufPtr, " "));
			break;
		}

	/* check if we cool */
	if (!major_num) {
		fprintf(stderr, "'%s' device NOT found in procfs.\n", drvr_nm);
		rc = -1;
		goto dr_install_exit;
	} else
		printf("\n'%s' device driver installed. Major number %d.\n",
		       drvr_nm, major_num);

	/* create service entry point */
	asprintf(&node_nm, "/dev/%s.0", drvr_nm);
	unlink(node_nm); /* if already exist delete it */
	devn = makedev(major_num, 0);
	if (mknod(node_nm, S_IFCHR | 0666, devn)) {
		mperr("Can't create %s service node", node_nm);
		rc = -1;
		goto dr_install_exit;
	}

	/* driverID (major num) will be used by the cdv_install() */
	rc = major_num;

 dr_install_exit:
	if (hotplug) {
		/* hotplug was disabled -- re-enable it back */
		set_kernel_hotplug(hotplug);
		free(hotplug);
	}
	if (drvr_nm)  free(drvr_nm);
	if (options)  free(options);
	if (drvrfile) free(drvrfile);
	if (node_nm)  free(node_nm);
	return rc;
}
Esempio n. 17
0
int main(int argc, char *argv[])
{
	unsigned int i;
	long int ret;
	unsigned long len;
	void *file;
	char *filename, *options = strdup("");
	char *p, *progname = argv[0];

	if (!options) {
		fprintf(stderr,
			"insmod: can't allocate memory: %s\n", strerror(errno));
		exit(1);
	}

	p = my_basename(argv[0]);

	if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
		puts(PACKAGE " version " VERSION);
		exit(0);
	}

	/*
	 * kdi-addition
	 */
	if (argv[1] && streq(argv[1], "-K")) {
		flag_kdi = 1;
		flag_use_kdi = 1;
		argv++;
		argc--;
		kdi_name = NOFAIL(strdup(argv[1]));
		argv++;
		argc--;
	}

	if (argv[1] && streq(argv[1], "-F")) {
		flag_kdi = 1;
		flag_inject_faults = 1;
		argv++;
		argc--;
	}

	if (argv[1] && streq(argv[1], "-T")) {
		flag_kdi = 1;
		flag_use_kdi_shadow = 1;
		argv++;
		argc--;
		shadow_type = NOFAIL(strdup(argv[1]));
		argv++;
		argc--;
	}

	if (argv[1] && streq(argv[1], "-N")) {
		flag_kdi = 1;
		flag_use_kdi = 1;
		flag_clobber_kdi = 1;
		argv++;
		argc--;
		clobber_module_name = NOFAIL(strdup(argv[1]));
		argv++;
		argc--;
	}
	/*
	 * kdi-end
	 */

	/* Ignore old options, for backwards compat. */
	while (argv[1] && (streq(argv[1], "-p")
			   || streq(argv[1], "-s")
			   || streq(argv[1], "-f"))) {
		argv++;
		argc--;
	}

	filename = argv[1];
	if (!filename)
		print_usage(progname);

	/* Rest is options */
	for (i = 2; i < argc; i++) {
		options = realloc(options,
				  strlen(options) + 1 + strlen(argv[i]) + 1);
		if (!options) {
			fprintf(stderr,
				"insmod: can't allocate memory: %s\n",
				strerror(errno));
			exit(1);
		}
		strcat(options, argv[i]);
		strcat(options, " ");
	}

	file = grab_file(filename, &len);
	if (!file) {
		fprintf(stderr, "insmod: can't read '%s': %s\n",
			filename, strerror(errno));
		exit(1);
	}

	/*
	 * kdi-addition
	 */
	if (flag_kdi) {
		if (init_kdi() != 0)
			exit(1);
		struct kdi_uargs *kdi_opts = malloc(sizeof(struct kdi_uargs));
		kdi_opts->use_kdi = flag_use_kdi;
		kdi_opts->use_kdi_shadow = flag_use_kdi_shadow;
		kdi_opts->inject_faults = flag_inject_faults;
		kdi_opts->clobber_kdi = flag_clobber_kdi;
		if (kdi_name)
			strncpy(kdi_opts->kdi_name, kdi_name, 100);
		if (clobber_module_name)
			strncpy(kdi_opts->clobber_module_name, clobber_module_name, 100);
		if (shadow_type)
			strncpy(kdi_opts->shadow_type, shadow_type, 100);

		ret = syscall(SYS_kdi_init_module, file, len, options, kdi_opts);
	} else
		ret = init_module(file, len, options);
	/*
	 * kdi-end
	 */
	if (ret != 0) {
		fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
			filename, ret, moderror(errno));
	}
	free(file);

	/*
	 * kdi-addition
	 */
	if (ret != 0 && flag_created_kdi == 1)
		syscall(SYS_kdi_delete_kdi, kdi_name);
	/*
	 * kdi-end
	 */
	if (ret != 0)
		exit(1);
	exit(0);
}
Esempio n. 18
0
static struct elf_file *grab_module(const char *name,
                                    const char *kernel,
                                    const char *basedir)
{
    char *data;
    unsigned long size;
    struct utsname buf;
    char *depname, *p, *moddir;
    struct elf_file *module;

    if (strchr(name, '.') || strchr(name, '/')) {
        module = grab_elf_file(name);
        if (!module)
            error("modinfo: could not open %s: %s\n",
                  name, strerror(errno));
        return module;
    }

    if (!kernel) {
        uname(&buf);
        kernel = buf.release;
    }
    if (strlen(basedir))
        nofail_asprintf(&moddir, "%s/%s/%s", basedir, MODULE_DIR, kernel);
    else
        nofail_asprintf(&moddir, "%s/%s", MODULE_DIR, kernel);

    /* Search for it in modules.dep. */
    nofail_asprintf(&depname, "%s/%s", moddir, "modules.dep");
    data = grab_file(depname, &size);
    if (!data) {
        error("modinfo: could not open %s\n", depname);
        free(depname);
        return NULL;
    }
    free(depname);

    for (p = data; p < data + size; p = next_line(p, data + size)) {
        if (name_matches(p, data + size, name)) {
            int namelen = strcspn(p, ":");
            const char *dir;
            char *filename;

            if ('/' == p[0])
                dir = basedir; /* old style deps - abs. path */
            else
                dir = moddir; /* new style - relative path */

            if (strlen(dir)) {
                nofail_asprintf(&filename, "%s/%s", dir, p);
                filename[namelen + strlen(dir) + 1] = '\0';
            } else {
                filename = strndup(p, namelen);
            }
            release_file(data, size);
            module = grab_elf_file(filename);
            if (!module)
                error("modinfo: could not open %s: %s\n",
                      filename, strerror(errno));
            free(filename);
            return module;
        }
    }
    release_file(data, size);
    error("modinfo: could not find module %s\n", name);
    return NULL;
}
Esempio n. 19
0
int main(int argc, char *argv[])
{
	size_t i, j, num;
	struct timeabs start, stop;
	char **w;
	ENTRY *words, *misswords;

	w = tal_strsplit(NULL, grab_file(NULL,
					 argv[1] ? argv[1] : "/usr/share/dict/words"), "\n", STR_NO_EMPTY);
	num = tal_count(w) - 1;
	printf("%zu words\n", num);

	hcreate(num+num/3);

	words = tal_arr(w, ENTRY, num);
	for (i = 0; i < num; i++) {
		words[i].key = w[i];
		words[i].data = words[i].key;
	}

	/* Append and prepend last char for miss testing. */
	misswords = tal_arr(w, ENTRY, num);
	for (i = 0; i < num; i++) {
		char lastc;
		if (strlen(w[i]))
			lastc = w[i][strlen(w[i])-1];
		else
			lastc = 'z';
		misswords[i].key = tal_fmt(misswords, "%c%s%c%c",
					   lastc, w[i], lastc, lastc);
	}

	printf("#01: Initial insert: ");
	fflush(stdout);
	start = time_now();
	for (i = 0; i < num; i++)
		hsearch(words[i], ENTER);
	stop = time_now();
	printf(" %zu ns\n", normalize(&start, &stop, num));

	printf("#02: Initial lookup (match): ");
	fflush(stdout);
	start = time_now();
	for (i = 0; i < num; i++)
		if (hsearch(words[i], FIND)->data != words[i].data)
			abort();
	stop = time_now();
	printf(" %zu ns\n", normalize(&start, &stop, num));

	printf("#03: Initial lookup (miss): ");
	fflush(stdout);
	start = time_now();
	for (i = 0; i < num; i++) {
		if (hsearch(misswords[i], FIND))
			abort();
	}
	stop = time_now();
	printf(" %zu ns\n", normalize(&start, &stop, num));

	/* Lookups in order are very cache-friendly for judy; try random */
	printf("#04: Initial lookup (random): ");
	fflush(stdout);
	start = time_now();
	for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
		if (hsearch(words[i], FIND)->data != words[i].data)
			abort();
	stop = time_now();
	printf(" %zu ns\n", normalize(&start, &stop, num));

	return 0;
}