int main(int argc, char *argv[]) { erl_init(NULL, 0); loop(); return 0; }
//HsBool HsInt vaskerl_init_connect (char *name, char *ip, char *host, char *dest, char *cookie) { if (fd > -1) return fd; struct in_addr addr; addr.s_addr = inet_addr(ip); // struct hostent *he = erl_gethostbyname("127.0.0.1"); // printf("... %s\n", he->h_name); char fullname[256]; sprintf(fullname, "%s@%s", name, host); erl_init(NULL, 0); if (erl_connect_xinit(host, name, fullname, &addr, cookie, 0) < 0) //if (erl_connect_xinit("McHoovy.rd.shawcable.net", "vaskerl", // "*****@*****.**", &addr, // cookie, 0) < 0) erl_err_quit("erl_connect_xinit failed"); printf("node=%s, host=%s, alive=%s, creation=%d\n", erl_thisnodename(), erl_thishostname(), erl_thisalivename(), erl_thiscreation()); if ((fd = erl_xconnect(&addr, dest)) < 0) erl_err_quit("erl_xconnect failed"); return fd; //return HS_BOOL_TRUE; }
DICT * dict_erlang_open(const char *key, int open_flags, int dict_flags) { DICT_ERLANG *dict_erlang; if (open_flags != O_RDONLY) msg_fatal("%s:%s map requires O_RDONLY access mode", DICT_TYPE_ERLANG, key); erl_init(NULL, 0); dict_erlang = (DICT_ERLANG *)dict_alloc(DICT_TYPE_ERLANG, key, sizeof(DICT_ERLANG)); dict_erlang->dict.lookup = dict_erlang_lookup; dict_erlang->dict.close = dict_erlang_close; dict_erlang->dict.flags = dict_flags; erlang_parse_config(dict_erlang, key); if (dict_erlang->nodes->argc == 0) msg_fatal("no erlang nodes specified"); dict_erlang->active_node = 0; if (dict_erlang->cookie == NULL) msg_fatal("no erlang cookie specified"); if (dict_erlang->mod == NULL) msg_fatal("no erlang module specified"); if (dict_erlang->fun == NULL) msg_fatal("no erlang function specified"); return (DICT_DEBUG(&dict_erlang->dict)); }
void erts_short_init(void) { int ncpu = early_init(NULL, NULL); erl_init(ncpu); erts_initialized = 1; }
int main() { ETERM *tuplep, *intp; ETERM *fnp, *argp; int res; byte buf[100]; long allocated, freed; erl_init(NULL, 0); while (read_cmd(buf) > 0) { tuplep = erl_decode(buf); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 17) == 0) { res = bar(ERL_INT_VALUE(argp)); } intp = erl_mk_int(res); erl_encode(intp, buf); write_cmd(buf, erl_term_len(intp)); erl_free_compound(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(intp); } }
MAIN(int argc, char *argv[]) { int i, n, no_threads; #ifndef VXWORKS #ifdef __WIN32__ HANDLE threads[100]; #else pthread_t threads[100]; #endif #endif if (argc < 3) exit(1); erl_init(NULL, 0); cookie = argv[1]; n = atoi(argv[2]); if (n > 100) exit(2); desthost = argv[3]; #ifndef VXWORKS no_threads = argv[4] != NULL && strcmp(argv[4], "nothreads") == 0; #else no_threads = 1; #endif for (i = 0; i < n; ++i) { if (!no_threads) { #ifndef VXWORKS #ifdef __WIN32__ unsigned tid; threads[i] = (HANDLE)_beginthreadex(NULL, 0, einode_thread, (void*)i, 0, &tid); #else pthread_create(&threads[i], NULL, einode_thread, (void*)i); #endif #else ; #endif } else einode_thread((void*)i); } if (!no_threads) #ifndef VXWORKS for (i = 0; i < n; ++i) { #ifdef __WIN32__ if (WaitForSingleObject(threads[i], INFINITE) != WAIT_OBJECT_0) #else if (pthread_join(threads[i], NULL) != 0) #endif printf("bad wait thread %d\n", i); } #else ; #endif printf("ok\n"); return 0; }
void erts_short_init(void) { int ncpu = early_init(NULL, NULL); erl_init(ncpu, ERTS_DEFAULT_MAX_PROCESSES, ERTS_DEFAULT_MAX_PORTS, 0); erts_initialized = 1; }
static int init(int *sd, int *portnr, int *epmd_fd) { char host[HOSTNAMESZ]; char servernode[NODENAMESZ]; struct hostent *h; int error = 0; #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); if ((error = WSAStartup(wVersionRequested, &wsaData))) { fprintf(stderr,"Can't initialize windows sockets: %d",error); } #endif /* get the host name */ error = gethostname(host,HOSTNAMESZ); if (error) { #ifdef __WIN32__ fprintf(stderr,"can't find own hostname (error = %ld) !\n",WSAGetLastError()); #else /* not __WIN32__ */ fprintf(stderr,"can't find own hostname !\n"); #endif } else { /* identify host */ if (!(h = erl_gethostbyname(host))) fprintf(stdout,"can't find own ip address\n"); else { /* get a listen port. 0 means let system choose port number */ *sd = getlisten(0); /* what port did we get? */ /* this call not necessary if we specified port in call to getlisten() */ *portnr = getport(*sd); /* make the nodename server@host */ sprintf(servernode,"%s@%s",SERVER,host); /* initiate */ erl_init(NULL,0); /* host, alive, alive@host, addr, cookie, creation */ erl_connect_xinit(host,SERVER,servernode,(Erl_IpAddr)(h->h_addr_list[0]),COOKIE,0); /* let epmd know we are here */ *epmd_fd = erl_publish(*portnr); return 0; } } return -1; }
int main() { ETERM *tuplep; ETERM *fnp; ETERM *args; byte buf[100]; const char* func_name; #ifdef __WIN32__ /* Attention Windows programmers: you need [to pay Serge Aleynikov a * beer because he's the one who figured out how to get this to work * on windows :)] explicitly set mode of stdin/stdout to binary or * else the port program won't work. */ _setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stdin), _O_BINARY); #endif eNotify_init(); erl_init(NULL, 0); while (read_cmd(buf) > 0) { tuplep = erl_decode(buf); fnp = erl_element(1, tuplep); func_name = (const char*)ERL_ATOM_PTR(fnp); args = erl_element(2, tuplep); // MATCH FIRST! -> REMEMBER THAT! if (strncmp(func_name, "get_error_desc", 14) == 0) { local_get_error_desc(args); } else if (strncmp(func_name, "add_watch", 9) == 0) { local_add_watch(args); } else if (strncmp(func_name, "remove_watch", 12) == 0) { local_remove_watch(args); } else { byte buf[10]; ETERM *nok = erl_mk_atom("undef"); // alloc nok erl_encode(nok, buf); write_cmd(buf, erl_term_len(nok)); erl_free_term(nok); // free nok } erl_free_compound(tuplep); erl_free_term(fnp); } return 0; }
int main(int argc, char **argv) { int fd; /* fd to Erlang node */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *tuplep, *fnp, *argp, *resp; int res; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); if ((fd = erl_connect("e1@hostname")) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to e1@hostname\n\r"); while (loop) { got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { fromp = erl_element(1, emsg.msg); tuplep = erl_element(2, emsg.msg); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) { res = bar(ERL_INT_VALUE(argp)); } resp = erl_format("{cnode, ~i}", res); erl_send(fd, fromp, resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(resp); } } } }
int main(int argc, char **argv) { int fd; /* file descriptor of Erlang node */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *argp, *resp; /* Reps of Erlang terms */ int res; /* Result of the fac call */ /* initialize erl_interface (once only) */ erl_init(NULL, 0); /* initialize the connection mechanism */ if (erl_connect_init(1, "refactorcookie", 0) == -1) erl_err_quit("erl_connect_init"); /* connect to a running Erlang node */ if ((fd = erl_connect("blah@Simon-Thompsons-Computer-2")) < 0) erl_err_quit("erl_connect"); while (loop) { /* message received */ got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { /* unpack message fields */ fromp = erl_element(1, emsg.msg); argp = erl_element(2, emsg.msg); /* call fac and send result back */ resp = erl_format("{ok, ~i}", fac(ERL_INT_VALUE(argp))); erl_send(fd, fromp, resp); /* free the term storage used */ erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(argp); erl_free_term(resp); } } } }
/* -------------------------------------------------- */ int efs_init() { int ret, fd; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); if ((fd = erl_connect("e1@uss")) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to e1@uss\n\r"); erlang_fd = fd; return 0; }
rockse * open(const char *path) { rockse *p; char ** errptr; erl_init(NULL, 0); p = malloc(sizeof(rockse)); p->options = rocksdb_options_create(); rocksdb_options_set_create_if_missing(p->options, 1); p->writeoptions = rocksdb_writeoptions_create(); p->readoptions = rocksdb_readoptions_create(); p->db = rocksdb_open(p->options, path, errptr); return p; }
int main(int argc, char *argv[]) { erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); int fd = -1; if ((fd = erl_connect("nefs@Daniels-MacBook")) < 0) erl_err_quit("erl_connect"); ferl_state FS={ .erlang_fd = fd }; fprintf(stderr, "Connected to nefs@Daniels-MacBook\n\r"); erl_rpc(fd,"io","format",erl_format("[ping]")); int retval = fuse_main(argc, argv, &hello_oper, (void *)&FS); fprintf(stderr, "Finished\n\r"); return retval; }
int setup_connection(int number, char* cookie, short creation, char* nodename) { int erl_fd; erl_init(NULL, 0); if(erl_connect_init(number, cookie, creation) == -1) { erl_err_quit("erl_connect_init failed"); } TRACE(("erl connection initialized")); erl_fd = erl_connect(nodename); if (erl_fd < 0) { erl_err_quit("erl_connect failed"); } TRACE(("connected!")); return erl_fd; }
int main() { int num_threads = 200; pthread_t threads[num_threads]; int active_thread_count = 0; byte buf[1000000]; erl_init(NULL, 0); while (read_cmd(buf) > 0) { if(active_thread_count == num_threads){ clear_active_threads(threads, active_thread_count); active_thread_count = 0; } handle_command(buf); active_thread_count = active_thread_count + 1; } clear_active_threads(threads, active_thread_count); return 0; }
int main(int argc, char *argv[]) { ETERM *msg; openlog(); logmsg("INFO: starting up."); erl_init(NULL, 0); mysql_init(&dbh); while ((msg = read_msg()) != NULL) { dispatch_db_cmd(msg); erl_free_term(msg); } mysql_close(&dbh); logmsg("INFO: shutting down."); closelog(); return 0; }
int main() #endif { _setmode(0, _O_BINARY); _setmode(1, _O_BINARY); erl_init(NULL, 0); for (;;) { char buf[4*1024]; ETERM* term; char* message; int n; if (readn(0, buf, 2) <= 0) { /* fprintf(stderr, "error reading message header\n"); */ /* actually this is where we leave the infinite loop */ exit(1); } n = buf[0] * 256 + buf[1]; if (readn(0, buf, n) < 0) { fprintf(stderr, "error reading message contents\n"); exit(1); } term = erl_decode(buf); if (term == NULL) { fprintf(stderr, "erl_decode() failed\n"); exit(1); } n = erl_print_term(stdout, term); erl_free_compound(term); fprintf(stdout,"\r%d\n", n); fflush(stdout); } }
int main(){ double result=0; int i=0; int error = 0; erlang_pid pid; char host[HOSTNAMESZ]; char server_node[HOSTNAMESZ]; char client_node[HOSTNAMESZ]; CORBA_Environment *env; /* Initiate names */ #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); if ((error = WSAStartup(wVersionRequested, &wsaData))) { fprintf(stderr,"Can't initialize windows sockets: %d",error); return 0; } #endif error = gethostname(host,HOSTNAMESZ); if (error) { #ifdef __WIN32__ fprintf(stderr,"can't find own hostname (error = %ld) !\n",WSAGetLastError()); #else /* not __WIN32__ */ fprintf(stderr,"can't find own hostname !\n"); #endif } sprintf(client_node,"%s@%s",CLNODENAME,host); sprintf(server_node,"%s@%s",SNODENAME,host); /* Create and init CORBA_Environment */ env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ); /* Initiating the connection */ erl_init(NULL,0); erl_connect_init(50,COOKIE,0); /* Initiating pid*/ strcpy(pid.node,client_node); pid.num = 99; pid.serial = 0; pid.creation = 0; /* Fixing environment variable */ env->_fd=erl_connect(server_node); strcpy(env->_regname,SREGNAME); env->_to_pid = NULL; env->_from_pid = &pid; if (env->_fd < 0) { fprintf(stderr,"Error : Cannot connect to Server\n"); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); exit(1); } /* Calling the init function */ rmod_random_init(NULL, 1, 2, 3, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ printf("Init complete !\n"); break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } /* Calling the produce function */ for(i=1; i<=10; i++) { result = rmod_random_produce(NULL, env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Init call failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); client_exit(env); default: /* Should not come here */ client_exit(env); } printf("the random number nr%d is %f\n",i,result); } /* Closing the connection */ erl_close_connection(env->_fd); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); return 0; }
int main(int argc, char **argv) { struct in_addr addr; /* 32-bit IP number of host */ int port; /* Listen port number */ int listen; /* Listen socket */ int fd; /* fd to Erlang node */ ErlConnect conn; /* Connection data */ int loop = 1; /* Lopp flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *tuplep, *fnp, *argp, *resp; int res; port = atoi(argv[1]); erl_init(NULL, 0); addr.s_addr = inet_addr("127.0.0.1"); if (erl_connect_xinit("alpha", "cnode", "*****@*****.**", &addr, "secretcookie", 0) == -1) erl_err_quit("erl_connect_xinit"); /* Make a listen socket */ if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if ((fd = erl_accept(listen, &conn)) == ERL_ERROR) erl_err_quit("erl_accept"); fprintf(stderr, "Connected to %s\n\r", conn.nodename); while (loop) { got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { fromp = erl_element(2, emsg.msg); tuplep = erl_element(3, emsg.msg); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) { res = bar(ERL_INT_VALUE(argp)); } resp = erl_format("{cnode, ~i}", res); erl_send(fd, fromp, resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(resp); } } } /* while */ }
int main(int argc, char* argv[]) { ETERM *tuplep, *fnp, *pathp, *binp, *widthp, *heightp; ETERM *res_tuplep; ETERM *res_arrp[2]; ETERM *res_ok_atomp, *res_error_atomp, *res_error_cause_atomp; int idx = 1; byte *buf = malloc(INIT_BUF_SIZE); int buf_len = INIT_BUF_SIZE; int res_len; erl_init(NULL, 0); //Create common atoms(ok, error) res_ok_atomp = erl_mk_atom("ok"); res_error_atomp = erl_mk_atom("error"); res_error_cause_atomp = erl_mk_atom("resize failed"); while (read_cmd(&buf, &buf_len) > 0) { tuplep = erl_decode(buf); fnp = erl_element(idx++, tuplep); pathp = erl_element(idx++, tuplep); binp = erl_element(idx++, tuplep); widthp = erl_element(idx++, tuplep); heightp = erl_element(idx++, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "resize", 6) == 0) { char* algop = ERL_ATOM_PTR(fnp) + 6; resizerl_handler* rhp = (strncmp(algop, "_opencv", 7) == 0) ? resizerl_impls[0] : NULL; if (rhp == NULL) { exit(1); } // ERL_INT_VALUE, ERL_BIN_PTR, ERL_BIN_SIZE void* dp = NULL; rhp->exec(ERL_BIN_PTR(binp), ERL_BIN_SIZE(binp), ERL_BIN_PTR(pathp), ERL_BIN_SIZE(pathp), ERL_INT_VALUE(widthp), ERL_INT_VALUE(heightp), &dp); if (dp == NULL) { exit(2); } unsigned char* dst; int dst_size; rhp->get_result(dp, &dst, &dst_size); res_arrp[0] = res_ok_atomp; res_arrp[1] = erl_mk_binary((const char*)dst, dst_size); res_tuplep = erl_mk_tuple(res_arrp, 2); res_len = erl_term_len(res_tuplep); if (res_len > buf_len) { byte* new_buf = (byte*)realloc((void*)buf, res_len); if (new_buf == NULL) { exit(3); } buf = new_buf; buf_len = res_len; } erl_encode(res_tuplep, buf); write_cmd(buf, erl_term_len(res_tuplep)); erl_free_term(res_arrp[1]); erl_free_term(res_tuplep); rhp->release(&dp); } else { res_arrp[0] = res_error_atomp; res_arrp[1] = res_error_cause_atomp; res_tuplep = erl_mk_tuple(res_arrp, 2); if (res_len > buf_len) { byte* new_buf = (byte*)realloc((void*)buf, res_len); if (new_buf == NULL) { exit(4); } buf = new_buf; buf_len = res_len; } erl_encode(res_tuplep, buf); write_cmd(buf, erl_term_len(res_tuplep)); erl_free_term(res_tuplep); } erl_free_compound(tuplep); erl_free_term(fnp); erl_free_term(pathp); erl_free_term(binp); erl_free_term(widthp); erl_free_term(heightp); idx = 1; } // normal return 0; }
int main(int argc, char **argv) { ETERM *tuplep; ETERM *fnp, *argp; byte buf[1024]; sqlite3 *db; char *zErrMsg = 0; int rc; log = fopen("/tmp/sqlite_port.log", "a+"); fprintf(log, "******start log (%s)******\n", argv[1]); fflush(log); rc = sqlite3_open(argv[1], &db); if (rc) { sqlite3_close(db); exit(1); } erl_init(NULL, 0); while (read_cmd(buf) > 0) { tuplep = erl_decode(buf); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp((const char *)ERL_ATOM_PTR(fnp), "close", 5) == 0) { fprintf(log, "closing sqlite3_close\n"); fflush(log); sqlite3_close(db); break; } else if (strncmp((const char *)ERL_ATOM_PTR(fnp), "list_tables", 11) == 0) { fprintf(log, "calling list_tables\n"); result = 0; rc = sqlite3_exec(db, MASTER_QUERY, list_tables, 0, &zErrMsg); if (rc != SQLITE_OK) { send_error(zErrMsg); sqlite3_free(zErrMsg); } else if (result != 0) { send_result(); } else { // not an error and no results. still need to return something send_ok(); } fflush(log); } else if (strncmp((const char *)ERL_ATOM_PTR(fnp), "sql_exec", 8) == 0) { fprintf(log, "calling sqlite3_exec %s\n", erl_iolist_to_string(argp)); result = 0; rc = sqlite3_exec(db, erl_iolist_to_string(argp), callback, 0, &zErrMsg); if (rc != SQLITE_OK) { send_error(zErrMsg); sqlite3_free(zErrMsg); } else if (result != 0) { send_result(); } else { // not an error and no results. still need to return something send_ok(); } fflush(log); } erl_free_compound(tuplep); erl_free_term(fnp); erl_free_term(argp); } fprintf(log, "******end log******\n"); fclose(log); return 0; }
int main(int argc, char **argv) #endif { struct hostent *hp; MyTimeval start, stop; int i, fd, ires, tries; CORBA_Environment *env; char *this_node_name = NULL; char *peer_node = NULL; char *cookie = NULL; char host[HOSTNAMESZ + 1]; char this_node[NODENAMESZ + 1]; erlang_msg msg; int status, loop; #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 0); if (WSAStartup(wVersionRequested, &wsaData) != 0) { fprintf(stderr, "Could not load winsock2 v2.0 compatible DLL"); exit(1); } #endif progname = argv[0]; host[HOSTNAMESZ] = '\0'; if (gethostname(host, HOSTNAMESZ) < 0) { fprintf(stderr, "Can't find own hostname\n"); done(1); } if ((hp = gethostbyname(host)) == 0) { fprintf(stderr, "Can't get ip address for host %s\n", host); done(1); } for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-help") == 0) { usage(); done(0); } else if (strcmp(argv[i], "-this-node-name") == 0) { i++; this_node_name = argv[i]; } else if (strcmp(argv[i], "-peer-node") == 0) { i++; peer_node = argv[i]; } else if (strcmp(argv[i], "-cookie") == 0) { i++; cookie = argv[i]; } else { fprintf(stderr, "Error : invalid argument \"%s\"\n", argv[i]); usage(); done(1); } } if (this_node_name == NULL || peer_node == NULL || cookie == NULL) { fprintf(stderr, "Error: missing option\n"); usage(); done(1); } /* Behead hostname at first dot */ for (i=0; host[i] != '\0'; i++) { if (host[i] == '.') { host[i] = '\0'; break; } } sprintf(this_node, "%s@%s", this_node_name, host); fprintf(stderr, "c_server: this node: \"%s\"\n", this_node); fprintf(stderr, "c_server: peer node: \"%s\"\n", peer_node); /* initialize erl_interface */ erl_init(NULL, 0); for (tries = 0; tries < MAXTRIES; tries++) { /* connect to peer node */ ires = erl_connect_xinit(host, this_node_name, this_node, (struct in_addr *)*hp->h_addr_list, cookie, 0); fprintf(stderr, "c_server: erl_connect_xinit(): %d\n", ires); fd = erl_connect(peer_node); fprintf(stderr, "c_server: erl_connect(): %d\n", fd); if (fd >= 0) break; fprintf(stderr, "c_server: cannot connect, retrying\n"); } if (fd < 0) { fprintf(stderr, "c_server: cannot connect, exiting\n"); done(1); } env = CORBA_Environment_alloc(INBUFSZ, OUTBUFSZ); env->_fd = fd; status = 1; loop = 1; my_gettimeofday(&start); while (status >= 0 && loop > 0) { status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, &msg, &env->_iin); switch(status) { case ERL_SEND: case ERL_REG_SEND: /* get result */ m_i__switch(NULL, env); switch(env->_major) { case CORBA_NO_EXCEPTION: break; case CORBA_SYSTEM_EXCEPTION: fprintf(stderr, "Request failure, reason : %s\n", (char *) CORBA_exception_value(env)); CORBA_exception_free(env); break; default: /* Should not happen */ CORBA_exception_free(env); break; } /* send back result data */ if (env->_iout > 0) ei_send_encoded(env->_fd, &env->_caller, env->_outbuf, env->_iout); loop = 0; break; case ERL_TICK: break; default: if (status < 0) { fprintf(stderr, "Status negative: %d\n", status); loop = 0; } break; } } my_gettimeofday(&stop); showtime(&start, &stop); erl_close_connection(fd); CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); if (status < 0) done(-status); else done(0); }
int main(int argc, char **argv) #endif { struct hostent *hp; erlang_pid pid; MyTimeval start, stop; int i, fd, ires, tres; IC_Env *env; int tries = 0; char *this_node_name = NULL; char *peer_node = NULL; char *peer_process_name = NULL; char *cookie = NULL; char host[HOSTNAMESZ + 1]; TestFunc test_func = NULL; TestCase *test_case; char *test_case_name = NULL; #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 0); if (WSAStartup(wVersionRequested, &wsaData) != 0) { fprintf(stderr, "Could not load winsock2 v2.0 compatible DLL"); exit(1); } #endif progname = argv[0]; host[HOSTNAMESZ] = '\0'; if (gethostname(host, HOSTNAMESZ) < 0) { fprintf(stderr, "Can't find own hostname\n"); done(1); } if ((hp = gethostbyname(host)) == 0) { fprintf(stderr, "Can't get ip address for host %s\n", host); done(1); } for (i = 1; i < argc; i++) { if (cmp_str(argv[i], "-help")) { usage(); done(0); } else if (cmp_str(argv[i], "-this-node-name")) { i++; this_node_name = argv[i]; } else if (cmp_str(argv[i], "-peer-node")) { i++; peer_node = argv[i]; } else if (cmp_str(argv[i], "-peer-process-name")) { i++; peer_process_name = argv[i]; } else if (cmp_str(argv[i], "-cookie")) { i++; cookie = argv[i]; } else if (cmp_str(argv[i], "-test-case")) { i++; test_case_name = argv[i]; } else { fprintf(stderr, "Error : invalid argument \"%s\"\n", argv[i]); usage(); done(1); } } if (this_node_name == NULL || peer_node == NULL || test_case_name == NULL || peer_process_name == NULL || cookie == NULL) { fprintf(stderr, "Error: missing option\n"); usage(); done(1); } test_case = test_cases; while (test_case->func) { if (cmp_str(test_case->name, test_case_name)) { test_func = test_case->func; break; } test_case++; } if (test_func == NULL) { fprintf(stderr, "Error: illegal test case: \"%s\"\n", test_case_name); done(1); } /* Behead hostname at first dot */ for (i=0; host[i] != '\0'; i++) { if (host[i] == '.') { host[i] = '\0'; break; } } sprintf(this_node, "%s@%s", this_node_name, host); fprintf(stderr, "c_client: this node: \"%s\"\n", this_node); fprintf(stderr, "c_client: peer node: \"%s\"\n", peer_node); fprintf(stderr, "c_client: test case: \"%s\"\n", test_case_name); fprintf(stderr, "c_client: starting\n"); /* initialize erl_interface */ erl_init(NULL, 0); for (tries = 0; tries < MAXTRIES; tries++) { /* connect to erlang node */ ires = erl_connect_xinit(host, this_node_name, this_node, (struct in_addr *)*hp->h_addr_list, cookie, 0); fprintf(stderr, "c_client: erl_connect_xinit(): %d\n", ires); fd = erl_connect(peer_node); fprintf(stderr, "c_client: erl_connect(): %d\n", fd); if (fd >= 0) break; fprintf(stderr, "c_client: cannot connect, retrying\n"); } if (fd < 0) { fprintf(stderr, "c_client: cannot connect, exiting\n"); done(1); } env = CORBA_Environment_alloc(INBUFSZ, OUTBUFSZ); env->_fd = fd; strcpy(env->_regname, peer_process_name); env->_to_pid = NULL; env->_from_pid = &pid; strcpy(pid.node, this_node); pid.num = fd; pid.serial = 0; pid.creation = 0; my_gettimeofday(&start); tres = test_func(env); /* Call test case */ my_gettimeofday(&stop); showtime(&start, &stop); erl_close_connection(fd); printf("c_client: env->_inbuf before : %d\n", INBUFSZ); printf("c_client: env->_outbuf before : %d\n", OUTBUFSZ); printf("c_client: env->_inbuf after : %d\n", env->_inbufsz); printf("c_client: env->_outbuf after : %d\n", env->_outbufsz); CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); done(tres); }
int main(int argc, char **argv) { /* int fd; /\* fd to Erlang node *\/ */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *msg_type, *fromp, *refp, *tuplep, *fnp, *arg1p, *arg2p, *resp; char hostname[255]; char erlang_nodename[255] = "e1@"; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); /* now we figure out where to connect to */ get_hostname(hostname); strcat(erlang_nodename, hostname); strstrip(erlang_nodename); if ((fd_erlang_node = erl_connect(erlang_nodename)) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to %s\n\r",erlang_nodename); while (loop) { got = erl_receive_msg(fd_erlang_node, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { msg_type = erl_element(1, emsg.msg); fromp = erl_element(2, emsg.msg); refp = erl_element(3, emsg.msg); tuplep = erl_element(4, emsg.msg); fnp = erl_element(1, tuplep); if (strncmp(ERL_ATOM_PTR(msg_type), "call", 4) == 0) { /* call expects a msg back */ /* always at least one argument so we get that out first*/ arg1p = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "init", 4) == 0) { printf("init requested\n"); arg2p = erl_element(3, tuplep); resp = gpio_init(arg1p, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "write", 5) == 0) { arg2p = erl_element(3, tuplep); /* @todo implement the real impl here */ resp = gpio_write(arg1p, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "read", 4) == 0) { resp = gpio_read(arg1p); } else if (strncmp(ERL_ATOM_PTR(fnp), "set_int", 7) == 0) { arg2p = erl_element(3, tuplep); /* @todo implement the real impl here */ resp = gpio_set_int(arg1p, fromp, handle_gpio_interrupt, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "release", 7) == 0) { resp = gpio_release(arg1p); } printf("going to send resp: %s\n", ERL_ATOM_PTR(resp)); erl_send(fd_erlang_node, fromp, erl_format("{~w,~w}", refp, resp)); } else if (strncmp(ERL_ATOM_PTR(msg_type), "cast", 4) == 0) { /* cast does not expect a msg back */ } erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(refp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(arg1p); erl_free_term(arg2p); erl_free_term(resp); } } } }
int main(int argc, char **argv) { libusb_context *ctx = NULL; dongleHandle dongle; int option; int address; int bus; ETERM *tuplep; ETERM *functionp; unsigned char buf[1024]; const char* func_name; address = -1; bus = -1; while ((option = getopt(argc, argv, "p:b:")) != -1) { switch (option) { case 'p': address = atoi(optarg); break; case 'b': bus = atoi(optarg); break; default: return 1; } } if (bus == -1 || address == -1) { return 1; } erl_init(NULL, 0); initDongle(ctx); dongle = getDongle(ctx, address, bus); if (dongle == NULL) { ERL_WRITE_ERROR("not_found"); exitDongle(ctx); return 1; } while (read_cmd(buf) > 0) { tuplep = erl_decode(buf); functionp = erl_element(1, tuplep); func_name = (const char*)ERL_ATOM_PTR(functionp); if (strncmp(func_name, "derive", 6) == 0){ hsm_derive(dongle, tuplep); } else if (strncmp(func_name, "import", 6) == 0){ hsm_import(dongle, tuplep); } else if (strncmp(func_name, "pin", 3) == 0){ hsm_pin(dongle, tuplep); } else if (strncmp(func_name, "pubkey", 6) == 0){ hsm_pubkey(dongle, tuplep); } else if (strncmp(func_name, "random", 6) == 0){ hsm_random(dongle, tuplep); } else if (strncmp(func_name, "sign", 4) == 0){ hsm_sign(dongle, tuplep); } else if (strncmp(func_name, "verify", 6) == 0){ hsm_verify(dongle, tuplep); } else if (strncmp(func_name, "close", 5) == 0){ break; } else { ERL_WRITE_ERROR("undef") } erl_free_compound(tuplep); erl_free_term(functionp); } closeDongle(dongle); exitDongle(ctx); ETERM *atom; atom = erl_mk_atom("closed"); unsigned char closed_buf[erl_term_len(atom)]; erl_encode(atom, closed_buf); write_cmd(closed_buf, erl_term_len(atom)); erl_free_term(atom); fprintf(stderr, "CLOSED"); return 0; }
/** ** rockse <path_to_tbl> <path_to_log> **/ int main(int argc, char **argv) { rockse * ptr; ETERM *from_erl; ETERM *cmd; ETERM *key; ETERM *value; char *ret; ETERM *to_send; byte buf[1024]; int length; log = fopen(argv[2], "a+"); erl_init(NULL, 0); fprintf(log, "*** rockse opening table %s\n", argv[1]); ptr = open(argv[1]); while (read_cmd(buf) > 0) { from_erl = erl_decode(buf); fprintf(log, " rockse command type %d\n", ERL_TYPE(from_erl)); if (match_close(from_erl)) { send_bok(); fprintf(log, " rockse command +close+ sent\n"); break; } if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 3) { /* can only be a {put, key, val} */ cmd = ERL_TUPLE_ELEMENT(from_erl, 0); if (match_put(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); value = ERL_TUPLE_ELEMENT(from_erl, 2); fprintf(log, " rockse command +put+ %s %s sent\n", erl_iolist_to_string(key), erl_iolist_to_string(value)); put(ptr, erl_iolist_to_string(key), erl_iolist_to_string(value)); send_put_ok(); } erl_free_term(from_erl); erl_free_term(cmd); erl_free_term(key); erl_free_term(value); } if (ERL_IS_TUPLE(from_erl) && ERL_TUPLE_SIZE(from_erl) == 2) { /* can only be a {get, key} or {delete, key} */ cmd = ERL_TUPLE_ELEMENT(from_erl, 0); if (match_get(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); fprintf(log, " rockse command +get+ %s sent\n", erl_iolist_to_string(key)); ret = get(ptr, erl_iolist_to_string(key)); if (ret == NULL) { send_nok(); } else { length = strlen(ret); fprintf(log, " rockse command +get+ found %s of size %d\n", ret, length); to_send = erl_format("{ok, get, ~s}", ret); send_get_result(to_send); free(ret); } } else if (match_delete(cmd)) { key = ERL_TUPLE_ELEMENT(from_erl, 1); fprintf(log, " rockse command +delete+ %s sent\n", erl_iolist_to_string(key)); delete(ptr, erl_iolist_to_string(key)); send_delete_ok(); } erl_free_term(from_erl); erl_free_term(cmd); erl_free_term(key); } fflush(log); } close_and_destroy(ptr); fprintf(log, "*** rockse closing table %s\n", argv[1]); erl_free_term(from_erl); fclose(log); return 0; }
int main(void) #endif { ei_x_buff eix; int index = 0; ETERM **etermpp = NULL, *etermp = NULL; char *charp = NULL; unsigned char uchar, **ucharpp = NULL, *ucharp = NULL; void *voidp = NULL; Erl_Heap *erl_heapp = NULL; int intx = 0; int *intp = NULL; unsigned int uintx, *uintp; unsigned long *ulongp = NULL; long longx = 0; double doublex = 0.0; short shortx = 42; FILE *filep = NULL; Erl_IpAddr erl_ipaddr = NULL; ErlMessage *erlmessagep = NULL; ErlConnect *erlconnectp = NULL; struct hostent *hostp = NULL; struct in_addr *inaddrp = NULL; /* Converion to erl_interface format is in liberl_interface */ intx = erl_errno; ei_encode_term(charp, &index, voidp); ei_x_encode_term(&eix, voidp); ei_decode_term(charp, &index, voidp); erl_init(voidp, longx); erl_connect_init(intx, charp,shortx); erl_connect_xinit(charp,charp,charp,erl_ipaddr,charp,shortx); erl_connect(charp); erl_xconnect(erl_ipaddr,charp); erl_close_connection(intx); erl_receive(intx, ucharp, intx); erl_receive_msg(intx, ucharp, intx, erlmessagep); erl_xreceive_msg(intx, ucharpp, intp, erlmessagep); erl_send(intx, etermp, etermp); erl_reg_send(intx, charp, etermp); erl_rpc(intx,charp,charp,etermp); erl_rpc_to(intx,charp,charp,etermp); erl_rpc_from(intx,intx,erlmessagep); erl_publish(intx); erl_accept(intx,erlconnectp); erl_thiscookie(); erl_thisnodename(); erl_thishostname(); erl_thisalivename(); erl_thiscreation(); erl_unpublish(charp); erl_err_msg(charp); erl_err_quit(charp); erl_err_ret(charp); erl_err_sys(charp); erl_cons(etermp,etermp); erl_copy_term(etermp); erl_element(intx,etermp); erl_hd(etermp); erl_iolist_to_binary(etermp); erl_iolist_to_string(etermp); erl_iolist_length(etermp); erl_length(etermp); erl_mk_atom(charp); erl_mk_binary(charp,intx); erl_mk_empty_list(); erl_mk_estring(charp, intx); erl_mk_float(doublex); erl_mk_int(intx); erl_mk_list(etermpp,intx); erl_mk_pid(charp,uintx,uintx,uchar); erl_mk_port(charp,uintx,uchar); erl_mk_ref(charp,uintx,uchar); erl_mk_long_ref(charp,uintx,uintx,uintx,uchar); erl_mk_string(charp); erl_mk_tuple(etermpp,intx); erl_mk_uint(uintx); erl_mk_var(charp); erl_print_term(filep,etermp); /* erl_sprint_term(charp,etermp); */ erl_size(etermp); erl_tl(etermp); erl_var_content(etermp, charp); erl_format(charp); erl_match(etermp, etermp); erl_global_names(intx, intp); erl_global_register(intx, charp, etermp); erl_global_unregister(intx, charp); erl_global_whereis(intx, charp, charp); erl_init_malloc(erl_heapp,longx); erl_alloc_eterm(uchar); erl_eterm_release(); erl_eterm_statistics(ulongp,ulongp); erl_free_array(etermpp,intx); erl_free_term(etermp); erl_free_compound(etermp); erl_malloc(longx); erl_free(voidp); erl_compare_ext(ucharp, ucharp); erl_decode(ucharp); erl_decode_buf(ucharpp); erl_encode(etermp,ucharp); erl_encode_buf(etermp,ucharpp); erl_ext_size(ucharp); erl_ext_type(ucharp); erl_peek_ext(ucharp,intx); erl_term_len(etermp); erl_gethostbyname(charp); erl_gethostbyaddr(charp, intx, intx); erl_gethostbyname_r(charp, hostp, charp, intx, intp); erl_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); erl_init_resolve(); erl_distversion(intx); erl_epmd_connect(inaddrp); erl_epmd_port(inaddrp, charp, intp); charp = ERL_ATOM_PTR(etermp); intx = ERL_ATOM_SIZE(etermp); ucharp = ERL_BIN_PTR(etermp); intx = ERL_BIN_SIZE(etermp); etermp = ERL_CONS_HEAD(etermp); etermp = ERL_CONS_TAIL(etermp); intx = ERL_COUNT(etermp); doublex= ERL_FLOAT_VALUE(etermp); uintx = ERL_INT_UVALUE(etermp); intx = ERL_INT_VALUE(etermp); intx = ERL_IS_ATOM(etermp); intx = ERL_IS_BINARY(etermp); intx = ERL_IS_CONS(etermp); intx = ERL_IS_EMPTY_LIST(etermp); intx = ERL_IS_FLOAT(etermp); intx = ERL_IS_INTEGER(etermp); intx = ERL_IS_LIST(etermp); intx = ERL_IS_PID(etermp); intx = ERL_IS_PORT(etermp); intx = ERL_IS_REF(etermp); intx = ERL_IS_TUPLE(etermp); intx = ERL_IS_UNSIGNED_INTEGER(etermp); uchar = ERL_PID_CREATION(etermp); charp = ERL_PID_NODE(etermp); uintx = ERL_PID_NUMBER(etermp); uintx = ERL_PID_SERIAL(etermp); uchar = ERL_PORT_CREATION(etermp); charp = ERL_PORT_NODE(etermp); uintx = ERL_PORT_NUMBER(etermp); uchar = ERL_REF_CREATION(etermp); intx = ERL_REF_LEN(etermp); charp = ERL_REF_NODE(etermp); uintx = ERL_REF_NUMBER(etermp); uintp = ERL_REF_NUMBERS(etermp); etermp = ERL_TUPLE_ELEMENT(etermp,intx); intx = ERL_TUPLE_SIZE(etermp); return BUFSIZ + EAGAIN + EHOSTUNREACH + EINVAL + EIO + EMSGSIZE + ENOMEM + ERL_ATOM + ERL_BINARY + ERL_ERROR + ERL_EXIT + ERL_FLOAT + ERL_INTEGER + ERL_LINK + ERL_LIST + ERL_MSG + ERL_NO_TIMEOUT + ERL_PID + ERL_PORT + ERL_REF + ERL_REG_SEND + ERL_SEND + ERL_SMALL_BIG + ERL_TICK + ERL_TIMEOUT + ERL_TUPLE + ERL_UNLINK + ERL_U_INTEGER + ERL_U_SMALL_BIG + ERL_VARIABLE + ETIMEDOUT + MAXNODELEN + MAXREGLEN; }
void erl_start(int argc, char **argv) { int i = 1; char* arg=NULL; char* Parg = NULL; int have_break_handler = 1; char envbuf[21]; /* enough for any 64-bit integer */ size_t envbufsz; int ncpu = early_init(&argc, argv); envbufsz = sizeof(envbuf); if (erts_sys_getenv(ERL_MAX_ETS_TABLES_ENV, envbuf, &envbufsz) == 0) user_requested_db_max_tabs = atoi(envbuf); else user_requested_db_max_tabs = 0; envbufsz = sizeof(envbuf); if (erts_sys_getenv("ERL_FULLSWEEP_AFTER", envbuf, &envbufsz) == 0) { Uint16 max_gen_gcs = atoi(envbuf); erts_smp_atomic32_set_nob(&erts_max_gen_gcs, (erts_aint32_t) max_gen_gcs); } #if (defined(__APPLE__) && defined(__MACH__)) || defined(__DARWIN__) /* * The default stack size on MacOS X is too small for pcre. */ erts_sched_thread_suggested_stack_size = 256; #endif #ifdef DEBUG verbose = DEBUG_DEFAULT; #endif erts_error_logger_warnings = am_error; while (i < argc) { if (argv[i][0] != '-') { erts_usage(); } if (strcmp(argv[i], "--") == 0) { /* end of emulator options */ i++; break; } switch (argv[i][1]) { /* * NOTE: -M flags are handled (and removed from argv) by * erts_alloc_init(). * * The -d, -m, -S, -t, and -T flags was removed in * Erlang 5.3/OTP R9C. * * -S, and -T has been reused in Erlang 5.5/OTP R11B. * * -d has been reused in a patch R12B-4. */ case '#' : arg = get_arg(argv[i]+2, argv[i+1], &i); if ((display_items = atoi(arg)) == 0) { erts_fprintf(stderr, "bad display items%s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("using display items %d\n",display_items)); break; case 'f': if (!strncmp(argv[i],"-fn",3)) { arg = get_arg(argv[i]+3, argv[i+1], &i); switch (*arg) { case 'u': erts_set_user_requested_filename_encoding(ERL_FILENAME_UTF8); break; case 'l': erts_set_user_requested_filename_encoding(ERL_FILENAME_LATIN1); break; case 'a': erts_set_user_requested_filename_encoding(ERL_FILENAME_UNKNOWN); default: erts_fprintf(stderr, "bad filename encoding %s, can be (l,u or a)\n", arg); erts_usage(); } break; } else { erts_fprintf(stderr, "%s unknown flag %s\n", argv[0], argv[i]); erts_usage(); } case 'L': erts_no_line_info = 1; break; case 'v': #ifdef DEBUG if (argv[i][2] == '\0') { verbose |= DEBUG_SYSTEM; } else { char *ch; for (ch = argv[i]+2; *ch != '\0'; ch++) { switch (*ch) { case 's': verbose |= DEBUG_SYSTEM; break; case 'g': verbose |= DEBUG_PRIVATE_GC; break; case 'M': verbose |= DEBUG_MEMORY; break; case 'a': verbose |= DEBUG_ALLOCATION; break; case 't': verbose |= DEBUG_THREADS; break; case 'p': verbose |= DEBUG_PROCESSES; break; case 'm': verbose |= DEBUG_MESSAGES; break; default : erts_fprintf(stderr,"Unknown verbose option: %c\n",*ch); } } } erts_printf("Verbose level: "); if (verbose & DEBUG_SYSTEM) erts_printf("SYSTEM "); if (verbose & DEBUG_PRIVATE_GC) erts_printf("PRIVATE_GC "); if (verbose & DEBUG_MEMORY) erts_printf("PARANOID_MEMORY "); if (verbose & DEBUG_ALLOCATION) erts_printf("ALLOCATION "); if (verbose & DEBUG_THREADS) erts_printf("THREADS "); if (verbose & DEBUG_PROCESSES) erts_printf("PROCESSES "); if (verbose & DEBUG_MESSAGES) erts_printf("MESSAGES "); erts_printf("\n"); #else erts_fprintf(stderr, "warning: -v (only in debug compiled code)\n"); #endif break; case 'V' : { char tmp[256]; tmp[0] = tmp[1] = '\0'; #ifdef DEBUG strcat(tmp, ",DEBUG"); #endif #ifdef ERTS_SMP strcat(tmp, ",SMP"); #endif #ifdef USE_THREADS strcat(tmp, ",ASYNC_THREADS"); #endif #ifdef HIPE strcat(tmp, ",HIPE"); #endif erts_fprintf(stderr, "Erlang "); if (tmp[1]) { erts_fprintf(stderr, "(%s) ", tmp+1); } erts_fprintf(stderr, "(" EMULATOR ") emulator version " ERLANG_VERSION "\n"); erl_exit(0, ""); } break; case 'H': /* undocumented */ fprintf(stderr, "The undocumented +H option has been removed (R10B-6).\n\n"); break; case 'h': { char *sub_param = argv[i]+2; /* set default heap size * * h|ms - min_heap_size * h|mbs - min_bin_vheap_size * */ if (has_prefix("mbs", sub_param)) { arg = get_arg(sub_param+3, argv[i+1], &i); if ((BIN_VH_MIN_SIZE = atoi(arg)) <= 0) { erts_fprintf(stderr, "bad heap size %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("using minimum binary virtual heap size %d\n", BIN_VH_MIN_SIZE)); } else if (has_prefix("ms", sub_param)) { arg = get_arg(sub_param+2, argv[i+1], &i); if ((H_MIN_SIZE = atoi(arg)) <= 0) { erts_fprintf(stderr, "bad heap size %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("using minimum heap size %d\n", H_MIN_SIZE)); } else { /* backward compatibility */ arg = get_arg(argv[i]+2, argv[i+1], &i); if ((H_MIN_SIZE = atoi(arg)) <= 0) { erts_fprintf(stderr, "bad heap size %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("using minimum heap size %d\n", H_MIN_SIZE)); } break; } case 'd': /* * Never produce crash dumps for internally detected * errors; only produce a core dump. (Generation of * crash dumps is destructive and makes it impossible * to inspect the contents of process heaps in the * core dump.) */ erts_no_crash_dump = 1; break; case 'e': if (sys_strcmp("c", argv[i]+2) == 0) { erts_ets_always_compress = 1; } else { /* set maximum number of ets tables */ arg = get_arg(argv[i]+2, argv[i+1], &i); if (( user_requested_db_max_tabs = atoi(arg) ) < 0) { erts_fprintf(stderr, "bad maximum number of ets tables %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("using maximum number of ets tables %d\n", user_requested_db_max_tabs)); } break; case 'i': /* define name of module for initial function */ init = get_arg(argv[i]+2, argv[i+1], &i); break; case 'b': /* define name of initial function */ boot = get_arg(argv[i]+2, argv[i+1], &i); break; case 'B': if (argv[i][2] == 'i') /* +Bi */ ignore_break = 1; else if (argv[i][2] == 'c') /* +Bc */ replace_intr = 1; else if (argv[i][2] == 'd') /* +Bd */ have_break_handler = 0; else if (argv[i+1][0] == 'i') { /* +B i */ get_arg(argv[i]+2, argv[i+1], &i); ignore_break = 1; } else if (argv[i+1][0] == 'c') { /* +B c */ get_arg(argv[i]+2, argv[i+1], &i); replace_intr = 1; } else if (argv[i+1][0] == 'd') { /* +B d */ get_arg(argv[i]+2, argv[i+1], &i); have_break_handler = 0; } else /* +B */ have_break_handler = 0; break; case 'K': /* If kernel poll support is present, erl_sys_args() will remove the K parameter and value */ get_arg(argv[i]+2, argv[i+1], &i); erts_fprintf(stderr, "kernel-poll not supported; \"K\" parameter ignored\n", arg); break; case 'P': /* set maximum number of processes */ Parg = get_arg(argv[i]+2, argv[i+1], &i); erts_proc.max = atoi(Parg); /* Check of result is delayed until later. This is because +R may be given after +P. */ break; case 'S' : /* Was handled in early_init() just read past it */ (void) get_arg(argv[i]+2, argv[i+1], &i); break; case 's' : { char *estr; int res; char *sub_param = argv[i]+2; if (has_prefix("bt", sub_param)) { arg = get_arg(sub_param+2, argv[i+1], &i); res = erts_init_scheduler_bind_type_string(arg); if (res != ERTS_INIT_SCHED_BIND_TYPE_SUCCESS) { switch (res) { case ERTS_INIT_SCHED_BIND_TYPE_NOT_SUPPORTED: estr = "not supported"; break; case ERTS_INIT_SCHED_BIND_TYPE_ERROR_NO_CPU_TOPOLOGY: estr = "no cpu topology available"; break; case ERTS_INIT_SCHED_BIND_TYPE_ERROR_NO_BAD_TYPE: estr = "invalid type"; break; default: estr = "undefined error"; break; } erts_fprintf(stderr, "setting scheduler bind type '%s' failed: %s\n", arg, estr); erts_usage(); } } else if (has_prefix("bwt", sub_param)) { arg = get_arg(sub_param+3, argv[i+1], &i); if (erts_sched_set_busy_wait_threshold(arg) != 0) { erts_fprintf(stderr, "bad scheduler busy wait threshold: %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("scheduler wakup threshold: %s\n", arg)); } else if (has_prefix("cl", sub_param)) { arg = get_arg(sub_param+2, argv[i+1], &i); if (sys_strcmp("true", arg) == 0) erts_sched_compact_load = 1; else if (sys_strcmp("false", arg) == 0) erts_sched_compact_load = 0; else { erts_fprintf(stderr, "bad scheduler compact load value '%s'\n", arg); erts_usage(); } } else if (has_prefix("ct", sub_param)) { arg = get_arg(sub_param+2, argv[i+1], &i); res = erts_init_cpu_topology_string(arg); if (res != ERTS_INIT_CPU_TOPOLOGY_OK) { switch (res) { case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID: estr = "invalid identifier"; break; case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID_RANGE: estr = "invalid identifier range"; break; case ERTS_INIT_CPU_TOPOLOGY_INVALID_HIERARCHY: estr = "invalid hierarchy"; break; case ERTS_INIT_CPU_TOPOLOGY_INVALID_ID_TYPE: estr = "invalid identifier type"; break; case ERTS_INIT_CPU_TOPOLOGY_INVALID_NODES: estr = "invalid nodes declaration"; break; case ERTS_INIT_CPU_TOPOLOGY_MISSING_LID: estr = "missing logical identifier"; break; case ERTS_INIT_CPU_TOPOLOGY_NOT_UNIQUE_LIDS: estr = "not unique logical identifiers"; break; case ERTS_INIT_CPU_TOPOLOGY_NOT_UNIQUE_ENTITIES: estr = "not unique entities"; break; case ERTS_INIT_CPU_TOPOLOGY_MISSING: estr = "missing cpu topology"; break; default: estr = "undefined error"; break; } erts_fprintf(stderr, "bad cpu topology '%s': %s\n", arg, estr); erts_usage(); } } else if (sys_strcmp("nsp", sub_param) == 0) erts_use_sender_punish = 0; else if (sys_strcmp("wt", sub_param) == 0) { arg = get_arg(sub_param+2, argv[i+1], &i); if (erts_sched_set_wakeup_other_thresold(arg) != 0) { erts_fprintf(stderr, "scheduler wakeup threshold: %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("scheduler wakeup threshold: %s\n", arg)); } else if (sys_strcmp("ws", sub_param) == 0) { arg = get_arg(sub_param+2, argv[i+1], &i); if (erts_sched_set_wakeup_other_type(arg) != 0) { erts_fprintf(stderr, "scheduler wakeup strategy: %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("scheduler wakeup threshold: %s\n", arg)); } else if (has_prefix("ss", sub_param)) { /* suggested stack size (Kilo Words) for scheduler threads */ arg = get_arg(sub_param+2, argv[i+1], &i); erts_sched_thread_suggested_stack_size = atoi(arg); if ((erts_sched_thread_suggested_stack_size < ERTS_SCHED_THREAD_MIN_STACK_SIZE) || (erts_sched_thread_suggested_stack_size > ERTS_SCHED_THREAD_MAX_STACK_SIZE)) { erts_fprintf(stderr, "bad stack size for scheduler threads %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("suggested scheduler thread stack size %d kilo words\n", erts_sched_thread_suggested_stack_size)); } else { erts_fprintf(stderr, "bad scheduling option %s\n", argv[i]); erts_usage(); } break; } case 't': /* set atom table size */ arg = get_arg(argv[i]+2, argv[i+1], &i); errno = 0; erts_atom_table_size = strtol(arg, NULL, 10); if (errno != 0 || erts_atom_table_size < MIN_ATOM_TABLE_SIZE || erts_atom_table_size > MAX_ATOM_TABLE_SIZE) { erts_fprintf(stderr, "bad atom table size %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("setting maximum number of atoms to %d\n", erts_atom_table_size)); break; case 'T' : arg = get_arg(argv[i]+2, argv[i+1], &i); errno = 0; erts_modified_timing_level = atoi(arg); if ((erts_modified_timing_level == 0 && errno != 0) || erts_modified_timing_level < 0 || erts_modified_timing_level >= ERTS_MODIFIED_TIMING_LEVELS) { erts_fprintf(stderr, "bad modified timing level %s\n", arg); erts_usage(); } else { VERBOSE(DEBUG_SYSTEM, ("using modified timing level %d\n", erts_modified_timing_level)); } break; case 'R': { /* set compatibility release */ arg = get_arg(argv[i]+2, argv[i+1], &i); erts_compat_rel = atoi(arg); if (erts_compat_rel < ERTS_MIN_COMPAT_REL || erts_compat_rel > this_rel_num()) { erts_fprintf(stderr, "bad compatibility release number %s\n", arg); erts_usage(); } ASSERT(ERTS_MIN_COMPAT_REL >= 7); switch (erts_compat_rel) { case 7: case 8: case 9: erts_use_r9_pids_ports = 1; default: break; } break; } case 'A': /* Was handled in early init just read past it */ (void) get_arg(argv[i]+2, argv[i+1], &i); break; case 'a': /* suggested stack size (Kilo Words) for threads in thread pool */ arg = get_arg(argv[i]+2, argv[i+1], &i); erts_async_thread_suggested_stack_size = atoi(arg); if ((erts_async_thread_suggested_stack_size < ERTS_ASYNC_THREAD_MIN_STACK_SIZE) || (erts_async_thread_suggested_stack_size > ERTS_ASYNC_THREAD_MAX_STACK_SIZE)) { erts_fprintf(stderr, "bad stack size for async threads %s\n", arg); erts_usage(); } VERBOSE(DEBUG_SYSTEM, ("suggested async-thread stack size %d kilo words\n", erts_async_thread_suggested_stack_size)); break; case 'r': { char *sub_param = argv[i]+2; if (has_prefix("g", sub_param)) { get_arg(sub_param+1, argv[i+1], &i); /* already handled */ } else { erts_ets_realloc_always_moves = 1; } break; } case 'n': /* XXX obsolete */ break; case 'c': if (argv[i][2] == 0) { /* -c: documented option */ erts_disable_tolerant_timeofday = 1; } #ifdef ERTS_OPCODE_COUNTER_SUPPORT else if (argv[i][2] == 'i') { /* -ci: undcoumented option*/ count_instructions = 1; } #endif break; case 'W': arg = get_arg(argv[i]+2, argv[i+1], &i); switch (arg[0]) { case 'i': erts_error_logger_warnings = am_info; break; case 'w': erts_error_logger_warnings = am_warning; break; case 'e': /* The default */ erts_error_logger_warnings = am_error; default: erts_fprintf(stderr, "unrecognized warning_map option %s\n", arg); erts_usage(); } break; case 'z': { char *sub_param = argv[i]+2; int new_limit; if (has_prefix("dbbl", sub_param)) { arg = get_arg(sub_param+4, argv[i+1], &i); new_limit = atoi(arg); if (new_limit < 1 || INT_MAX/1024 < new_limit) { erts_fprintf(stderr, "Invalid dbbl limit: %d\n", new_limit); erts_usage(); } else { erts_dist_buf_busy_limit = new_limit*1024; } } else { erts_fprintf(stderr, "bad -z option %s\n", argv[i]); erts_usage(); } break; } default: erts_fprintf(stderr, "%s unknown flag %s\n", argv[0], argv[i]); erts_usage(); } i++; } /* Delayed check of +P flag */ if (erts_proc.max < ERTS_MIN_PROCESSES || erts_proc.max > ERTS_MAX_PROCESSES || (erts_use_r9_pids_ports && erts_proc.max > ERTS_MAX_R9_PROCESSES)) { erts_fprintf(stderr, "bad number of processes %s\n", Parg); erts_usage(); } /* Restart will not reinstall the break handler */ #ifdef __WIN32__ if (ignore_break) erts_set_ignore_break(); else if (replace_intr) erts_replace_intr(); else init_break_handler(); #else if (ignore_break) erts_set_ignore_break(); else if (have_break_handler) init_break_handler(); if (replace_intr) erts_replace_intr(); #endif boot_argc = argc - i; /* Number of arguments to init */ boot_argv = &argv[i]; erl_init(ncpu); load_preloaded(); erts_end_staging_code_ix(); erts_commit_staging_code_ix(); erts_initialized = 1; erl_first_process_otp("otp_ring0", NULL, 0, boot_argc, boot_argv); #ifdef ERTS_SMP erts_start_schedulers(); /* Let system specific code decide what to do with the main thread... */ erts_sys_main_thread(); /* May or may not return! */ #else erts_thr_set_main_status(1, 1); #if ERTS_USE_ASYNC_READY_Q erts_get_scheduler_data()->aux_work_data.async_ready.queue = erts_get_async_ready_queue(1); #endif set_main_stack_size(); process_main(); #endif }
int main(int argc, char **argv) { int port; // Listen port number int fd; // Socket descriptor int listen; // Listen socket ErlConnect conn; // Connection data char *cookie; // Erlang magic cookie pid_t pid; int x; pthread_t thread; pthread_attr_t thread_attrs; thread_data_t *data; // Per thread data: thread ID, // connection node name static unsigned int tidx = 0; pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); if (setsid() < 0) exit(EXIT_FAILURE); umask(0); chdir("/"); signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_DFL); if (argc == 3) { port = atoi(argv[1]); cookie = argv[2]; } else { fprintf(stderr, "Usage: %s <port number> <erlang secret cookie>\n\r",argv[0]); exit(EXIT_FAILURE); } for (x = sysconf(_SC_OPEN_MAX); x>0; x--) close (x); setlogmask (LOG_UPTO (LOG_NOTICE)); openlog("nfq_node", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); erl_init(NULL, 0); if (erl_connect_init(1, cookie, 0) == -1) erl_err_quit("erl_connect_init"); if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if (pthread_attr_init(&thread_attrs)) { syslog(LOG_ERR, "error while init pthread attr struct\n\r"); exit(EXIT_FAILURE); } if ((pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED))) { syslog(LOG_ERR, "error while set pthread attributes\n\r"); exit(EXIT_FAILURE); } syslog(LOG_NOTICE,"Waiting for connections...\n\r"); for(;;) { while((fd = erl_accept(listen, &conn)) == ERL_ERROR) syslog(LOG_ERR,"%s Connection error\n\r", argv[0]); if((data = (thread_data_t *)malloc(sizeof(thread_data_t))) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } if((data->node = (char *)malloc(strlen(conn.nodename)+1)) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } data->fd = fd; data->idx = tidx; strcpy(data->node, conn.nodename); if (pthread_create(&thread, &thread_attrs, erl_message_read_loop, data)) { syslog(LOG_ERR, "Thread create failure\n\r"); exit(EXIT_FAILURE); } tidx++; } closelog(); return 0; }