Exemplo n.º 1
0
/* ONLY CALL WHEN MUTEX ACQUIRED */
void add_active(bzz_thread_t *thread, bzz_t *lock) {
	// Determine thread color and remove from correct waiting list
	if(is_gold(thread)) {
		int position = list_locate(lock->waiting_gold_threads, thread);
		list_delete_at(lock->waiting_gold_threads, position);
	} else {
		int position = list_locate(lock->waiting_black_threads, thread);
		list_delete_at(lock->waiting_black_threads, position);
	}
	lock->active_threads++;
}
Exemplo n.º 2
0
CK_RV create_slot(sc_reader_t *reader)
{
	struct sc_pkcs11_slot *slot;

	if (list_size(&virtual_slots) >= sc_pkcs11_conf.max_virtual_slots)
		return CKR_FUNCTION_FAILED;

	slot = (struct sc_pkcs11_slot *)calloc(1, sizeof(struct sc_pkcs11_slot));
	if (!slot)
		return CKR_HOST_MEMORY;

	list_append(&virtual_slots, slot);
	slot->login_user = -1;
	slot->id = (CK_SLOT_ID) list_locate(&virtual_slots, slot);
	sc_debug(context, SC_LOG_DEBUG_NORMAL, "Creating slot with id 0x%lx", slot->id);
	
	list_init(&slot->objects);
	list_attributes_seeker(&slot->objects, object_list_seeker);

	init_slot_info(&slot->slot_info);
	if (reader != NULL) {
		slot->reader = reader;
		strcpy_bp(slot->slot_info.slotDescription, reader->name, 64);
	}
	return CKR_OK;
}
Exemplo n.º 3
0
/*
 * ns_set_value_from_string() sets the passed in kvp in the passed in printer
 * structure.
 */
int
ns_set_value_from_string(const char *key, const char *string,
			ns_printer_t *printer)
{
	if (printer == NULL)
		return (-1);

	if (key == NULL)
		list_iterate((void **)printer->attributes,
				(VFUNC_T)ns_kvp_destroy);
	else {
		ns_kvp_t *kvp;

		if (((kvp = list_locate((void **)printer->attributes,
					(COMP_T)ns_kvp_match_key,
					(void *)key)) == NULL) &&
		    ((kvp = calloc(1, sizeof (*kvp))) != NULL)) {
			kvp->key = strdup(key);
			printer->attributes = (ns_kvp_t **)
				list_append((void **)printer->attributes, kvp);
		}
		if (string != NULL)
			kvp->value = strdup(string);
		else
			kvp->value = NULL;
	}

	return (0);
}
Exemplo n.º 4
0
static LONG MSGCheckHandleAssociation(SCARDHANDLE hCard,
	SCONTEXT * threadContext)
{
	int list_index = 0;

	if (0 == threadContext->hContext)
	{
		/* the handle is no more valid. After SCardReleaseContext() for
		 * example */
		Log1(PCSC_LOG_CRITICAL, "Invalidated handle");
		return -1;
	}

	(void)pthread_mutex_lock(&threadContext->cardsList_lock);
	list_index = list_locate(&threadContext->cardsList, &hCard);
	(void)pthread_mutex_unlock(&threadContext->cardsList_lock);
	if (list_index >= 0)
		return 0;

	/* Must be a rogue client, debug log and sleep a couple of seconds */
	Log1(PCSC_LOG_ERROR, "Client failed to authenticate");
	(void)SYS_Sleep(2);

	return -1;
}
Exemplo n.º 5
0
    bag::value_type bag::grab( ) const {
		size_type i;
		const node *cursor;
		assert(size( ) > 0);
		i = (rand( ) % size( )) + 1;
		cursor = list_locate(head_ptr, i);
		return cursor->data( );
    }
Exemplo n.º 6
0
/*
 * FUNCTION:
 *	ns_printer_match_name(const ns_printer_t *printer, const char *name)
 * INPUT(S):
 *	const ns_printer_t *printer
 *		- key/value pair to check
 *	const char *key
 *		- key for matching
 * OUTPUT(S):
 *	int (return value)
 *		- 0 if matched
 * DESCRIPTION:
 */
int
ns_printer_match_name(ns_printer_t *printer, const char *name)
{
	if ((printer == NULL) || (printer->name == NULL) || (name == NULL))
		return (-1);

	if ((strcmp(printer->name, name) == 0) ||
	    (list_locate((void **)printer->aliases,
			(COMP_T)ns_strcmp, (void *)name) != NULL))
		return (0);

	return (-1);
}
Exemplo n.º 7
0
void list_insert(List* self, int p_index, ADT p_src)
{
	RealList* l_reallist = (RealList*)self;

	if ( p_index<=0 || p_index>l_reallist->length )
	{
		list_append(self, p_src);
		return ;
	}

	if ( list_locate(self, p_index) ) list_insert_after(self, p_src);
	else list_append(self, p_src);
}
Exemplo n.º 8
0
Arquivo: slot.c Projeto: llogar/OpenSC
CK_RV create_slot(sc_reader_t *reader)
{
	/* find unused virtual hotplug slots */
	struct sc_pkcs11_slot *slot = reader_get_slot(NULL);

	/* create a new slot if no empty slot is available */
	if (!slot) {
		if (list_size(&virtual_slots) >= sc_pkcs11_conf.max_virtual_slots)
			return CKR_FUNCTION_FAILED;

		slot = (struct sc_pkcs11_slot *)calloc(1, sizeof(struct sc_pkcs11_slot));
		if (!slot)
			return CKR_HOST_MEMORY;

		list_append(&virtual_slots, slot);
		if (0 != list_init(&slot->objects)) {
			return CKR_HOST_MEMORY;
		}
		list_attributes_seeker(&slot->objects, object_list_seeker);

		if (0 != list_init(&slot->logins)) {
			return CKR_HOST_MEMORY;
		}
	} else {
		/* reuse the old list of logins/objects since they should be empty */
		list_t logins = slot->logins;
		list_t objects = slot->objects;

		memset(slot, 0, sizeof *slot);

		slot->logins = logins;
		slot->objects = objects;
	}

	slot->login_user = -1;
	slot->id = (CK_SLOT_ID) list_locate(&virtual_slots, slot);
	init_slot_info(&slot->slot_info, reader);
	sc_log(context, "Initializing slot with id 0x%lx", slot->id);

	if (reader != NULL) {
		slot->reader = reader;
		strcpy_bp(slot->slot_info.manufacturerID, reader->vendor, 32);
		strcpy_bp(slot->slot_info.slotDescription, reader->name, 64);
		slot->slot_info.hardwareVersion.major = reader->version_major;
		slot->slot_info.hardwareVersion.minor = reader->version_minor;
	}

	return CKR_OK;
}
Exemplo n.º 9
0
 void bag::operator -=(const bag & subtrahend)
 {
     node *copy_head_ptr;
     node *copy_tail_ptr;

     if(subtrahend.many_nodes > 0)
     {
         list_copy(subtrahend.head_ptr, copy_head_ptr, copy_tail_ptr);
         for(int i = 1; i <= list_length(copy_head_ptr); i++)
         {
             value_type val = list_locate(copy_head_ptr, i)->data();
             erase_one(val);
         }
     }
 }
Exemplo n.º 10
0
int list_contains(const list_t *l, const void *data) {
    return (list_locate(l, data) >= 0);
}
Exemplo n.º 11
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  Strip out any lines that are internal Falcon issues, append
 *  the remaining lines to the alarm context list.
 */
int alarm_filter_lines( alarm_context_t* alarm_list, buffer_t* buf,
                        time_t last_alarm_time )
{
    int result = 1;

    regex_t regex;
    regmatch_t match_list[MAX_MATCHES];
    regmatch_t* match = NULL;
    regmatch_t* code_match = NULL;
    regmatch_t* desc_match = NULL;
    regmatch_t* evt_match  = NULL;
    regmatch_t* time_match = NULL;
    buffer_t* line = NULL;
    alarm_line_t* line_element = NULL;
    char* content_string = NULL;
    char tmp_char = '\0';
    size_t code_si = 0;
    size_t code_ei = 0;
    size_t desc_si = 0;
    size_t desc_ei = 0;
    size_t evt_si  = 0;
    size_t evt_ei  = 0;
    size_t time_si = 0;
    size_t time_ei = 0;
    struct tm time_struct;
    uint16_t code = 0;

    // Set up the csv directory url
    if (buffer_size(buf) && alarm_list) 
    {
        // Construct the regular expression for filtering alarm messages
        if (regcomp(&regex, "AH([0-9]{3})[-]([0-9]{4})[-]([^ ]+)[ ]*[-]([0-9]{2}[/][0-9]{2}[/][0-9]{2} [0-9]{2}[:][0-9]{2}[:][0-9]{2})[^:]*? ([^: ]+)([:][^\n\r]+)", REG_EXTENDED | REG_NEWLINE))
        {
            goto unclean;
        }

        memset(&match_list, 0, sizeof(match_list));
        content_string = (char*)buf->content;
        // Only process lines we are interested in
        while (!regexec(&regex, content_string, (size_t)MAX_MATCHES, match_list, 0)) 
        {
            match = match_list;
            code_match = &match_list[2];
            evt_match  = &match_list[3];
            time_match = &match_list[4];
            desc_match = &match_list[5];
            if (match->rm_so && (match->rm_eo > match->rm_so)) 
            {
                if (gDebug) printf("Parsing Alarm\n");

                line_element = alarm_line_init();
                if (!line_element)
                    goto unclean;
                    
                line = buffer_init();
                if (!line)
                    goto unclean;

                buffer_write(line, (uint8_t*)(content_string + match->rm_so),
                                   (size_t)(match->rm_eo - match->rm_so));
                buffer_terminate(line);
                line_element->text = (char*)buffer_detach(line);
                line = buffer_destroy(line);
                line_element->hash = murmur_32(line_element->text, 
                                               strlen(line_element->text),
                                               HASH_SEED_32);
                if (gDebug) printf("  Text: %s\n", line_element->text);

                // Parse time
                time_si = time_match->rm_so - match->rm_so;
                time_ei = time_match->rm_eo - match->rm_so;
                tmp_char = line_element->text[time_ei];
                line_element->text[time_ei] = '\0'; // temporarily terminate at end of time
                if (gDebug) printf("  Timestamp: %s\n", line_element->text + time_si);
                memset(&time_struct, 0, sizeof(struct tm));
                strptime((const char*)(line_element->text + time_si),
                         "%m/%d/%y %H:%M:%S", &time_struct);
                line_element->timestamp = mktime(&time_struct);
                line_element->text[time_ei] = tmp_char; // restore character

                // Get the event code
                code_si = code_match->rm_so - match->rm_so;
                code_ei = code_match->rm_eo - match->rm_so;
                tmp_char = line_element->text[code_ei];
                line_element->text[code_ei] = '\0';
                if (gDebug) printf("  Code: %s\n", line_element->text + code_si);
                code = atoi(line_element->text + code_si);
                line_element->channel = code / 10;
                line_element->event = code % 10;
                line_element->text[code_ei] = tmp_char;

                // Determine if the alarm is a trigger or return
                evt_si = evt_match->rm_so - match->rm_so;
                evt_ei = evt_match->rm_eo - match->rm_so;
                tmp_char = line_element->text[evt_ei];
                line_element->text[evt_ei] = '\0';
                if (gDebug) printf("  Event: %s\n", line_element->text + evt_si);
                if (!strcmp("RTN", line_element->text + evt_si)) {
                    line_element->event |= 0x80;
                }
                line_element->text[evt_ei] = tmp_char;

                // Record the description code
                desc_si = desc_match->rm_so - match->rm_so;
                desc_ei = desc_match->rm_eo - match->rm_so;
                tmp_char = line_element->text[desc_ei];
                line_element->text[desc_ei] = '\0';
                if (gDebug) printf("  Description: %s\n", line_element->text + desc_si);
                strncpy(line_element->description, line_element->text + desc_si, 8);
                line_element->description[8] = '\0';
                line_element->text[desc_ei] = tmp_char;

                // If this is a duplicate message, throw it out
                if ((line_element->timestamp <= last_alarm_time) ||
                    (list_locate(alarm_list, line_element) > -1))
                {
                    free(line_element->text);
                    free(line_element);
                }
                // Otherwise, add it to the list
                else
                {
                    list_append(alarm_list, line_element);
                    if ( list_size(alarm_list) > MAX_ALARMS )
                    {
                        line_element = list_fetch(alarm_list);
                        line_element = alarm_line_destroy(line_element);
                    }
                }
            } 
            content_string += match->rm_eo;
            memset(&match_list, 0, sizeof(match_list));
        }
    }

    goto clean;
 unclean:
    result = 0;
    line_element = alarm_line_destroy(line_element);
    line = buffer_destroy(line);

 clean:
    regfree(&regex);

    return result;
} // alarm_filter_lines()
Exemplo n.º 12
0
/*
 * FUNCTION:
 *	ns_r_get_value(const char *key, const ns_printer_t *printer)
 * INPUT(S):
 *	const char *key
 *		- key for matching
 *	const ns_printer_t *printer
 *		- printer to glean this from
 * OUTPUT(S):
 *	char * (return value)
 *		- NULL, if not matched
 * DESCRIPTION:
 */
static void *
ns_r_get_value(const char *key, const ns_printer_t *printer, int level)
{
	ns_kvp_t *kvp, **attrs;

	if ((key == NULL) || (printer == NULL) ||
	    (printer->attributes == NULL))
		return (NULL);

	if (level++ == 16)
		return (NULL);

	/* find it right here */
	if ((kvp = list_locate((void **)printer->attributes,
			(COMP_T)ns_kvp_match_key, (void *)key)) != NULL) {
		void *value = string_to_value(key, kvp->value);

		/* fill in an empty printer for a bsdaddr */
		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
			ns_bsd_addr_t *addr = value;

			if (addr->printer == NULL)
				addr->printer = strdup(printer->name);
		}
		return (value);
	}

	/* find it in a child */
	for (attrs = printer->attributes; attrs != NULL && *attrs != NULL;
	    attrs++) {
		void *value = NULL;

		if ((strcmp((*attrs)->key, NS_KEY_ALL) == 0) ||
		    (strcmp((*attrs)->key, NS_KEY_GROUP) == 0)) {
			char **printers;

			for (printers = string_to_value((*attrs)->key,
						(*attrs)->value);
			    printers != NULL && *printers != NULL; printers++) {
				ns_printer_t *printer =
					ns_printer_get_name(*printers, NULL);

				if ((value = ns_r_get_value(key, printer,
							    level)) != NULL)
					return (value);
				ns_printer_destroy(printer);
			}
		} else if (strcmp((*attrs)->key, NS_KEY_LIST) == 0) {
			ns_printer_t **printers;

			for (printers = string_to_value((*attrs)->key,
						(*attrs)->value);
			    printers != NULL && *printers != NULL; printers++) {
				if ((value = ns_r_get_value(key, *printers,
							    level)) != NULL)
					return (value);
			}
		} else if (strcmp((*attrs)->key, NS_KEY_USE) == 0) {
			char *string = NULL;
			ns_printer_t *printer =
				ns_printer_get_name((*attrs)->value, NULL);
			if ((value = ns_r_get_value(key, printer,
					level)) != NULL)
				string = value_to_string(string, value);
			if (string != NULL)
				value = string_to_value(key, string);
			ns_printer_destroy(printer);
		}

		if (value != NULL)
			return (value);
	}

	return (NULL);
}
Exemplo n.º 13
0
void list_remove(List* self, int p_index)
{
	if ( list_locate(self, p_index) )
		list_remove_current(self);
}
Exemplo n.º 14
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Update the csv context with the latest contents from the
 *   Falcon. For each buffer that has at least on hour worth of
 *   data, compress and write the data to the diskloop.
 */
void csv_poll( csv_context_t* csv_buffer_list, buffer_t* url_str,
               st_info_t* st_info, time_t initial_time )
{
    list_t* file_list = NULL;
    buffer_t* buf = NULL;
    buffer_t* url = NULL;
    char* file_name = NULL;
    const char* path = "/data/minute";
    csv_buffer_t csv_tmp;
    csv_buffer_t* csv_buffer;
    int location = 0;
    uint64_t file_hash = 0LL;

    uint8_t buffer_found = 0;

    int tally = 0;

    // Build the CSV directory URL
    url = buffer_init();
    buffer_write(url, url_str->content, url_str->length);
    buffer_write(url, (uint8_t*)path, strlen(path));
    buffer_terminate(url);

    // Initialize the CSV file list
    file_list = (list_t*)malloc(sizeof(list_t));
    if (!file_list)
        goto clean;
    list_init(file_list);
    list_attributes_seeker( file_list,  _file_list_seeker );
    list_attributes_comparator( file_list,  _file_list_comparator );

    // Get the html page listing the available CSV files
    buf = buffer_init();
    get_page((char*)url->content, buf);

    // Generate a list of files from the page
    if (!csv_get_file_list(file_list, buf))
        goto clean;
    buffer_reset(buf);
    buffer_reset(url);

    // Step through each CSV file and update its csv_buffer
    // in the csv_buffer_list
    while (!list_empty(file_list)) 
    {
        tally++;
        file_name = (char*)list_fetch(file_list);
        memset(&csv_tmp, 0, sizeof(csv_tmp));
        csv_tmp.file_name = file_name;

        // If there is not a csv buffer for this csv file, create a
        // new buffer and add it to the list
        if ((location = list_locate(csv_buffer_list, &csv_tmp)) < 0)
        {
            csv_buffer = csv_buffer_init();
            csv_buffer->file_name = file_name;
            list_append(csv_buffer_list, csv_buffer);
            buffer_found = 0;
        // Otherwise re-use the old csv buffer
        } else { 
            csv_buffer = (csv_buffer_t*)list_get_at(csv_buffer_list, location);
            buffer_found = 1;
        }

     // Process the contents of this CSV file
        // Generate the URL for retrieving the file
        buffer_write(url, url_str->content, url_str->length);
        buffer_write(url, (uint8_t*)csv_buffer->file_name,
                     strlen(csv_buffer->file_name));
        buffer_terminate(url);
        if (gDebug) {
            printf("getting page %s\n", url->content);
        }
        // Download the file
        get_page((char*)url->content, buf);
        file_hash = murmur_64_b( buf->content, buf->length, HASH_SEED_64 );
        if (gDebug) {
            fprintf(stderr, "file '%s' [0x%016llx] uncompressed size is %lu bytes\n",
                   csv_buffer->file_name, (unsigned long long)file_hash,
                   (unsigned long)buf->length);
            if (strcmp("/data/minute/logm1.csv", csv_buffer->file_name) == 0)
              fprintf(stderr, "'%s'\n", buf->content);
        }
        // Populate a csv_buffer with the contents of the file
        csv_parse_file(csv_buffer, buf, initial_time);
        if (gDebug) {
            fprintf(stderr, "The CSV buffer contains %u rows\n", csv_buffer->list->numels);
        }
        // Empty our temporary buffers
        buffer_reset(buf);
        buffer_reset(url);
        if (buffer_found) {
            free(file_name);
        }
        file_name = NULL;
        csv_buffer = NULL;
    }

// Clean up all temporary resources
clean:
    buf = buffer_destroy(buf);
    url = buffer_destroy(url);
    if (file_list) 
    {
        while(!list_empty(file_list))
        {
            file_name = list_fetch(file_list);
            if (file_name)
            {
                free(file_name);
            }
        }
        list_destroy(file_list);
        free(file_list);
    }
} // csv_poll()