void iwdpm_create_bridge(iwdpm_t self) {
  sm_t sm = sm_new(4096);
  iwdp_t iwdp = iwdp_new(self->frontend);
  if (!sm || !iwdp) {
    sm_free(sm);
    return;
  }
  self->sm = sm;
  self->iwdp = iwdp;
  iwdp->subscribe = iwdpm_subscribe;
  iwdp->attach = iwdpm_attach;
  iwdp->select_port = iwdpm_select_port;
  iwdp->listen = iwdpm_listen;
  iwdp->connect = iwdpm_connect;
  iwdp->send = iwdpm_send;
  iwdp->add_fd = iwdpm_add_fd;
  iwdp->remove_fd = iwdpm_remove_fd;
  iwdp->state = self;
  iwdp->is_debug = &self->is_debug;
  sm->on_accept = iwdpm_on_accept;
  sm->on_sent = iwdpm_on_sent;
  sm->on_recv = iwdpm_on_recv;
  sm->on_close = iwdpm_on_close;
  sm->state = self;
  sm->is_debug = &self->is_debug;
}
Пример #2
0
int simple_test() {
    StackMachine *sm = sm_new();
    int i;
    v4sf f, x= {0.11,0.14,-0.24,-0.85}, y= {0.711,0.223,-0.546, -7.661}, z= {0.1, 0.2, -0.3, -0.4};

    sm_add_constant(sm, 0.5);
    sm_add_constant(sm, 1.0);
    sm_add_constant(sm, 0.0);
    sm_add_constant(sm, 2.0);

    sm_set_global(sm, 0, x[0]);
    sm_set_global(sm, 1, y[1]);
    sm_set_global(sm, 2, z[2]);
    sm_set_stackcur(sm, 10);

    sm_add_opcodes(sm, test_codes, testcodelen);
    f = sm_run(sm, test_codes, testcodelen);

    //printf("RESULT: %f, should be: %f\n", f, MAX(MAX(x, y), z)-0.5);
    /*printf("[");

    for (i=0; i<sm->stackcur+10; i++) {
      if (i > 0) {
        printf(", ");
      }
      printf("%.2f", sm->stack[i]);
    }
    printf("]\n");*/

}
Пример #3
0
int main(int argc, char **argv) {
    
    if (argc != 3) {
        printf("ERROR: Incorrect number of inputs.\n");
        exit(0);
    }
    
    
    
    char *index_file = argv[1];
    char input[MAX_LEN];
    
    if (file_exist(index_file)) {
        do {
            printf("There is already a file named \"%s\", would you like to overwrite this file? Enter yes/no:\n", index_file);
            scanf("%s", input);
            if (strcasecmp(input, "yes") == 0) {
                break;
            }
            else if (strcasecmp(input, "no") == 0) {
                do {
                    printf("Choose another name: \n");
                    scanf("%s", input);
                    index_file = input;
                } while(file_exist(index_file));
                break;
            }
        } while (1);
        
    }
    
    StrMap *hashmap = sm_new(40);
    
    DIR *dir;
    struct dirent *dp;
    
    if ((dir = opendir(argv[2])) == NULL) {     //if file
        FILE *fp = fopen(argv[2], "r");
        filetok(fp, hashmap, argv[2]);
        printf("done\n");
        fclose(fp);
        exit(0);
    }
    
    char *path = (char*)malloc(sizeof(argv[2]) + sizeof(char));
    path = argv[2];
    strcat(path, "/");
    traverse(dir, path, hashmap);
    
    int x = sm_get_count(hashmap);
    printf("THIS IS HTE COUNT: %d\n", x);
    
    return 0;
    
}
Пример #4
0
void fn_v_InitCoreData( nla_core_data* pCoreData, int iHashSize ) 
{
    pCoreData->p_map_root   = NULL ;
    pCoreData->p_data_head  = NULL ;


    pCoreData->p_map_root   = NLA_MM_CALLOC( g_pNLAMM, sizeof( nla_map_node ) ) ;
    fn_v_InitRootMapNode( pCoreData->p_map_root ) ;

    pCoreData->p_id2data_map = sm_new( g_pNLAMM, iHashSize ) ;
}
Пример #5
0
int complicated_test() {
    StackMachine *sm = sm_new();
    int i, totvert, tottri;
    float *verts;
    int *tris;
    v4sf f, x= {0.11,0.14,-0.24,-0.85}, y= {0.711,0.223,-0.546, -7.661}, z= {0.1, 0.2, -0.3, -0.4};
    float min[3] = {-1.0, -1.0, -1.0}, max[3] = {1.0, 1.0, 1.0};
    float mat[4][4] = {
        {1,0,0,0},
        {0,1,0,0},
        {0,0,1,0},
        {0,0,0,1}
    };

    sm_add_constant(sm, 0.5);
    sm_add_constant(sm, 1.0);
    sm_add_constant(sm, 0.0);
    sm_add_constant(sm, 2.0);

    sm_set_global(sm, 0, x[0]);
    sm_set_global(sm, 1, y[0]);
    sm_set_global(sm, 2, z[0]);
    sm_set_stackcur(sm, 10);

    sm_add_opcodes(sm, test_codes, testcodelen);
    sm_set_sampler_machine(sm);

    //f = sm_sampler(x, y, z, 0);

    //f = sm_run(sm, sm->codes, sm->totcode);
    //printf("RESULT: %f, should be: %f\n", f, MAX(MAX(x, y), z)-0.5);
    /*printf("[");

    for (i=0; i<sm->stackcur+10; i++) {
      if (i > 0) {
        printf(", ");
      }
      printf("%.2f", sm->stack[i]);
    }
    printf("]\n");*/

    //void sm_tessellate(float **vertout, int *totvert, int **triout, int *tottri,
    //                  float min[3], float max[3], int ocdepth, int thread) {
    sm_tessellate(&verts, &totvert, &tris, &tottri, min, max, 3, mat, 0);
    printf("\ntotvert: %d, tottri: %d\n", totvert, tottri);
    printf("%p %p", verts, tris);

    //sm_free_tess(verts, tris);
}
Пример #6
0
void sm_set_sampler_machine(StackMachine *sm) {
  int i;
  
  sampler_machines[0] = sm;
  sm->threadnr = 0;
  
  for (i=1; i<MAXTHREAD; i++) {
    StackMachine *sm2;
    
    sm2 = sm_new();
    memcpy(sm2, sm, sizeof(*sm2));
    
    sm2->codes = MEM_copyalloc(sm->codes, sizeof(SMOpCode)*sm->totcode);
    sm2->registers = NULL;
    sm2->totcode = sm->totcode;
    sm2->threadnr = i;
    
    sampler_machines[i] = sm2;
  }
}
Пример #7
0
nvar_t *nvar_init(dim) {

    nvar_t *nvar = malloc(sizeof(nvar_t));

    nvar->dim = dim;

    nvar->n = 0;

    nvar->wsums = calloc(dim, sizeof(double));

    nvar->sum_wprods = sm_new(dim, 0);

    // allocate space for the array of pointers to variable labels
    nvar->labels = calloc(nvar->dim, sizeof(char *));

    // allocate space for the array of pointers to covariance labels
    nvar->plabels = calloc(nvar_count_covariances(nvar), sizeof(char *));

    return nvar;
    
}
Пример #8
0
static int parse_agent_options(char *options)
{
    char *next;
    char *all_conf_files = NULL;

    gdata->tcp_port = 0;
    gdata->size_threshold = 500;
    gdata->reference_chain_length = 0;
    gdata->max_fan_in = 5;
	gdata->debug = 0;
	gdata->self_check = 0;
	gdata->num_elements_to_dump = 5;
	gdata->run_gc = JNI_TRUE;
	gdata->show_unreachables = JNI_FALSE;
	gdata->ignore_classes = sm_new(10, free_class_limit);
	gdata->ignore_referenced_by = sm_new(10, free_referenced_by_limit);

    /* Parse options and set flags in gdata */
    if ( options==NULL )
    {
        return 1;
    }

    alert("Start memory leak detection (options are %s)\n", options);

    next = strtok(options, ",=");

    /* While not at the end of the options string, process this option. */
    while ( next != NULL )
    {
    	if (strcmp(next,"tcp_port") == 0) {
    		char *endptr;
        	next = strtok(NULL, ",");
        	gdata->tcp_port = strtol(next, &endptr, 10);
        	if (*endptr != '\0')
        	{
        		alert("Error: Bad TCP port %s\n", next);
        		return 0;
        	}
        	debug("jleaker: Using tcp_port=%d\n", gdata->tcp_port);
    	}
    	else if (strcmp(next,"size_threshold") == 0)
    	{
    		char *endptr;
        	next = strtok(NULL, ",");
        	gdata->size_threshold = strtol(next, &endptr, 10);
        	if (*endptr != '\0')
        	{
        		alert("Error: Bad size_threshold %s\n", next);
        		return 0;
        	}
        	debug("jleaker: Using size_threshold=%d\n", gdata->size_threshold);
    	}
    	else if (strcmp(next,"reference_chain_length") == 0)
    	{
    		char *endptr;
        	next = strtok(NULL, ",");
        	gdata->reference_chain_length = strtol(next, &endptr, 10);
        	if (*endptr != '\0')
        	{
        		alert("Error: Bad reference_chain_length %s\n", next);
        		return 0;
        	}
        	debug("jleaker: Using reference_chain_length=%d\n", gdata->reference_chain_length);
    	}
    	else if (strcmp(next,"max_fan_in") == 0)
    	{
    		char *endptr;
        	next = strtok(NULL, ",");
        	gdata->max_fan_in = strtol(next, &endptr, 10);
        	if (*endptr != '\0')
        	{
        		alert("Error: Bad max_fan_in %s\n", next);
        		return 0;
        	}
        	debug("jleaker: Using max_fan_in=%d\n", gdata->max_fan_in);
    	}
    	else if (strcmp(next,"debug") == 0)
    	{
    		gdata->debug = 1;
        	debug("jleaker: Using debug=true\n");
    	}
    	else if (strcmp(next,"self_check") == 0)
    	{
    		gdata->self_check = 1;
        	debug("jleaker: Using self_check=true\n");
    	}
    	else if (strcmp(next,"no_gc") == 0)
    	{
    		gdata->run_gc = JNI_FALSE;
        	debug("jleaker: Using no_gc=true\n");
    	}
    	else if (strcmp(next,"consider_local_references") == 0)
    	{
    		gdata->consider_local_references = JNI_TRUE;
        	debug("jleaker: Using consider_local_references=true\n");
    	}
    	else if (strcmp(next,"conf_file") == 0)
    	{
    		all_conf_files = strtok(NULL, ",");
        	debug("jleaker: Using conf_file=%s\n", all_conf_files);
    	}
    	else if (strcmp(next,"show_unreachables") == 0)
    	{
    		gdata->show_unreachables = JNI_TRUE;
        	debug("jleaker: Using show_unreachables=true\n");
    	}
    	else
    	{
    		/* We got a non-empty token and we don't know what it is. */
    		alert("ERROR: Unknown option: %s\n", next);
    		return 0;
    	}
    	next = strtok(NULL, ",=");
    }

    if (NULL != all_conf_files)
    {
        next = strtok(all_conf_files, PATH_SEPARATOR);
        while (NULL != next)
        {
        	if (ini_parse(next, ini_handler, NULL) < 0)
        	{
        		alert("Can't load '%s'\n", next);
        		return 0;
        	}
        	next = strtok(NULL, PATH_SEPARATOR);
        }
    }

    return 1;
}
Пример #9
0
int main(int argc, char **argv) {

  init_config(argc, argv);

  open_authorizations("r");

  init_webfinger();

  /** OPEN MAGIC DATABASE **/

  magic_cookie = magic_open(MAGIC_MIME);
  if(magic_load(magic_cookie, RS_MAGIC_DATABASE) != 0) {
    log_error("Failed to load magic database: %s", magic_error(magic_cookie));
    exit(EXIT_FAILURE);
  }

  log_info("starting process: main");

  if(prctl(PR_SET_NAME, "rs-serve [main]", 0, 0, 0) != 0) {
    log_error("Failed to set process name: %s", strerror(errno));
  }

  /** SETUP EVENT BASE **/

  rs_event_base = event_base_new();
  ASSERT_NOT_NULL(rs_event_base, "event_base_new()");
  log_debug("libevent method: %s", event_base_get_method(rs_event_base));
  event_set_log_callback(log_event_base_message);

  // TODO: add error cb to base

  /** SETUP AUTH TOKENS HASH TABLE **/

  auth_sessions = sm_new(1024); // FIXME: this a hardcoded value.

  /** SETUP MAIN LISTENER **/

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(0);
  sin.sin_port = htons(RS_PORT);

  evhtp_t *server = evhtp_new(rs_event_base, NULL);

  if(RS_USE_SSL) {
    evhtp_ssl_cfg_t ssl_config = {
      .pemfile = RS_SSL_CERT_PATH,
      .privfile = RS_SSL_KEY_PATH,
      .cafile = RS_SSL_CA_PATH,
      // what's this???
      .capath = NULL,
      .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW",
      .ssl_opts = SSL_OP_NO_SSLv2,
      .ssl_ctx_timeout = 60*60*48,
      .verify_peer = SSL_VERIFY_PEER,
      .verify_depth = 42,
      .x509_verify_cb = dummy_ssl_verify_callback,
      .x509_chk_issued_cb = dummy_check_issued_cb,
      .scache_type = evhtp_ssl_scache_type_internal,
      .scache_size = 1024,
      .scache_timeout = 1024,
      .scache_init = NULL,
      .scache_add = NULL,
      .scache_get = NULL,
      .scache_del = NULL
    };

    if(evhtp_ssl_init(server, &ssl_config) != 0) {
      log_error("evhtp_ssl_init() failed");
      exit(EXIT_FAILURE);
    }
  }

  /* WEBFINGER */

  evhtp_callback_cb webfinger_cb = (RS_WEBFINGER_ENABLED ?
                                    handle_webfinger : reject_webfinger);
  evhtp_set_cb(server, "/.well-known/webfinger", webfinger_cb, NULL);
  // support legacy webfinger clients (we don't support XRD though):
  evhtp_set_cb(server, "/.well-known/host-meta", webfinger_cb, NULL);
  evhtp_set_cb(server, "/.well-known/host-meta.json", webfinger_cb, NULL);

  /* REMOTESTORAGE */

  evhtp_callback_t *storage_cb = evhtp_set_regex_cb(server, "^/storage/([^/]+)/.*$", handle_storage, NULL);

  evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL);

  if(evhtp_bind_sockaddr(server, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) {
    log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }

  /** SETUP AUTH LISTENER **/

  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(0);
  sin.sin_port = htons(RS_AUTH_PORT);

  evhtp_t *server_auth = evhtp_new(rs_event_base, NULL);

  if(RS_USE_SSL) {
    evhtp_ssl_cfg_t ssl_config = {
      .pemfile = RS_SSL_CERT_PATH,
      .privfile = RS_SSL_KEY_PATH,
      .cafile = RS_SSL_CA_PATH,
      // what's this???
      .capath = NULL,
      .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW",
      .ssl_opts = SSL_OP_NO_SSLv2,
      .ssl_ctx_timeout = 60*60*48,
      .verify_peer = SSL_VERIFY_PEER,
      .verify_depth = 42,
      .x509_verify_cb = dummy_ssl_verify_callback,
      .x509_chk_issued_cb = dummy_check_issued_cb,
      .scache_type = evhtp_ssl_scache_type_internal,
      .scache_size = 1024,
      .scache_timeout = 1024,
      .scache_init = NULL,
      .scache_add = NULL,
      .scache_get = NULL,
      .scache_del = NULL
    };

    if(evhtp_ssl_init(server_auth, &ssl_config) != 0) {
      log_error("evhtp_ssl_init() failed");
      exit(EXIT_FAILURE);
    }
  }

  /* AUTH */

  evhtp_set_cb(server_auth, "/authenticate", handle_authenticate, NULL);
  evhtp_set_cb(server_auth, "/authorizations", handle_authorizations, NULL);

  evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL);

  if(evhtp_bind_sockaddr(server_auth, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) {
    log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }

  /** SETUP SIGNALS **/

  sigset_t sigmask;
  sigemptyset(&sigmask);
  sigaddset(&sigmask, SIGINT);
  sigaddset(&sigmask, SIGTERM);
  sigaddset(&sigmask, SIGCHLD);
  ASSERT_ZERO(sigprocmask(SIG_BLOCK, &sigmask, NULL), "sigprocmask()");
  int sfd = signalfd(-1, &sigmask, SFD_NONBLOCK);
  ASSERT_NOT_EQ(sfd, -1, "signalfd()");

  struct event *signal_event = event_new(rs_event_base, sfd, EV_READ | EV_PERSIST,
                                         handle_signal, NULL);
  event_add(signal_event, NULL);

  /** RUN EVENT LOOP **/

  if(RS_DETACH) {
    int pid = fork();
    if(pid == 0) {
      event_reinit(rs_event_base);

      if(RS_LOG_FILE == stdout) {
        log_warn("No --log-file option given. Future output will be lost.");
        freopen("/dev/null", "r", stdout);
        freopen("/dev/null", "r", stderr);
      }

      return event_base_dispatch(rs_event_base);
    } else {
      printf("rs-serve detached with pid %d\n", pid);
      if(RS_PID_FILE) {
        fprintf(RS_PID_FILE, "%d", pid);
        fflush(RS_PID_FILE);
      }
      _exit(EXIT_SUCCESS);
    }
  } else {
    if(RS_PID_FILE) {
      fprintf(RS_PID_FILE, "%d", getpid());
      fflush(RS_PID_FILE);
    }
    return event_base_dispatch(rs_event_base);
  }
}