コード例 #1
0
ファイル: util.c プロジェクト: BzzzBoy/gethooks
/* get_user_obj_name()
Get the name of a user object.

'object' is a handle to the user object you want the name of.

returns nonzero on success.
if success then '*name' has received a pointer to the user object's name. free() when done.
if fail then '*name' has received NULL.
if fail call GetLastError() for GetUserObjectInformationW() last error
*/
int get_user_obj_name( 
	WCHAR **const name,   // out deref
	HANDLE const object   // in
)
{
	DWORD bytes_needed = 0;
	
	FAIL_IF( !name );
	FAIL_IF( !object );
	
	
	SetLastError( 0 ); // error code may be evaluated on success
	GetUserObjectInformationW( object, UOI_NAME, NULL, 0, &bytes_needed );
	if( ( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
		|| ( bytes_needed < sizeof( WCHAR ) )
	)
	{
		*name = NULL;
		return FALSE;
	}
	
	*name = must_calloc( bytes_needed, 1 );
	
	SetLastError( 0 ); // error code may be evaluated on success
	if( !GetUserObjectInformationW( object, UOI_NAME, *name, bytes_needed, NULL ) )
	{
		free( *name );
		*name = NULL;
		return FALSE;
	}
	
	(*name)[ ( bytes_needed / sizeof( WCHAR ) ) - 1 ] = L'\0';
	
	return TRUE;
}
コード例 #2
0
ファイル: config.c プロジェクト: BzzzBoy/gethooks
/* create_config_store()
Create a configuration store and its descendants or die.

The configuration store holds the user-specified configuration derived from the command line.
*/
void create_config_store( 
	struct config **const out   // out deref
)
{
	struct config *config = NULL;
	
	FAIL_IF( !out );
	FAIL_IF( *out );
	
	
	/* allocate a config store */
	config = must_calloc( 1, sizeof( *config ) );
	
	/* allocate the list store for the linked list of desktops to filter */
	create_list_store( &config->desklist );
	
	/* allocate the list store for the linked list of hooks to filter */
	create_list_store( &config->hooklist );
	
	/* allocate the list store for the linked list of programs to filter */
	create_list_store( &config->proglist );
	
	/* allocate the list store for the linked list of test parameters */
	create_list_store( &config->testlist );
	
	
	*out = config;
	return;
}
コード例 #3
0
ファイル: list.c プロジェクト: BzzzBoy/gethooks
/* create_list_store()
Create a list store and its descendants or die.
*/
void create_list_store( 
	struct list **const out   // out deref
)
{
	FAIL_IF( !out );
	FAIL_IF( *out );
	
	
	*out = must_calloc( 1, sizeof( **out ) );
	
	return;
}
コード例 #4
0
void main_loop(void)
{
	int i, n, timeout = 250;
	struct connection *conn;

	ufds = must_calloc(thread_limit, sizeof(struct pollfd));
	for (i = 0; i < thread_limit; ++i)
		ufds[i].fd = -1;

	while (head || outstanding) {
		start_next_comic();

		n = poll(ufds, thread_limit, timeout);
		if (n < 0) {
			my_perror("poll");
			continue;
		}

		if (n == 0) {
			timeout_connections();
			if (!start_next_comic())
				/* Once we have all the comics
				 * started, increase the timeout
				 * period. */
				timeout = 1000;
			continue;
		}

		for (conn = comics; conn; conn = conn->next)
			if (!conn->poll)
				continue;
			else if (conn->poll->revents & POLLOUT) {
				if (!conn->connected)
					check_connect(conn);
				else {
					time(&conn->access);
					write_request(conn);
				}
			} else if (conn->poll->revents & POLLIN) {
				/* This check is needed for openssl */
				if (!conn->connected)
					check_connect(conn);
				else
					read_conn(conn);
			}
	}

	free(ufds);
}
コード例 #5
0
ファイル: prog.c プロジェクト: tigros/HookTools
/* create_prog_store()
Create a program store and its descendants or die.

The program store holds some basic program and system info.
*/
void create_prog_store( 
	struct prog **const out   // out deref
)
{
	struct prog *prog = NULL;
	
	FAIL_IF( !out );
	FAIL_IF( *out );
	
	
	/* allocate a prog store */
	prog = must_calloc( 1, sizeof( *prog ) );
	
	
	*out = prog;
	return;
}
コード例 #6
0
ファイル: global.c プロジェクト: BzzzBoy/gethooks
/* create_global_store()
Create the global store and its descendants or die.

Everything that must be available globally descends from the global store.
 */
void create_global_store( void )
{
	FAIL_IF( G );   // Fail if the global store already exists
	
	
	/* the global store (the store of global stores) */
	G = must_calloc( 1, sizeof( *G ) );
	
	/* program store (command line arguments, OS version, etc) */
	create_prog_store( &G->prog );
	
	/* configuration store (user-specified command line configuration) */
	create_config_store( &G->config );
	
	/* desktop store (linked list of desktops' heap and thread info) */
	create_desktop_store( &G->desktops );
	
	
	return;
}
コード例 #7
0
ファイル: util.c プロジェクト: BzzzBoy/gethooks
/* get_wstr_from_mbstr()
Get a wide character string from a multibyte character string.

'mbstr' is the multibyte character string.

returns nonzero on success.
if success then '*pwcstr' has received a pointer to the wide character string. free() when done.
if fail then '*pwcstr' has received NULL.
*/
int get_wstr_from_mbstr( 
	WCHAR **const pwcstr,   // out deref
	const char *const mbstr   // in
)
{
	size_t ecount = 0;
	
	FAIL_IF( !pwcstr );
	FAIL_IF( !mbstr );
	
	
	/* get the number of wide characters, excluding null, 
	needed to output the multibyte string as a wide character string
	*/
	ecount = mbstowcs( NULL, mbstr, 0 );
	if( ecount >= (size_t)INT_MAX ) /* error */
	{
		*pwcstr = NULL;
		return FALSE;
	}
	
	++ecount; /* add 1 for null terminator */
	
	*pwcstr = must_calloc( ecount, sizeof( WCHAR ) );
	
	ecount = mbstowcs( *pwcstr, mbstr, ecount );
	if( ecount >= (size_t)INT_MAX ) /* error */
	{
		free( *pwcstr );
		*pwcstr = NULL;
		return FALSE;
	}
	
	(*pwcstr)[ ecount ] = L'\0';
	
	return TRUE;
}
コード例 #8
0
ファイル: link-check.c プロジェクト: xkostadinov/get-comics
int main(int argc, char *argv[])
{
	char *env;
	int i, n, timeout = 250;
	struct connection *conn;

	method = "HEAD";

	while ((i = getopt(argc, argv, "hp:t:vT:")) != -1)
		switch ((char)i) {
		case 'h':
			usage(0);
		case 'p':
			set_proxy(optarg);
			break;
		case 't':
			thread_limit = strtol(optarg, NULL, 0);
			break;
		case 'v':
			verbose++;
			break;
		case 'T':
			read_timeout = strtol(optarg, NULL, 0);
			break;
		default:
			usage(1);
		}

	if (optind < argc)
		while (optind < argc)
			read_link_file(argv[optind++]);
	else
		read_urls(stdin);

	/* set_proxy will not use this if proxy already set */
	env = getenv("COMICS_PROXY");
	if (env)
		set_proxy(env);

	if (thread_limit == 0) {
		printf("You must allow at least one thread\n");
		exit(1);
	}

	if (thread_limit > n_comics)
		thread_limit = n_comics;

#ifdef _WIN32
	win32_init();
#else
	signal(SIGTERM, dump_outstanding);
	signal(SIGHUP, dump_outstanding);
#endif

	npoll = thread_limit + 1; /* add one for stdin */
	ufds = must_calloc(npoll, sizeof(struct pollfd));
	for (i = 0; i < npoll; ++i)
		ufds[i].fd = -1;

	while (head || outstanding) {

		start_next_comic();

		n = poll(ufds, npoll, timeout);
		if (n < 0) {
			my_perror("poll");
			continue;
		}

		if (n == 0) {
			timeout_connections();
			if (!start_next_comic())
				/* Once we have all the comics
				 * started, increase the timeout
				 * period. */
				timeout = 1000;
			continue;
		}

		for (conn = comics; conn; conn = conn->next)
			if (!conn->poll)
				continue;
			else if (conn->poll->revents & POLLOUT) {
				if (!conn->connected)
					check_connect(conn);
				else {
					time(&conn->access);
					write_request(conn);
				}
			} else if (conn->poll->revents & POLLIN) {
				/* This check is needed for openssl */
				if (!conn->connected)
					check_connect(conn);
				else
					read_conn(conn);
			}
	}

	out_results(comics, 0);

	return n_comics != gotit;
}
コード例 #9
0
ファイル: list.c プロジェクト: BzzzBoy/gethooks
/* add_list_item()
Append an item to a list store's linked list.

this function appends an item to a list if the id and/or name (depending on list type) is not 
already in the list. any comparison of 'name' is case insensitive.

'store' is the generic list store to append the item to.
whether 'id' and/or 'name' is used depends on the type of list. refer to list_item struct in list.h

hook:
'name' is optional. 'id' is required.
if 'name' then its corresponding id will be used for id instead of the passed in 'id'.
if not 'name' then the corresponding name (if any) of the passed in 'id' will be used for name.

prog:
'name' and 'id' are mutually exclusive. if 'name' use name and ignore 'id', else use 'id'.

desktop:
'name' required. 'id' is ignored.

test:
'name' required. 'id' required.

the item's name will point to a duplicate of the passed in 'name'.

returns on success a pointer to the list item that was added to the list. if there is already an 
existing item with the same id and/or name (depending on list type) a pointer to it is returned.
*/
struct list_item *add_list_item( 
	struct list *const store,   // in
	__int64 id,   // in, optional
	const WCHAR *name   // in, optional
)
{
	struct list_item *item = NULL;
	
	/* special case, if there's no 'name' for a hook point to a copy of the corresponding name 
	associated with the 'id'. this must be freed if a new item will not be created.
	*/
	WCHAR *hookname = NULL;
	
	FAIL_IF( !store );
	FAIL_IF( !store->type );
	
	
	/* if an item already in the list matches then return */
	if( ( store->type == LIST_INCLUDE_HOOK ) || ( store->type == LIST_EXCLUDE_HOOK ) )
	{
		if( name )
		{
			int hookid = 0;
			
			/* it is considered fatal if there's no id associated with a name. 
			it's possible this program's list of hooks is outdated, but in that case the user 
			would have to specify by id instead of name
			*/
			if( !get_HOOK_id_from_name( &hookid, name ) )
			{
				MSG_ERROR( "get_HOOK_id_from_name() failed." );
				printf( "Unknown id for hook name: %ls\n", name );
				printf( "\n" );
				item = NULL;
				goto existing_item;
			}
			
			id = hookid;
		}
		else
		{
			/* it is not considered fatal if there's no name associated with an id.
			maybe the user specified some undocumented hook ids in use without a name?
			hookname points to allocated memory and must be freed if a new item will not be created.
			*/
			if( !get_HOOK_name_from_id( &hookname, (int)id ) )
			{
				MSG_WARNING( "get_HOOK_name_from_id() failed." );
				printf( "Unknown name for hook id: %I64d\n", id );
				printf( "\n" );
			}
		}
		
		/* check if the hook id is already in the list.
		a hook id always has the same name (if any).
		if the hook id is already in the list a new item will not be created.
		*/
		for( item = store->head; item; item = item->next )
		{
			if( id == item->id ) /* hook id in list */
			{
				MSG_WARNING( "Hook id already in list." );
				print_list_item( item );
				printf( "\n" );
				goto existing_item;
			}
		}
		
		goto new_item;
	}
	else if( ( store->type == LIST_INCLUDE_PROG ) || ( store->type == LIST_EXCLUDE_PROG ) )
	{
		/* for the program list, name and id are mutually exclusive.
		if name then a program name has been passed in, 
		otherwise an id has been passed in. 
		if name or id is already in the list then there is no reason to append
		*/
		if( name )
		{
			/* check if the program name is already in the list */
			for( item = store->head; item; item = item->next )
			{
				if( item->name && !_wcsicmp( item->name, name ) ) /* name in list */
				{
					MSG_WARNING( "Program name already in list." );
					print_list_item( item );
					printf( "\n" );
					goto existing_item;
				}
			}
		}
		else
		{
			/* check if the PID/TID is already in the list */
			for( item = store->head; item; item = item->next )
			{
				/* a program list item's id is only valid if doesn't have a name */
				if( !item->name && id == item->id ) /* PID/TID in list */
				{
					MSG_WARNING( "PID/TID already in list." );
					print_list_item( item );
					printf( "\n" );
					goto existing_item;
				}
			}
		}
		
		goto new_item;
	}
	else if( store->type == LIST_INCLUDE_DESK )
	{
		FAIL_IF( !name );
		FAIL_IF( id ); // not expecting an id parameter for a desktop list
		
		/* for a desktop list only the name is used. check if it's already in the list */
		for( item = store->head; item; item = item->next )
		{
			if( item->name && !_wcsicmp( item->name, name ) ) /* name in list */
			{
				MSG_WARNING( "Desktop name already in list." );
				print_list_item( item );
				printf( "\n" );
				goto existing_item;
			}
		}
		
		goto new_item;
	}
	else if( store->type == LIST_INCLUDE_TEST )
	{
		FAIL_IF( !name );
		// id can be 0
		
		/* for a test list both name and id are used. check if it's already in the list */
		for( item = store->head; item; item = item->next )
		{
			if( ( item->id && id )
				&& ( item->name && !_wcsicmp( item->name, name ) )
			) // name/id combo already in list
			{
				MSG_WARNING( "Test name/id combo already in list." );
				print_list_item( item );
				printf( "\n" );
				goto existing_item;
			}
		}
		
		goto new_item;
	}
	else // handle generic here?
	{
		MSG_FATAL( "Unknown list type." );
		printf( "store->type: %d\n", store->type );
		exit( 1 );
	}
	
	
new_item:
	/* create a new item and add it to the list */
	
	item = must_calloc( 1, sizeof( *item ) );
	
	if( hookname ) /* special case. this function already has made a copy of the name to store */
		item->name = hookname;
	else if( name ) /* store a copy of the passed in name */
		item->name = must_wcsdup( name );
	else
		item->name = NULL;
	
	item->id = id;
	item->next = NULL;
	
	if( !store->head )
	{
		store->head = item;
		store->tail = item;
	}
	else
	{
		store->tail->next = item;
		store->tail = item;
	}
	
	return item;
	
	
existing_item:
	/* do not create a new item. free resources and return existing item, if any */
	
	free( hookname );
	return item;
}