Exemplo n.º 1
0
void boot_unstripped(char *setup,char *kernel,IMAGE_DESCR *descr)
{
    struct exec exec;
    int fd,sectors;

    if (verbose) printf("Unstripped: setup %s, kernel %s\n",setup,kernel);
    (void) geo_open(&geo,setup,O_RDONLY);
    map_begin_section();
    map_add(&geo,0,SETUPSECS);
    geo_close(&geo);
    fd = geo_open(&geo,kernel,O_RDONLY);
    if (read(fd,(char *) &exec,sizeof(struct exec)) != sizeof(struct exec))
	die("read %s: %s",kernel,strerror(errno));
    if (exec.a_entry || exec.a_trsize || exec.a_drsize || !exec.a_syms)
	die("Not an unstripped kernel: %s",kernel);
    map_add(&geo,2,(exec.a_text+exec.a_data+SECTOR_SIZE-1)/SECTOR_SIZE);
    if ((exec.a_text+exec.a_data) & 15)
	fprintf(stderr,"Warning: Unsupported BSS - no initialization.\n");
    else {
	if (verbose > 2)
	    printf("BSS: 0x%X+%d\n",exec.a_text+exec.a_data+DEF_SYSSEG,
	      exec.a_bss);
	descr->bss_seg = ((exec.a_text+exec.a_data) >> 4)+DEF_SYSSEG;
	descr->bss_segs = exec.a_bss >> 16;
	descr->bss_words = ((exec.a_bss & 0xffff)+1) >> 1;
    }
    geo_close(&geo);
    sectors = map_end_section(&descr->start);
    if (verbose > 1)
	printf("Mapped %d sector%s.\n",sectors,sectors == 1 ? "" : "s");
}
Exemplo n.º 2
0
void _map_test()
{
    int a = 5, b = 12, c = 12345, d = 54321;
    int *aa, *bb, *cc, *dd, *ff;
    int repeat = 1;
    char *e = "Immanuel Kant",
          *ee;

    struct _map *map = map_new();
    assert(map != NULL);

    map_add(map, "rest not", &a);
    map_add(map, "life is sweeping by", &b);
    map_add(map, "go and do before you die", &c);
    map_add(map, "something mighty and sublime", &d);
    map_add(map, "leave behind to conquer time", e);

_repeat:
    aa = map_get(map, "rest not");
    if (repeat)
        assert(*aa == 5);
    else
        assert(aa == NULL);

    bb = map_get(map, "life is sweeping by");
    if (repeat)
        assert(*bb == 12);
    else
        assert(bb == NULL);
    cc = map_get(map, "go and do before you die");
    if (repeat)
        assert(*cc == 12345);
    else
        assert(cc == NULL);
    dd = map_get(map, "something mighty and sublime");
    if (repeat)
        assert(*dd == 54321);
    else
        assert(dd == NULL);
    ee = map_get(map, "leave behind to conquer time");
    if (repeat)
        assert(ee == e);
    else
        assert(ee == NULL);
    ff = map_get(map, "this is a nonexistent key");
    assert(ff == NULL);
    if (repeat) {
        repeat = 0;
        map_clear(map);
        goto _repeat;
    }
    fprintf(stdout, "%s: all tests passed\n", __func__);
    map_free(map);
}
Exemplo n.º 3
0
int main ()
{
	//these work fine - functions given by Allen
    Hashable *hashable1 = make_hashable_int (1);
    Hashable *hashable2 = make_hashable_string ("Allen");
    Hashable *hashable3 = make_hashable_int (2);

    // make_int_value also given
    Value *value1 = make_int_value (17);
	//failing here!
    Node *node1 = make_node(hashable1, value1, NULL);
	fprintf(stdout, "Print node:\n");
    print_node (node1);

    Value *value2 = make_string_value ("Downey");
    Node *list = prepend(hashable2, value2, node1);
	fprintf(stdout, "Print list:\n");
    print_list (list);

    // run some test lookups
    Value *value = list_lookup (list, hashable1);
	fprintf(stdout, "List lookup:\n");
    print_lookup(value);

	fprintf(stdout, "List lookup:\n");
    value = list_lookup (list, hashable2);
    print_lookup(value);

	fprintf(stdout, "List lookup:\n");
    value = list_lookup (list, hashable3);
    print_lookup(value);

    // make a map
    Map *map = make_map(10);
    map_add(map, hashable1, value1);
    map_add(map, hashable2, value2);

    printf ("Map\n");
    print_map(map);

    // run some test lookups
    value = map_lookup(map, hashable1);
    print_lookup(value);

    value = map_lookup(map, hashable2);
    print_lookup(value);

    value = map_lookup(map, hashable3);
    print_lookup(value);

    return 0;
}
Exemplo n.º 4
0
main()
{ 
    content_add("c");
    content_print(); 
    content_add("a");
    content_print(); 
    Content *f= content_add("b");
    map_add(&(f->bhead),2,4); 
    map_add(&(f->bhead),7,5); 
    map_add(&(f->bhead),3,2); 
    content_print(); 
    content_add("d");
    content_print(); 
} 
Exemplo n.º 5
0
void boot_device(char *spec,char *range,IMAGE_DESCR *descr)
{
    char *here;
    int start,secs;
    int sectors;

    if (verbose) printf("Boot device: %s, range %s\n",spec,range);
    (void) geo_open(&geo,spec,O_NOACCESS);
    map_begin_section();
    if (here = strchr(range,'-')) {
	*here++ = 0;
	start = to_number(range);
	if ((secs = to_number(here)-start+1) < 0) die("Invalid range");
    }
    else {
	if (here = strchr(range,'+')) {
	    *here++ = 0;
	    start = to_number(range);
	    secs = to_number(here);
	}
	else {
	    start = to_number(range);
	    secs = 1;
	}
    }
    map_add(&geo,start,secs);
    sectors = map_end_section(&descr->start);
    geo_close(&geo);
    if (verbose > 1)
	printf("Mapped %d sector%s.\n",sectors,sectors == 1 ? "" : "s");
}
Exemplo n.º 6
0
/* remember a file block using next available cache block */ 
int remember_fblock(const char *name, int fblock, const char *block) {
    Content *f; 
    Map *b; 
    int cblock; 
    if (strlen(name)>255) { 
	flog("name %s too long",name); 
	return FALSE;
    } 
    if (!block) { 
	flog("can't read from NULL pointer"); 
	return FALSE; 
    } 
    f = content_add(name); 
    if ((b=content_fblock_stored(f,fblock))) { 
	cblock=b->cblock; 
	flog("reused old cache block %d",cblock); 
    } else {
	cblock=next_cblock(); 
	flog("chose new cache block %d",cblock); 
    } 
    storage_put(cblock, (const char *)block); 
    map_add(&(f->bhead),fblock,cblock); 
    flog("stored block %d of %s (cblock %d)",fblock,name,cblock); 
    return TRUE; 
} 
Exemplo n.º 7
0
/**
 * Add non-existing key into the multimap.
 *
 * @tmap multimap.
 * @key key
 * @newcell prepared inserted value.
 *
 * @return 0 in success,
 *	   -ENOMEM if no memory.
 *	   -EEXIST if key already exists.
 */
static int multimap_add_newkey(
    struct multimap *tmap, u64 key,
    struct tree_cell *newcell, gfp_t gfp_mask)
{
    int ret;
    struct tree_cell_head *newhead;

    /* Allocate and initialize new tree cell head. */
    newhead = alloc_cell_head(tmap->mmgr, gfp_mask);
    if (!newhead) {
        LOGe("memory allocation failed.\n");
        return -ENOMEM;
    }
    newhead->key = key;
    INIT_HLIST_HEAD(&newhead->head);
    hlist_add_head(&newcell->list, &newhead->head);
    ASSERT(!hlist_empty(&newhead->head));

    /* Add to the map. */
    ret = map_add((struct map *)tmap, key, (unsigned long)newhead, gfp_mask);
    if (ret != 0) {
        free_cell_head(tmap->mmgr, newhead);
        LOGe("map_add failed.\n");
        ASSERT(ret != -EINVAL);
    }
    return ret;
}
Exemplo n.º 8
0
void add_int_variable(_map *mem, char *_name, int _value) {
	_variable *var = (_variable *)malloc(sizeof(_variable)
		+ (strlen(_name) + 1) * sizeof(char));
	//	variable length name

	if (var) {
		var->int_value = _value;
		var->type = INT;
		var->is_constant = false;
		strcpy(var->name, _name);
		void **p_old_var;
		_variable *old_var;
		if ((p_old_var = map_get(mem, _name)) && (old_var = (_variable *)*p_old_var)) {
			free(var);
			error = VAR_REDEFINE;
			return;
		}
		map_add(mem, _name, var);
	}
	else {
		error = MALLOC_ERROR;
	}

	return;
}
Exemplo n.º 9
0
/* map a file block to a specific cache block */
int remember_fblock_at_cblock(const char *name, int fblock, int cblock, const char *block) {
    Content *f, *g; 
    if (strlen(name)>255) { 
	flog("name %s too long",name); 
	return FALSE;
    } 
    if (cblock<0 || cblock>=storage_blocks) {
	flog("cache block %d does not exist",cblock); 
	return FALSE; 
    } 
    f = content_add(name); 
    if ((g=clist_cblock_used(cblock))) { 
	Map *b=content_cblock_used(g,cblock); 
	flog("cache block %d in use for (%s %d) (removing)",cblock,g->name,b->fblock); 
	content_forget_cblock(g,cblock); 
    } 
    Map *b; 
    if ((b=content_fblock_stored(f,fblock))) { 
	flog("file block %d already stored for file %s (local %d) (removing)",fblock,f->name,b->cblock); 
	storage_forget(b->cblock); 
	content_forget_fblock(f,fblock); 
    } 
    storage_put(cblock, (const char *)block); 
    map_add(&(f->bhead),fblock,cblock); 
    flog("stored block %d of %s (cblock %d)",fblock,name,cblock); 
    return TRUE; 
} 
Exemplo n.º 10
0
map_t *
map_alloc(off_t start, off_t size)
{
	off_t delta;
	map_t *m;

	if (start == 0 && size != 0)
		size = ROUNDUP(size);
	for (m = mediamap; m != NULL; m = m->map_next) {
		if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
			continue;
		if (start != 0 && m->map_start > start)
			return (NULL);
		delta = (start != 0) ? start - m->map_start : ROUNDUP(m->map_start) - m->map_start;
		if (size == 0 || m->map_size - delta >= size) {
			if (m->map_size - delta <= 0)
				continue;
			if (size == 0) {
				size = m->map_size - delta;
				if (start == 0)
					size = ROUNDDOWN(size);
			}
			return (map_add(m->map_start + delta, size,
				    MAP_TYPE_GPT_PART, NULL));
		}
	}

	return (NULL);
}
Exemplo n.º 11
0
void map_read(struct map_ent **melp)
{
	FILE *f;
	char buf[8192];
	char path[201];
	int uuid[4];
	char devnm[32];
	char metadata[30];

	*melp = NULL;

	f = open_map(MAP_READ);
	if (!f) {
		RebuildMap();
		f = open_map(MAP_READ);
	}
	if (!f)
		return;

	while (fgets(buf, sizeof(buf), f)) {
		path[0] = 0;
		if (sscanf(buf, " %s %s %x:%x:%x:%x %200s",
			   devnm, metadata, uuid, uuid+1,
			   uuid+2, uuid+3, path) >= 7) {
			map_add(melp, devnm, metadata, uuid, path);
		}
	}
	fclose(f);
}
Exemplo n.º 12
0
int map_update(struct map_ent **mpp, char *devnm, char *metadata,
	       int *uuid, char *path)
{
	struct map_ent *map, *mp;
	int rv;

	if (mpp && *mpp)
		map = *mpp;
	else
		map_read(&map);

	for (mp = map ; mp ; mp=mp->next)
		if (strcmp(mp->devnm, devnm) == 0) {
			strcpy(mp->metadata, metadata);
			memcpy(mp->uuid, uuid, 16);
			free(mp->path);
			mp->path = path ? xstrdup(path) : NULL;
			mp->bad = 0;
			break;
		}
	if (!mp)
		map_add(&map, devnm, metadata, uuid, path);
	if (mpp)
		*mpp = NULL;
	rv = map_write(map);
	map_free(map);
	return rv;
}
Exemplo n.º 13
0
static obj_ptr _map_add(obj_ptr arg1, obj_ptr arg2, obj_ptr arg3, obj_ptr env)
{
    if (NMAPP(arg1))
        return MKERROR(MKSTRING("Expected a map in map-add"), arg1);

    map_add(&MAP(arg1), arg2, arg3);
    return NIL;
}
Exemplo n.º 14
0
int main ()
{
    Hashable *hashable1 = make_hashable_int (1);
    Hashable *hashable2 = make_hashable_string ("Allen");
    Hashable *hashable3 = make_hashable_int (2);

    // make a list by hand
    Value *value1 = make_int_value (17);
    Node *node1 = make_node(hashable1, value1, NULL);
    print_node (node1);

    Value *value2 = make_string_value ("Downey");
    Node *list = prepend(hashable2, value2, node1);
    print_list (list);

    // run some test lookups
    Value *value = list_lookup (list, hashable1);
    print_lookup(value);

    value = list_lookup (list, hashable2);
    print_lookup(value);

    value = list_lookup (list, hashable3);
    print_lookup(value);

    // make a map
    Map *map = make_map(10);
    map_add(map, hashable1, value1);
    map_add(map, hashable2, value2);

    printf ("Map\n");
    print_map(map);

    // run some test lookups
    value = map_lookup(map, hashable1);
    print_lookup(value);

    value = map_lookup(map, hashable2);
    print_lookup(value);

    value = map_lookup(map, hashable3);
    print_lookup(value);

    return 0;
}
Exemplo n.º 15
0
int global_add_reconnect(int id, on_reconnect_cb * cb, void * client, void * addition){
	struct reconnect * rc = (struct reconnect *)malloc(sizeof(struct reconnect));
	if(!rc)return -1;
	rc->cb = cb;
	rc->client = client;
	rc->addition = addition;
	if(map_add(self.rcmq, id, rc) < 0){
		free(rc);
		return -1;
	}
	return 0;
}
Exemplo n.º 16
0
Status
event_init (Server *server)
{
  Event *event = &server->event;
  int rc = 0;

  event->reusable_base = NULL;
  event->base_alloc = false;

  debug ("event map init");  
  event_map = string_map_new (EventKey, node, key);
  if (!event_map)
    return G_ERR;
  
  size_t i = sizeof (event_keys) / sizeof (EventKey);
  while (i--)
    if (map_add (event_map, &event_keys[i], event_keys[i].key_size)
        != MAP_OK)
      goto error;
  
  debug ("json parser init");
  event->json = json_parser_new ();
  if (!event->json)
    goto error;

  debug ("udp init");
  rc = uv_udp_init (server->loop, (uv_udp_t *) event);
  if (rc < 0)
    goto error;

  /* Init sockaddr used for UDP.  TODO: should parse either a multiaddr or
   * "ip:port" string ("[ip]:port" for ipv6). */
  debug ("udp host init: %s:%d", server->host_ip, server->host_port);
  struct sockaddr_storage host;
  rc = uv_ip4_addr (server->host_ip, server->host_port,
                    (struct sockaddr_in *) &host);
  if (rc < 0)
    goto error;

  debug ("udp bind: %s:%d", server->host_ip, server->host_port);
  rc = uv_udp_bind ((uv_udp_t *) event, (const struct sockaddr *) &host,
                    UV_UDP_REUSEADDR);
  if (rc < 0)
    goto error;

  return G_OK;

error:
  if (rc)
    debug ("error: %s", uv_strerror (rc));
  return G_ERR;
}
Exemplo n.º 17
0
static void	storage_file(t_map **addr, int fd, off_t size, char *file_name)
{
	char	map[size + 1];
	int		ret;
	int		tmp;

	ft_bzero((char *)map, (size + 1));
	ret = 0;
	while (((tmp = read(fd, (map + ret), size - ret)) < size) && tmp != (-1))
		ret += tmp;
	((tmp == (-1)) ? error_call_system("read") : 0);
	map_add(addr, map, file_name);
}
Exemplo n.º 18
0
Arquivo: cli.c Projeto: draringi/gini
void registerCLI(char *key, void (*handler)(),
		 char *shelp, char *usage, char *lhelp)
{
	cli_entry_t *clie = (cli_entry_t *) malloc(sizeof(cli_entry_t));

	clie->handler = handler;
	strcpy(clie->long_helpstr, lhelp);
	strcpy(clie->usagestr, usage);
	strcpy(clie->short_helpstr, shelp);
	strcpy(clie->keystr, key);

	verbose(2, "adding command %s.. to cli map ", key);
	map_add(cli_map, key, clie);

}
Exemplo n.º 19
0
void
op_mappush(void)
{
    int		 num = frame.m->code[frame.pc++], i;
    Var		 v;
    Var		 from, to;

    v.type = MAP;
    v.v.map = map_new(num);
    for (i = 0; i < num; i++) {
	to = pop();  from = pop();
	v.v.map = map_add(v.v.map, from, to);
    }
    push(v);
}
Exemplo n.º 20
0
int main()
{
	map *m;
	string *k;
	string *v;
	string *q;
	map_iterator *n;
	pair *t;

	m = map_init(stcmp);
	q = string_init_cstring("");
	k = string_init();
	string_read_line(k);

	while(string_cmp(k, q))
	{
		v = string_init();
		string_read_line(v);
		map_add(m, k, v);

		k = string_init();
		string_read_line(k);
	}

	k = string_free(k);

	/* Iterate through map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_print(t->first);
		printf(" => ");
		string_println(t->second);
	}

	/* Free map. */
	for(n = map_begin(m); n; n = map_next(n))
	{
		t = n->data;
		string_free(t->first);
		string_free(t->second);
	}

	string_free(q);
	m = map_free(m);

	return 0;
}
Exemplo n.º 21
0
map_t
map_alloc(gpt_t gpt, off_t start, off_t size, off_t alignment)
{
	off_t delta;
	map_t m;

	if (alignment > 0) {
		if ((start % alignment) != 0)
			start = (start + alignment) / alignment * alignment;
		if ((size % alignment) != 0)
			size = (size + alignment) / alignment * alignment;
	}

	for (m = gpt->mediamap; m != NULL; m = m->map_next) {
		if (m->map_type != MAP_TYPE_UNUSED || m->map_start < 2)
			continue;
		if (start != 0 && m->map_start > start)
			return NULL;

		if (start != 0)
			delta = start - m->map_start;
		else if (alignment > 0 && m->map_start % alignment != 0)
			delta = (m->map_start + alignment) /
			        alignment * alignment - m->map_start;
		else
			delta = 0;

                if (size == 0 || m->map_size - delta >= size) {
			if (m->map_size - delta < alignment)
				continue;
			if (size == 0) {
				if (alignment > 0 &&
				    (m->map_size - delta) % alignment != 0)
					size = (m->map_size - delta) /
					    alignment * alignment;
				else
					size = m->map_size - delta;
			}
			return map_add(gpt, m->map_start + delta, size,
			    MAP_TYPE_GPT_PART, NULL, 0);
		}
	}

	return NULL;
}
Exemplo n.º 22
0
/******************************************************************************
    rest_var := '=' expr
                rest_num_op
 *****************************************************************************/
void parse_rest_var( val_t* val )
{
    if ( match_char( '=' ) ) {
        val_t vexp;
        parse_expr( &vexp );
        if ( vexp.type != TYPE_FLOAT ) {
            printf("Error: Tried to assign non-number to %s.\n", val->d.variable );
            longjmp( env, 1 );
        }

        printf("Assigned to %s: ", val->d.variable );
        map_add( val->d.variable, vexp.d.fval );
        *val = vexp;

    } else {
      parse_rest_num_op( val );
    }
}
Exemplo n.º 23
0
map_table_t *json_to_map(bombyx_env_t *env, json_t *json)
{
    size_t index;
    json_t *value;
    const char *key;
    map_table_t *map;

    // TODO: minimum map size (100?)

    if (json_is_object(json))
    {
        map = map_table_create(json_object_size(json));
        json_object_foreach(json, key, value)
        {
            var v = {0};
            if (json_is_object(value))
            {
                // process recursively
                v.type = VAR_MAP;
                v.data = json_to_map(env, value);
                v.data_size = sizeof(map_table_t);
            }
            else if (json_is_array(value))
            {
                v.type = VAR_ARRAY;
                v.data = json_to_array(env, value);
                v.data_size = sizeof(array_t);
            }
            else if (json_is_string(value))
            {
                v.type = VAR_STRING;
                v.data = strdup(json_string_value(value));
                v.data_size = json_string_length(value) + 1;
            }
            else if (json_is_number(value))
            {
                v.type = VAR_DOUBLE;
                v.data = challoc(env->pool_of_doubles);
                v.data_size = sizeof(double);
                *(double *)v.data = json_number_value(value);
            }
            map_add(env, map, (char *)key, v);
        }
    }
Exemplo n.º 24
0
void boot_other(char *loader,char *boot,char *part,IMAGE_DESCR *descr)
{
    int l_fd,p_fd,walk,found;
    BOOT_SECTOR buff;

    if (verbose) printf("Boot other: %s, on %s, loader %s\n",boot,part,loader);
    (void) geo_open(&geo,boot,O_NOACCESS);
    if ((l_fd = open(loader,O_RDONLY)) < 0)
	die("open %s: %s",loader,strerror(errno));
    if (!*part) memset(&buff,0,SECTOR_SIZE);
    else {
	if ((p_fd = open(part,O_RDONLY)) < 0)
	    die("open %s: %s",part,strerror(errno));
	if (read(p_fd,(char *) &buff,SECTOR_SIZE) != SECTOR_SIZE)
	    die("read %s: %s",part,strerror(errno));
    }
    if (read(l_fd,(char *) &buff,PART_TABLE_OFFSET) < 0)
	die("read %s: %s",loader,strerror(errno));
    check_version(&buff,STAGE_CHAIN);
    if (*part) {
	found = 0;
	for (walk = 0; walk < PARTITION_ENTRIES; walk++)
	    if (!PART(buff,walk).sys_ind || PART(buff,walk).start_sect != geo.start) {
		if (PART(buff,walk).sys_ind != PART_DOS12 && PART(buff,walk).
		  sys_ind != PART_DOS16) PART(buff,walk).sys_ind = PART_INVALID;
	    }
	    else {
		if (found) die("Duplicate entry in partition table");
		buff.par_c.offset = walk*PARTITION_ENTRY;
		PART(buff,walk).boot_ind = 0x80;
		found = 1;
	    }
	if (!found) die("Partition entry not found.");
	(void) close(p_fd);
    }
    (void) close(l_fd);
    map_begin_section();
    map_add_sector(&buff);
    map_add(&geo,0,1);
    (void) map_end_section(&descr->start);
    geo_close(&geo);
    if (verbose > 1) printf("Mapped 2 (1+1) sectors.\n");
}
Exemplo n.º 25
0
void boot_image(char *spec,IMAGE_DESCR *descr)
{
    BOOT_SECTOR buff;
    int fd,sectors;

    if (verbose) printf("Boot image: %s\n",spec);
    fd = geo_open(&geo,spec,O_RDONLY);
    map_begin_section();
    if (fstat(fd,&st) < 0) die("fstat %s: 5s",spec,strerror(errno));
    if (read(fd,(char *) &buff,SECTOR_SIZE) != SECTOR_SIZE)
	die("read %s: %s",spec,strerror(errno));
    if (buff.par_l.root_dev) descr->root_dev = buff.par_l.root_dev;
    if (buff.par_l.swap_dev) descr->swap_dev = buff.par_l.swap_dev;
    map_add(&geo,1,(st.st_size+SECTOR_SIZE-1)/SECTOR_SIZE-1);
    sectors = map_end_section(&descr->start);
    geo_close(&geo);
    if (verbose > 1)
	printf("Mapped %d sector%s.\n",sectors,sectors == 1 ? "" : "s");
}
Exemplo n.º 26
0
static int
do_asgnindex(Var var, Var idx, Var expr, Var *ret)
{
    ret->type = var.type;
    if (var.type == LIST) {
	if (idx.type != NUM) {
	    var_free(idx);  var_free(expr);
	    raise(E_TYPE);  return -1;
	} else if (idx.v.num <= 0 || idx.v.num > var.v.list->len) {
	    var_free(expr);
	    raise(E_RANGE);  return -2;
	} else {
	    ret->v.list = list_assign(var.v.list, expr, idx.v.num);
	    return 0;
	}
    } else if (var.type == MAP) {
	ret->v.map = map_add(var.v.map, idx, expr);
	return 0;
    } else {
	var_free(expr);  var_free(idx);
	raise(E_TYPE);  return -3;
    }
}    
Exemplo n.º 27
0
static int
recover_gpt_tbl(gpt_t gpt, int type, off_t start)
{
	const char *name, *origname;
	map_t *dtbl, stbl;

	switch (type) {
	case MAP_TYPE_PRI_GPT_TBL:
		dtbl = &gpt->tbl;
		stbl = gpt->lbt;
		origname = "secondary";
		name = "primary";
		break;
	case MAP_TYPE_SEC_GPT_TBL:
		dtbl = &gpt->lbt;
		stbl = gpt->tbl;
		origname = "primary";
		name = "secondary";
		break;
	default:
		gpt_warn(gpt, "Bad table type %d", type);
		return -1;
	}

	*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
	if (*dtbl == NULL) {
		gpt_warnx(gpt, "Adding %s GPT table failed", name);
		return -1;
	}
	if (gpt_write(gpt, *dtbl) == -1) {
		gpt_warnx(gpt, "Writing %s GPT table failed", name);
		return -1;
	}
	gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
	return 0;
}
Exemplo n.º 28
0
void RebuildMap(void)
{
	struct mdstat_ent *mdstat = mdstat_read(0, 0);
	struct mdstat_ent *md;
	struct map_ent *map = NULL;
	int require_homehost;
	char sys_hostname[256];
	char *homehost = conf_get_homehost(&require_homehost);

	if (homehost == NULL || strcmp(homehost, "<system>")==0) {
		if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
			sys_hostname[sizeof(sys_hostname)-1] = 0;
			homehost = sys_hostname;
		}
	}

	for (md = mdstat ; md ; md = md->next) {
		struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS);
		struct mdinfo *sd;

		if (!sra)
			continue;

		for (sd = sra->devs ; sd ; sd = sd->next) {
			char namebuf[100];
			char dn[30];
			int dfd;
			int ok;
			int devid;
			struct supertype *st;
			char *subarray = NULL;
			char *path;
			struct mdinfo *info;

			sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
			dfd = dev_open(dn, O_RDONLY);
			if (dfd < 0)
				continue;
			st = guess_super(dfd);
			if ( st == NULL)
				ok = -1;
			else {
				subarray = get_member_info(md);
				ok = st->ss->load_super(st, dfd, NULL);
			}
			close(dfd);
			if (ok != 0)
				continue;
			if (subarray)
				info = st->ss->container_content(st, subarray);
			else {
				info = xmalloc(sizeof(*info));
				st->ss->getinfo_super(st, info, NULL);
			}
			if (!info)
				continue;

			devid = devnm2devid(md->devnm);
			path = map_dev(major(devid), minor(devid), 0);
			if (path == NULL ||
			    strncmp(path, "/dev/md/", 8) != 0) {
				/* We would really like a name that provides
				 * an MD_DEVNAME for udev.
				 * The name needs to be unique both in /dev/md/
				 * and in this mapfile.
				 * It needs to match what -I or -As would come
				 * up with.
				 * That means:
				 *   Check if array is in mdadm.conf
				 *        - if so use that.
				 *   determine trustworthy from homehost etc
				 *   find a unique name based on metadata name.
				 *
				 */
				struct mddev_ident *match = conf_match(st, info,
								       NULL, 0,
								       NULL);
				struct stat stb;
				if (match && match->devname && match->devname[0] == '/') {
					path = match->devname;
					if (path[0] != '/') {
						strcpy(namebuf, "/dev/md/");
						strcat(namebuf, path);
						path = namebuf;
					}
				} else {
					int unum = 0;
					char *sep = "_";
					const char *name;
					int conflict = 1;
					if ((homehost == NULL ||
					     st->ss->match_home(st, homehost) != 1) &&
					    st->ss->match_home(st, "any") != 1 &&
					    (require_homehost
					     || ! conf_name_is_free(info->name)))
						/* require a numeric suffix */
						unum = 0;
					else
						/* allow name to be used as-is if no conflict */
						unum = -1;
					name = info->name;
					if (!*name) {
						name = st->ss->name;
						if (!isdigit(name[strlen(name)-1]) &&
						    unum == -1) {
							unum = 0;
							sep = "";
						}
					}
					if (strchr(name, ':')) {
						/* Probably a uniquifying
						 * hostname prefix.  Allow
						 * without a suffix, and strip
						 * hostname if it is us.
						 */
						if (homehost && unum == -1 &&
						    strncmp(name, homehost,
							    strlen(homehost)) == 0 &&
						    name[strlen(homehost)] == ':')
							name += strlen(homehost)+1;
						unum = -1;
					}

					while (conflict) {
						if (unum >= 0)
							sprintf(namebuf, "/dev/md/%s%s%d",
								name, sep, unum);
						else
							sprintf(namebuf, "/dev/md/%s",
								name);
						unum++;
						if (lstat(namebuf, &stb) != 0 &&
						    (map == NULL ||
						     !map_by_name(&map, namebuf+8)))
							conflict = 0;
					}
					path = namebuf;
				}
			}
			map_add(&map, md->devnm,
				info->text_version,
				info->uuid, path);
			st->ss->free_super(st);
			free(info);
			break;
		}
		sysfs_free(sra);
	}
	/* Only trigger a change if we wrote a new map file */
	if (map_write(map))
		for (md = mdstat ; md ; md = md->next) {
			struct mdinfo *sra = sysfs_read(-1, md->devnm,
							GET_VERSION);
			if (sra)
				sysfs_uevent(sra, "change");
			sysfs_free(sra);
		}
	map_free(map);
	free_mdstat(mdstat);
}
Exemplo n.º 29
0
Arquivo: main.c Projeto: gc87/ctools
int 
main(int argc, char** argv) {
	char path[50] = { 0x00 };
	if (2 <= argc) {
		strncpy(path, argv[1], strlen(argv[1]));
	} else {
		strncpy(path, "server.cfg", 10);
	}

	//读取配置文件
	TinyConf* config; 
	char* run;
	for(;;) {
		config = conf_open(path, '=');
		if(NULL == config) {
			sleep(5);
			continue;
		}

		run = conf_get(config, "run");
		if(0 >= strlen(run)) { 
			sleep(5);
			continue;
		} 

		if(0 != strcmp(run, "on")) {
			sleep(5);
			continue;
		}

		break; //一切ok,启动正常处理
	}

	//初始化mysql配置
	char* mysql_host = conf_get(config, "mysql_host");
	char* mysql_port_str = conf_get(config, "mysql_port");
	char* mysql_user = conf_get(config, "mysql_user");
	char* mysql_password = conf_get(config, "mysql_password");
	char* mysql_database = conf_get(config, "mysql_database");

	//将数据库设置存入到map中
	TinyMap* store_map = map_create();
	map_add(store_map, "host", mysql_host, NULL);
	map_add(store_map, "port", mysql_port_str, NULL);
	map_add(store_map, "user", mysql_user, NULL);
	map_add(store_map, "password", mysql_password, NULL);
	map_add(store_map, "database", mysql_database, NULL);

	//初始化tcpserver的相关代码
	int tcp_port = 7000;
	char* tcp_port_str = conf_get(config, "port");
	if(0 < strlen(tcp_port_str)) {
		tcp_port = atoi(tcp_port_str);
	}

	TCPServer* tcp_server = tcp_server_create(tcp_port); //生成tcp服务器实例
	if(NULL == tcp_server) {
		fprintf(stderr, "Create server falied.\n");
	}

	tcp_server_set_proc_callback(tcp_server, store_map, analyze_proc); //注册数据处理的回调函数
	tcp_server_set_timeout_callback(tcp_server, store_map, analyze_timeout); //注册客户端连接超时的回调函数

	int ret = tcp_server_start(tcp_server); //启动tcp服务器
	if(-1 == ret) {
 		fprintf(stderr, "Start tcpserver falied.\n");
 		return 0;
	}


	//创建存储实例
	Store* store = store_create(mysql_host, atoi(mysql_port_str), mysql_user, mysql_password, mysql_database);

	//创建网络检测线程
	pthread_t thread_id;
	int p_ret = pthread_create(&thread_id, NULL, (void*)check_networks, store);
	if(0 != p_ret) {
		fprintf(stderr, "%s\n", "create networks check thread err.");
	}

	for(;;) { //阻塞
		sleep(1000);
	}

	pthread_cancel(thread_id); //退出线程
	store_destroy(store); //销毁存储实例
	tcp_server_stop(tcp_server); //停止tcp服务器实例
	tcp_server_destroy(tcp_server); //销毁tcp服务器实例
	map_destroy(store_map); //销毁store_map实例
	conf_close(config); //关闭配置信息读取实例
	return 0;
}
Exemplo n.º 30
0
static void find_includes(llnode_t *head, llist_t *todo, llist_t *paths,
                   set_t *incl, map_t *deps)
{
    FILE *fp;
    llnode_t *tmp;
    char *buffer,*full,*ptr,*end;
    const char *file;

    buffer = (char *)malloc(4096);
    full = (char *)malloc(4096);

    tmp = head;
    while (tmp->next != NULL) {
        file = tmp->key;
        fp = fopen(file,"r");
        if (fp == NULL) {
            perror("Cannot read source");
            fprintf(stderr,"For file: %s\n",file);
            exit(EXIT_FAILURE);
        }

        /* read file line by line and look for #include "..." */
        while (!feof(fp) && !ferror(fp)) {
            if (fgets(buffer,4096,fp) == NULL) continue;
            ptr = buffer;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '#') continue;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*++ptr != 'i') continue;
            if (*++ptr != 'n') continue;
            if (*++ptr != 'c') continue;
            if (*++ptr != 'l') continue;
            if (*++ptr != 'u') continue;
            if (*++ptr != 'd') continue;
            if (*++ptr != 'e') continue;
            ++ptr;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '"') continue;
            ++ptr;
            end = ptr;
            while (*end != '"') {
                if (*end == '\0') {
                    fprintf(stderr,"Unmatched '\"': %s\n",buffer);
                    exit(EXIT_FAILURE);
                }
                ++end;
            }
            *end = '\0';

            /* get full path to include file */
            make_path(ptr,paths,full);
            /* skip, if not found or unreadable. */
            if (full[0] == '\0') continue;

            /* if this is a yet unknown include, add to the
             * todo list, if append is enabled */
            if (set_find(incl,full) == 0) {
                set_add(incl,full);
                llist_append(todo,full);
            }

            map_add(deps,file,full);
        }
        fclose(fp);
        tmp = tmp->next;
    }
    free(buffer);
    free(full);
}