Esempio n. 1
0
int main(int argc, char *argv[])
{
    Hashmap *packages;

    if (!(packages = readPackages(PACKAGES)))
        return -1;

    //hashmapForEach(packages, dumpPackage, NULL);
    printf("Found packages: %u\n", (int) hashmapSize(packages));

    initUsers();

    if (argc == 2)
    {
        APK *package = (APK *) hashmapGet(packages, argv[1]);
        if (package)
            checkPackage(NULL, package, NULL);
        else
            printf("Package %s not found\n", argv[1]);
    }
    else
        hashmapForEach(packages, checkPackage, NULL);

    hashmapForEach(packages, freePackage, NULL);
    hashmapFree(packages);
    return 0;
}
Esempio n. 2
0
/**
 * Called when the peer dies.
 */
static void peerProxyKill(PeerProxy* peerProxy, bool errnoIsSet) {
    if (errnoIsSet) {
        ALOGI("Peer %d died. errno: %s", peerProxy->credentials.pid, 
                strerror(errno));
    } else {
        ALOGI("Peer %d died.", peerProxy->credentials.pid);
    }
    
    // If we lost the master, we're up a creek. We can't let this happen.
    if (peerProxy->master) {    
        LOG_ALWAYS_FATAL("Lost connection to master.");
    }

    Peer* localPeer = peerProxy->peer;
    pid_t pid = peerProxy->credentials.pid;
    
    peerLock(localPeer);
    
    // Remember for awhile that the peer died.
    localPeer->deadPeers[localPeer->deadPeerCursor] 
        = peerProxy->credentials.pid;
    localPeer->deadPeerCursor++;
    if (localPeer->deadPeerCursor == PEER_HISTORY) {
        localPeer->deadPeerCursor = 0;
    }
  
    // Remove from peer map.
    hashmapRemove(localPeer->peerProxies, &pid);
    
    // External threads can no longer get to this peer proxy, so we don't 
    // need the lock anymore.
    peerUnlock(localPeer);
    
    // Remove the fd from the selector.
    if (peerProxy->fd != NULL) {
        peerProxy->fd->remove = true;
    }

    // Clear outgoing packet queue.
    while (peerProxyNextPacket(peerProxy)) {}

    bufferFree(peerProxy->inputBuffer);

    // This only applies to the master.
    if (peerProxy->connections != NULL) {
        // We can't leave these other maps pointing to freed memory.
        hashmapForEach(peerProxy->connections, &peerProxyRemoveConnection, 
                peerProxy);
        hashmapFree(peerProxy->connections);
    }

    // Invoke death listener.
    localPeer->onDeath(pid);

    // Free the peer proxy itself.
    free(peerProxy);
}
/*
 * Frees ctx and all entries within it
 */
static void freeCtx(struct ctx *ctx) {
    if (ctx) {
        Node *n = list_getHead(&ctx->list);
        while (list_isNode(n)) {
            Node *next = list_getNext(n);
            list_remove(n);
            // This will free n and the ScheduleIndex instance
            node_free(n);
            n = next;
        }

        if (ctx->tiploc)
            hashmapFree(ctx->tiploc);

        if (ctx->activity)
            hashmapFree(ctx->activity);

        free(ctx);
    }
}
Esempio n. 4
0
static void cleanup(int sig)
{
	killed = 1;

	printf( "Restoring arp table ...\n" );
	int i, j;
	struct ether_addr gateway_mac;

	if( netmap == NULL )
	{
		if( arp_lookup( gateway_ip, &gateway_mac, iface ) == 0 )
		{
			for( i = 0; i < 3; i++ )
			{
				/* XXX - on BSD, requires ETHERSPOOF kernel. */
				arp_send( lnet, ARPOP_REPLY, (unsigned char *)&gateway_mac, gateway_ip, (unsigned char *)&target_mac, target_ip );
				sleep(1);
			}
		}
	}
	else
	{
		if( arp_lookup( gateway_ip, &gateway_mac, iface ) == 0 )
		{
			for( i = 0; i < 3; i++ )
			{
				for( j = 0; j < netmap->bucketCount; j++ )
				{
					Entry* hentry = netmap->buckets[j];
					while( hentry != NULL )
					{
						Entry 					*next  = hentry->next;
						network_entry_t *entry = ( network_entry_t * )hentry->value;

						arp_send( lnet, ARPOP_REPLY, (unsigned char *)&gateway_mac, gateway_ip, entry->mac, entry->address );

						hentry = next;
					}
				}

				sleep(1);
			}
		}

		hashmapFree( netmap );
	}

	exit(0);
}
Esempio n. 5
0
void endGame() {
    hashmapFree(currentGame.gameMap);
}
Esempio n. 6
0
void fileHelperLoad(FileHelper fileHelper) {
    FileHelperInternal *m = (FileHelperInternal *) fileHelper;
    fileHelperCheckCreate(m->filePath);
    int sizeOfInt = sizeof(int);
    struct direct **schemasIds = {0};
    struct direct *schenaId;
    int size = scandir(m->filePath, &schemasIds, 0, alphasort);
    // loop all schemas
    int i;
    for (i = 0; i < size; i++) {
        schenaId = (struct direct *) schemasIds[i];
        char *schemaIdString = schenaId->d_name;
        int schemaId = atoi(schemaIdString);
        if (schemaId != 0) {
            printf("schema %s\n", schenaId->d_name);
            struct direct **pagesIds = {0};
            struct direct *pageStruct;
            char endOdString = '\0';
            int pageDirLength = strlen(m->filePath);
            int schemaIdStringLength = strlen(schemaIdString);
            char slesh = '/';
            char *pageDir = (char *) memoryAlloc(pageDirLength + 1 + schemaIdStringLength + 1);
            memcpy(pageDir, m->filePath, pageDirLength);
            memcpy(pageDir + pageDirLength, &slesh, 1);
            memcpy(pageDir + pageDirLength + 1, schemaIdString, schemaIdStringLength);
            memcpy(pageDir + pageDirLength + 1 + schemaIdStringLength, &endOdString, 1);


//			cleanBuffer(pageDir);
//			strcat(pageDir,m->filePath);
//			strcat(pageDir,"/");
//			strcat(pageDir,schemaIdString);
            HashMap childrenRelations = hashmapCreate(SMALL_CONTAINER_SIZE);
            Registry registry = registryCreate();
            hashmapPut(m->registryMap, schemaId, registry);
            PageManager pageManager = pageManagerCreate(schemaId, pageDir);
            hashmapPut(m->managers, schemaId, pageManager);
            int size = scandir(pageDir, &pagesIds, 0, alphasort);
            int h;
            //loop pages
            for (h = 0; h < size; h++) {
                //scandir (pageDir, &pagesIds, 0, alphasort);
                pageStruct = (struct direct *) pagesIds[h];
                char *pageString = pageStruct->d_name;
                int pageId = atoi(pageString);
                if (pageId != 0) {
                    printf("page %s\n", pageStruct->d_name);
                    // create file path.

                    //int i=0;
                    int pageDirLength = strlen(pageDir);
                    int pageStringLength = strlen(pageString);
                    char slesh = '/';
                    char *filePath = (char *) memoryAlloc(pageDirLength + 1 + pageStringLength + 1);
                    memcpy(filePath, pageDir, pageDirLength);
                    memcpy(filePath + pageDirLength, &slesh, 1);
                    memcpy(filePath + pageDirLength + 1, pageString, pageStringLength);
                    memcpy(filePath + pageDirLength + 1 + pageStringLength, &endOdString, 1);
                    FILE *fp;
                    long len;
                    // Create pointer to file.
                    fp = fopen(filePath, "rb");
                    if (fp == NULL) {
                        printf("couldn't open the properties file");
                    }
                    fseek(fp, 0, SEEK_END);                // Go to end
                    len = ftell(fp);                        // Get position at end (length)
                    // Read data to buffer
                    char *buffer = memoryAlloc(sizeof(char) * len); // Create buffer
                    char *pointerToFreeBuffer = buffer;
                    fseek(fp, 0, SEEK_SET);                // Go to the beginning.
                    int result = fread(buffer, len, 1, fp);                // Read into buffer
                    if (result < 0) {
                        printf("Fail to read file : %s", buffer);
                    }
                    fclose(fp);
                    int order = 0;
                    long index = 0;
                    long step = 0;
                    while (index < len) {
                        char *start = buffer;
                        int id;
                        int schemaId;
                        int numberOfAttributes;
                        int childrenSize;
                        int attributeSize;
                        memcpy(&schemaId, buffer + SIZE_OF_INT * 0, SIZE_OF_INT);
                        memcpy(&id, buffer + SIZE_OF_INT * 1, SIZE_OF_INT);
                        memcpy(&numberOfAttributes, buffer + SIZE_OF_INT * 2, SIZE_OF_INT);
                        buffer += SIZE_OF_INT * 3;
                        Node node;
                        nodeCreate(registry, id, schemaId, FALSE, &node);
                        int h;
                        for (h = 0; h < numberOfAttributes; h++) {
                            memcpy(&attributeSize, buffer, SIZE_OF_INT);
                            Attribute attribute = attributeDeserializeLocal(buffer);
                            buffer += attributeSize;
                            stringHashmapPut(node->attributes, attribute->name, attribute->nameLength, attribute);
                        }
                        memcpy(&childrenSize, buffer, SIZE_OF_INT);
                        Array children = arrayCreate(childrenSize);
                        hashmapPut(childrenRelations, id, children);
                        int i;
                        buffer += SIZE_OF_INT;
                        int childrenIdBuffer;
                        for (i = 0; i < childrenSize; i++) {
                            memcpy(&childrenIdBuffer, buffer + SIZE_OF_INT, SIZE_OF_INT);
                            void *bufferElement = memoryAlloc(childrenIdBuffer + SIZE_OF_INT * 2);
                            memcpy(bufferElement, buffer, SIZE_OF_INT * 2 + childrenIdBuffer);
                            arrayInsertObject(children, bufferElement);
                            buffer += SIZE_OF_INT * 2 + childrenIdBuffer;
                        }
                        step = buffer - start;
                        index += step;
                        pageManagerRegister(pageManager, pageId, id, index - step, index, order, filePath);
                        order++;
                    }
                    // Free the file buffer
                    memoryFree(pointerToFreeBuffer);
                }
            }
            if (size > 0) {
                while (size) {
                    size = size - 1;
                    free(pagesIds[size]);
                }
                free(pagesIds);
            }
            hashmapIterate(childrenRelations, &updateChildren, registry);
            hashmapFree(childrenRelations);
        }
    }
    if (size > 0) {
        while (size) {
            size = size - 1;
            free(schemasIds[size]);
        }
        free(schemasIds);
    }

}
Esempio n. 7
0
void s2_touch_event_free(struct s2_touch_event * self)
{
        hashmapFree(self->touches);
}
Esempio n. 8
0
void sprite_frame_cache_free(struct sprite_frame_cache* self) {
    hashmapFree(self->cache);
    s_free(self);
}
Esempio n. 9
0
void texture_cache_free(struct texture_cache* self)
{
    hashmapFree(self->cache);
    s_free(self);
}
Esempio n. 10
0
void massdns_scan(lookup_context_t *context)
{
    memset(&stats, 0, sizeof(dns_stats_t));
    size_t line_buflen = 4096;
    char *line = safe_malloc(line_buflen);
    size_t line_len = 0;
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = 0;
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    int socketbuf = 1024 * 1024 * 100;
    if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &socketbuf, sizeof(socketbuf)) != 0)
    {
        perror("Failed to adjust socket send buffer size.");
    }
    if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socketbuf, sizeof(socketbuf)) != 0)
    {
        perror("Failed to adjust socket receive buffer size.");
    }
    bind(sock, (sockaddr_t *) &server_addr, sizeof(server_addr));

    fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);

    if (sock < 0)
    {
        perror("Failed to create socket");
        exit(1);
    }
    FILE *f;
    if(context->cmd_args.show_progress)
    {
        f = fopen(context->cmd_args.domains, "r");
        if (f == NULL)
        {
            perror("Failed to open domain file");
            exit(1);
        }
        while(!feof(f))
        {
            if (0 <= getline(&line, &line_buflen, f))
            {
                trim_end(line);
                strtolower(line);
                if (strcmp(line, "") != 0)
                {
                    context->total_domains++;
                }
            }
        }
        fclose(f);
    }
    f = fopen(context->cmd_args.domains, "r");
    if (f == NULL)
    {
        perror("Failed to open domain file");
        exit(1);
    }
    if (geteuid() == 0)
    {
        fprintf(stderr, "You have started the program with root privileges.\n");
        struct passwd *nobody = getpwnam(UNPRIVILEGED_USER);
        if (!context->cmd_args.root)
        {
            if (nobody && setuid(nobody->pw_uid) == 0)
            {
                fprintf(stderr, "Privileges have been dropped to \"%s\" for security reasons.\n\n", UNPRIVILEGED_USER);
            }
            else if (!context->cmd_args.root)
            {
                fprintf(stderr, "Privileges could not be dropped to \"%s\".\n"
                        "For security reasons, this program will only run as root user when supplied with --root"
                        "which is not recommended.\n"
                        "It is better practice to run this program as a different user.\n", UNPRIVILEGED_USER);
                exit(1);
            }
        }
        else
        {
            fprintf(stderr, "[WARNING] Privileges were not dropped. This is not recommended.\n\n");
        }
    }
    FILE *randomness = fopen("/dev/urandom", "r");
    if (!randomness)
    {
        fprintf(stderr, "Failed to open /dev/urandom.\n");
        exit(1);
    }
    context->current_rate = 0;
    context->sock = sock;
    context->resolvers = massdns_resolvers_from_file(context->cmd_args.resolvers);
    context->map = hashmapCreate(context->cmd_args.hashmap_size, hash_string, cmp_lookup);
    context->initial = true;
    gettimeofday(&context->start_time, NULL);
    context->next_update = context->start_time;
    while (true)
    {
        while (hashmapSize(context->map) < context->cmd_args.hashmap_size && !feof(f))
        {
            if (0 <= getline(&line, &line_buflen, f))
            {
                trim_end(line);
                line_len = strlen(line);
                strtolower(line);
                if(strcmp(line, "") == 0)
                {
                    continue;
                }
                if (line_len > 0 && line[line_len - 1] == '.')
                {
                    // Remove trailing dot from FQDN
                    line[line_len] = 0;
                }
                lookup_t *lookup = hashmapGet(context->map, line);
                if (lookup == NULL)
                {
                    char *value = safe_malloc(line_len + 1);
                    strcpy(value, line);
                    lookup = safe_malloc(sizeof(*lookup));
                    lookup->domain = value;
                    lookup->tries = 0;
                    if (fread(&lookup->transaction, 1, sizeof(lookup->transaction), randomness) !=
                        sizeof(lookup->transaction))
                    {
                        fprintf(stderr, "Failed to get randomness for transaction id.\n");
                        exit(1);
                    }
                    gettimeofday(&lookup->next_lookup, NULL);
                    hashmapPut(context->map, value, lookup);
                }

            }
        }
        if(!context->cooldown && hashmapSize(context->map) < context->cmd_args.hashmap_size)
        {
            context->cooldown = true;
            gettimeofday(&context->cooldown_time, NULL);
        }
        while (massdns_receive_packet(sock, massdns_handle_packet, context));
        if (feof(f) && hashmapSize(context->map) == 0)
        {
            break;
        }
        hashmapForEach(context->map, handle_domain, context);
    }
    for (size_t i = 0; i < context->resolvers.len; i++)
    {
        free(((sockaddr_in_t **) context->resolvers.data)[i]);
        ((sockaddr_in_t **) context->resolvers.data)[i] = NULL;
    }
    free(line);
    free(context->resolvers.data);
    context->resolvers.data = NULL;
    print_stats(context);
    hashmapFree(context->map);
    context->map = NULL;
    fclose(f);
    fclose(randomness);
}
Esempio n. 11
0
static int load_config(const char *config)
{
	struct config_parser *cp;
	char *value;
	int i;

	if (!config || strlen(config) == 0)
		cp = config_parser_init(TBOOT_CONFIG);
	else
		cp = config_parser_init(config);

	if (!cp)
		goto err;

	/* init config with default values */
	tboot_config = hashmapCreate(array_size(tc_keys), strhash, strcompare);
	if (!tboot_config)
		goto err;

	for (i = 0; tc_keys[i]; i++)
		hashmapPut(tboot_config, (void *)tc_keys[i], (void *)tc_values[i]);

	/* parse config file */
	value = config_parser_get(cp, UI_CONF_KEY);
	if (value)
		tboot_config_set(UI_CONF_KEY, value);

	value = config_parser_get(cp, DISK_CONFIG_KEY);
	if (value)
		tboot_config_set(DISK_CONFIG_KEY, value);

	value = config_parser_get(cp, PUSH_DIR_KEY);
	if (value)
		tboot_config_set(PUSH_DIR_KEY, value);

	value = config_parser_get(cp, ROOTFS_KEY);
	if (value)
		tboot_config_set(ROOTFS_KEY, value);

	value = config_parser_get(cp, ENABLE_NFS_KEY);
	if (value)
		tboot_config_set(ENABLE_NFS_KEY, value);

	value = config_parser_get(cp, NFSURL_KEY);
	if (value)
		tboot_config_set(NFSURL_KEY, value);

	value = config_parser_get(cp, DISK_FORCE_KEY);
	if (value)
		tboot_config_set(DISK_FORCE_KEY, value);

	value = config_parser_get(cp, BAT_THRESHOLD_KEY);
	if (value)
		tboot_config_set(BAT_THRESHOLD_KEY, value);

	value = config_parser_get(cp, LCD_DIM_TIMEOUT_KEY);
	if (value)
		tboot_config_set(LCD_DIM_TIMEOUT_KEY, value);

	value = config_parser_get(cp, LCD_OFF_TIMEOUT_KEY);
	if (value)
		tboot_config_set(LCD_OFF_TIMEOUT_KEY, value);

	value = config_parser_get(cp, LCD_DIM_BRIGHTNESS_KEY);
	if (value)
		tboot_config_set(LCD_DIM_BRIGHTNESS_KEY, value);

	config_parser_free(cp);
	tboot_config_dump();
	return 0;

err:
	if (cp)
		config_parser_free(cp);
	if (tboot_config)
		hashmapFree(tboot_config);
	return -1;
}
Esempio n. 12
0
int main(int argc, char **argv)
{
	pthread_t t_auto, t_input, t_uevent, t_sighandler;
	//Volume *vol;
	int ret;
	char *disk_conf;
	void (*ui_update)(void *) = NULL;
	sigset_t set;

	/* block signals for all theads */
	sigemptyset(&set);
	sigaddset(&set, SIGPIPE);
	sigaddset(&set, SIGALRM);
	if (pthread_sigmask(SIG_BLOCK, &set, NULL))
		pr_critial("block signal failed.\n");

	/* create a dedicate thread to handle signals */
	if (pthread_create(&t_sighandler, NULL, sig_handler, &set))
		pr_critial("create signal handler thread failed.\n");

	if (argc > 1)
		ret = load_config(argv[1]);
	else
		ret = load_config(NULL);
	/* currently, missing config file is not critical */
	if (ret)
		pr_warning("can't load tboot configuration.\n");

	if (ret = tboot_ui_init(tboot_config_get(UI_CONF_KEY)))
		pr_error("UI crashed!\n");

	tboot_config_set(UI_CONF_KEY, tboot_ui_getconfpath());

	ev_init(input_callback, NULL);

	display_sysinfo(ret);

	if (!ret) {
		ui_update = ui_callback;
	}

	pr_info(" -- preos v%s for %s --\n", preos_version, DEVICE_NAME);
	import_kernel_cmdline(parse_cmdline_option);

	disk_conf = tboot_config_get(DISK_CONFIG_KEY);
	if (!disk_conf) {
		pr_error("Invalid tboot config disk_conf.\n");
		die();
	}

	setup_disk_information(disk_conf);

	aboot_register_commands();

	register_tboot_plugins();

	if (pthread_create(&t_input, NULL, input_listener_thread,
					NULL)) {
		pr_perror("pthread_create");
		die();
	}

	lcd_state_init();

	if (pthread_create(&t_uevent, NULL, uevent_thread, ui_update)) {
		pr_perror("pthread_create t_uevent");
		die();
	}

	/*
	vol = volume_for_path(SDCARD_VOLUME);
	if (vol)
		try_update_sw(vol, 1);
	*/

	if (g_use_autoboot && !g_update_location) {
		/*
		 * Create menu items for debug boot, this is a feature for developers
		 * only.
		 *
		 * Developers (kernel developers) put kernel, cmdline,
		 * rmadisk.img(optional) to platform boot directory  in kexec enabled
		 * preos.
		 *
		 * Generally, there are two ways to do that.
		 *
		 * 1. you can put these files into /boot after the system boot
		 * into rootfs.
		 * 2. you can put these files into platform.img.gz and create a new
		 * platform.img.gz and then flash it to target device.
		 *
		 * Previous, if the system can't boot into rootfs, the only way left
		 * is flash a new platform image. Apparently, it's a huge effort.
		 *
		 * To make more choice, two kernels are supported now, so make sure
		 * there is a works kernel in /boot , you can always boot into
		 * normal system and put a new kernel by any other way, such as scp,
		 * ftp and etc.
		 */

		/*
		 * make our menu acts more like grub which more users familiar with.
		 */
		tboot_ui_menu_item("Boot: kernel", start_default_kernel);
		tboot_ui_menu_item("Boot: kernel.bak", start_backup_kernel);
		tboot_ui_menu_item("Boot: kernel/rootfs on TF/micro-SD card",
				start_mmc_kernel);
		/*
		 * boot from NFS is quite useless now because the usb network bandwidth
		 * is too narrow to satisfy the boot up sequence. It may (80%) hang at
		 * system boot up, especially at X server start up
		 *
		 * So, by default, disable it.
		 */
		if (strcasecmp(tboot_config_get(ENABLE_NFS_KEY), "yes") == 0)
			tboot_ui_menu_item("Boot: kernel/rootfs on NFS",
					start_nfs_kernel);

		tboot_ui_menu_selected(3);      // set debug boot: kernel selected

		if (pthread_create(&t_auto, NULL, autoboot_thread, NULL)) {
			pr_perror("pthread_create");
			die();
		}
	}

	/*
	 * dealy to start tboot server
	 */
	do {
		sleep(1);
	} while (autoboot_enabled || power_pressed);

	pr_info("Listening for the fastboot protocol over USB.\n");
	fastboot_init(g_scratch_size * MEGABYTE);

	/* Shouldn't get here */
	tboot_ui_exit();

	if (tboot_config)
		hashmapFree(tboot_config);
//	tboot_cmds_free();

	exit(1);
}