Ejemplo n.º 1
0
int bootimg_unpack(const char *input, const char *output, bool dt_support)
{
	int fd;
	int ret;
	int count;
	char *filename;
	char pathname[1024];
	struct bootimg_header hdr;
	struct bootimg_image images[4];

	fd = open(input, O_RDONLY);
	if (fd < 0)
	{
		pr_error_info("open file `%s'", input);
		return fd;
	}

	ret = bootimg_read_header(fd, &hdr);
	if (ret < 0)
	{
		pr_red_info("bootimg_read_header");
		goto out_close_fd;
	}

	bootimg_header_dump(&hdr);

	filename = text_copy(pathname, output);
	ret = mkdir_hierarchy(pathname, 0777);
	if (ret < 0)
	{
		pr_red_info("mkdir_hierarchy");
		goto out_close_fd;
	}

	*filename++ = '/';

	count = 0;

	if (hdr.kernel_size > 0)
	{
		images[count].size = hdr.kernel_size;
		images[count++].name = FILE_KERNEL_NAME;
	}

	if (hdr.ramdisk_size > 0)
	{
		images[count].size = hdr.ramdisk_size;
		images[count++].name = FILE_RAMDISK_NAME;
	}

	if (hdr.second_size > 0)
	{
		images[count].size = hdr.second_size;
		images[count++].name = FILE_SECOND_NAME;
	}
	else
	{
		strcpy(filename, FILE_SECOND_NAME);
		unlink(filename);
	}

	if (hdr.dt_size > 0 && dt_support)
	{
		images[count].size = hdr.dt_size;
		images[count++].name = FILE_DT_NAME;
	}
	else
	{
		strcpy(filename, FILE_DT_NAME);
		unlink(filename);
	}

	images[count].size = 0;
	images[count++].name = FILE_REMAIN_NAME;

	if (count > 0)
	{
		struct bootimg_image *p, *p_end;

		for (p = images, p_end = p + count; p < p_end; p++)
		{
			strcpy(filename, p->name);
			println("%s -> %s", p->name, pathname);

			if (p->size > 0)
			{
				ret = cavan_file_seek_next_page(fd, hdr.page_size);
				if (ret < 0)
				{
					pr_red_info("cavan_file_seek_next_page");
					goto out_close_fd;
				}

				ret = file_ncopy2(fd, pathname, p->size, O_WRONLY | O_TRUNC | O_CREAT, 0777);
			}
			else
			{
				ret = file_copy2(fd, pathname, O_WRONLY | O_TRUNC | O_CREAT, 0777);
			}

			if (ret < 0)
			{
				pr_red_info("file_copy2");
				goto out_close_fd;
			}
		}
	}

	if (hdr.cmdline[0])
	{
		strcpy(filename, FILE_CMDLINE_TXT);
		println("cmdline -> %s", pathname);

		if (hdr.extra_cmdline[0])
		{
			char *p;
			char buff[sizeof(hdr.cmdline) + sizeof(hdr.extra_cmdline)];

			p = text_copy(buff, (char *) hdr.cmdline);
			p = text_copy(p, (char *) hdr.extra_cmdline);

			ret = file_writeto(pathname, buff, p - buff, 0, O_TRUNC);
			if (ret < 0)
			{
				pr_red_info("file_writeto");
				goto out_close_fd;
			}
		}
		else
		{
			ret = file_writeto(pathname, hdr.cmdline, strlen((char *) hdr.cmdline), 0, O_TRUNC);
			if (ret < 0)
			{
				pr_red_info("file_writeto");
				goto out_close_fd;
			}
		}
	}

	strcpy(filename, FILE_REPACK_SH);

	ret = bootimg_gen_repack_script(&hdr, pathname, dt_support);
	if (ret < 0)
	{
		pr_red_info("bootimg_gen_pack_script");
		goto out_close_fd;
	}

	strcpy(filename, FILE_CONFIG_TXT);

	ret = bootimg_write_config_file(&hdr, pathname);
	if (ret < 0)
	{
		pr_red_info("bootimg_write_config_file");
		goto out_close_fd;
	}

	ret = 0;
out_close_fd:
	close(fd);
	return ret;
}
Ejemplo n.º 2
0
void store_record(struct metadata* mdata, struct lnfstore_conf *conf)
{
	static uint8_t buffer[(uint16_t)-1];
	
	static lnf_rec_t *recp = NULL;
	static lnf_file_t *lfp = NULL; 
	stack_t *smap = conf->pst;

	if( conf->profiles && !mdata->channels ){
		//Record wont be stored, it does not belong to any channel and profiling is activated
		return;
	}

	if(recp == NULL) {
		lnf_rec_init(&recp);
	} else { 
		lnf_rec_clear(recp);
	}

    uint16_t offset, length;
    offset = 0;
    struct ipfix_template *templ = mdata->record.templ;
    uint8_t *data_record = (uint8_t*) mdata->record.record;

    /* get all fields */
    for (uint16_t count = 0, index = 0; count < templ->field_count; ++count, ++index) {
		
		struct ipfix_lnf_map *item, key;
        
        /* Get Enterprise number and ID */
        key.ie = templ->fields[index].ie.id;
        length = templ->fields[index].ie.length;
        key.en = 0;

        if (key.ie & 0x8000) {
			key.ie &= 0x7fff;
            key.en = templ->fields[++index].enterprise_number;
        }

		item = bsearch(&key, tr_table, MAX_TABLE , sizeof(struct ipfix_lnf_map), ipfix_lnf_map_compare);
		
		int problem = 1;
		if(item != NULL){
			problem = item->func(data_record, &offset, &length, buffer, item);	
			lnf_rec_fset(recp, item->lnf_id, buffer);
		}
		
		if(problem){
			length = real_length(data_record, &offset, length);
		}

        offset += length;
    } //end of element processing

	//!< Decide whether close close files and create new time window
    time_t now = time(NULL);
    if(difftime(now, conf->t_vars->window_start) > conf->time_window){

		mktime_window(now, conf);
		if( conf->profiles ){
			prec_t* item = NULL;
			//Close all old files
			for(unsigned x = 0; x < smap->top; x += al4B(sizeof(prec_t))){
				item = (prec_t*)smap->data + x; 
				if(item->lfp == NULL)
					continue;
				lnf_close(item->lfp);
				item->lfp = NULL;
			}
		} else {
			lnf_close(lfp);
			lfp = NULL;
		}
    }
    
    if( conf->profiles ){
		//On stack allocation of bit array
		int ba_size = smap->top/(8*sizeof(int))+1;
		int ba[ba_size];
		memset(&ba[0], 0, ba_size*sizeof(int));
		int status = 0;
		
		prec_t *item = NULL;

		for( int i = 0; mdata->channels[i] != 0; i++ ){
			
			void* rec_prof = channel_get_profile(mdata->channels[i]);

			item = bsearch(&rec_prof, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
			
			if( item == NULL ){
				//Profile is not in configuration
				prec_t entry;
				entry.address = rec_prof;
				entry.lfp = NULL;
				
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&entry.lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);

				stack_push(smap, &entry, sizeof(prec_t));
				//Add resilience
				qsort(smap->data, smap->top/al4B(sizeof(prec_t)), 
						al4B(sizeof(prec_t)), prec_compare);
				
				free(path);
			} else if( item->lfp == NULL ){
				//Profile file is closed
				const char* prpath = profile_get_path(rec_prof);
				char* path = mkpath_string(conf, prpath);
				mkdir_hierarchy(path);
				status = lnf_open(&item->lfp, path, LNF_WRITE | (conf->compress? LNF_COMP : 0), (char*)conf->ident);
				//Get prof name and open aprop file
				free(path);
			}
		}
		void* profile = NULL;
		int i = 0;
		for(profile = channel_get_profile(mdata->channels[i=0]);
				mdata->channels[i] != 0;
				profile = channel_get_profile(mdata->channels[i++]) ){

			item = bsearch(&profile, smap->data, smap->top/al4B(sizeof(prec_t)),
					al4B(sizeof(prec_t)), prec_compare);
				
			int index = (((base_t*)item) - smap->data)/al4B(sizeof(prec_t));
			if( !GETb(ba, index) ){ //And profile is not shadow
				status = lnf_write(item->lfp, recp);
				SETb(ba, index, 1);
			}
		}
	} else {
		if( lfp == NULL ){
			int status;
			char* path = mkpath_string(conf, NULL);
			status = mkdir_hierarchy(path);
			status |= lnf_open(&lfp, path, LNF_WRITE | (conf->compress ? LNF_COMP : 0), (char*)conf->ident);
			if(status != LNF_OK) {
				MSG_ERROR(msg_module, "Failed to open file! ...at path: %s \n", path);
			}
			free(path);
		}
		lnf_write(lfp, recp);
	}
	return;
}