int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
/* call this from the child process to start an erlang system. This * function just builds the erlang command line and then calls it. * * node - the nodename for the new node * flags - various options that can be set (see erl_start.h) * erl - name of the erlang executable, or NULL for default ("erl") * args - additional arguments to pass to erlang executable * port - the port number where we wait for acknowledgment from the enode * * we have a potential problem if args conflicts with any of the * arguments we use here. */ static int exec_erlang(ei_cnode *ec, char *alive, Erl_IpAddr adr, int flags, char *erl, char *args[], int port) { #if !defined(__WIN32__) && !defined(VXWORKS) int fd,len,l,i; char **s; char *argv[4]; char argbuf[BUFSIZ]; struct in_addr myaddr; struct in_addr *hisaddr = (struct in_addr *)adr; get_addr(ei_thishostname(ec), &myaddr); /* on this host? */ /* compare ip addresses, unless forced by flag setting to use rsh */ if ((flags & ERL_START_REMOTE) || (hisaddr->s_addr != myaddr.s_addr)) { argv[0] = RSH; len = strlen(inet_ntoa(*hisaddr)); argv[1] = malloc(len+1); strcpy(argv[1],inet_ntoa(*hisaddr)); } else { /* Yes - use sh to start local Erlang */ argv[0] = "sh"; argv[1] = "-c"; } argv[2] = argbuf; argv[3] = NULL; len = 0; *argbuf=(char)0; sprintf(argbuf,"exec %s ", (erl? erl: "erl")); len = strlen(argbuf); /* *must* be noinput or node (seems to) hang... */ /* long or short names? */ sprintf(&argbuf[len], "-noinput %s %s ", ((flags & ERL_START_LONG) ? "-name" : "-sname"), alive); len = strlen(argbuf); /* now make the new node report back when it's ready */ /* add: myip, myport and replymsg */ sprintf(&argbuf[len], "-s erl_reply reply %s %d %d ", inet_ntoa(myaddr),port,(int)getpid()); #ifdef DEBUG fprintf(stderr,"erl_call: debug %s\n",&argbuf[len]); #endif len = strlen(argbuf); /* additional arguments to be passed to the other system */ /* make sure that they will fit first */ for (l=0, s = args; s && *s; s++) l+= strlen(*s) + 1; if (len + l + 1 > BUFSIZ) return ERL_BADARG; else { for (s = args; s && *s; s++) { strcat(argbuf," "); strcat(argbuf,*s); } len += l + 1; } if (flags & ERL_START_VERBOSE) { fprintf(stderr,"erl_call: %s %s %s\n",argv[0],argv[1],argv[2]); } /* close all descriptors in child */ for (i=0; i<64; i++) close(i); /* debug output to file? */ if (flags & ERL_START_DEBUG) { char debugfile[MAXPATHLEN+1]; char *home=getenv("HOME"); sprintf(debugfile,"%s/%s.%s",home,ERL_START_LOGFILE,alive); if ((fd=open(debugfile, O_WRONLY | O_CREAT | O_APPEND, 0644)) >= 0) { time_t t = time(NULL); dup2(fd,1); dup2(fd,2); fprintf(stderr,"\n\n===== Log started ======\n%s \n",ctime(&t)); fprintf(stderr,"erl_call: %s %s %s\n",argv[0],argv[1],argv[2]); } } /* start the system */ execvp(argv[0], argv); if (flags & ERL_START_DEBUG) { fprintf(stderr,"erl_call: exec failed: (%d) %s %s %s\n", errno,argv[0],argv[1],argv[2]); } #endif /* (hopefully) NOT REACHED */ return ERL_SYS_ERROR; } /* exec_erlang() */
const char *erl_thishostname(void) { return ei_thishostname(&erl_if_ec); }
static unsigned long spawn_erlang_epmd(ei_cnode *ec, char *alive, Erl_IpAddr adr, int flags, char *erl_or_epmd, char *args[], int port, int is_erlang) { #if defined(VXWORKS) FUNCPTR erlfunc; #else /* Windows */ STARTUPINFO sinfo; SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pinfo; #endif char *cmdbuf; int cmdlen; char *ptr; int i; int num_args; char *name_format; struct in_addr myaddr; struct in_addr *hisaddr = (struct in_addr *)adr; char iaddrbuf[IP_ADDR_CHARS + 1]; int ret; if(is_erlang){ get_addr(ei_thishostname(ec), &myaddr); #if defined(VXWORKS) inet_ntoa_b(myaddr, iaddrbuf); #else /* Windows */ if((ptr = inet_ntoa(myaddr)) == NULL) return 0; else strcpy(iaddrbuf,ptr); #endif } if ((flags & ERL_START_REMOTE) || (is_erlang && (hisaddr->s_addr != myaddr.s_addr))) { return 0; } else { num_args = enquote_args(args, &args); for(cmdlen = i = 0; args[i] != NULL; ++i) cmdlen += strlen(args[i]) + 1; #if !defined(VXWORKS) /* On VxWorks, we dont actually run a command, we call start_erl() */ if(!erl_or_epmd) #endif erl_or_epmd = (is_erlang) ? DEF_ERL_COMMAND : DEF_EPMD_COMMAND; if(is_erlang){ name_format = (flags & ERL_START_LONG) ? ERL_NAME_FMT : ERL_SNAME_FMT; cmdlen += strlen(erl_or_epmd) + (*erl_or_epmd != '\0') + strlen(name_format) + 1 + strlen(alive) + strlen(ERL_REPLY_FMT) + 1 + strlen(iaddrbuf) + 2 * FORMATTED_INT_LEN + 1; ptr = cmdbuf = malloc(cmdlen); if(*erl_or_epmd != '\0') ptr += sprintf(ptr,"%s ",erl_or_epmd); ptr += sprintf(ptr, name_format, alive); ptr += sprintf(ptr, " " ERL_REPLY_FMT, iaddrbuf, port, unique_id()); } else { /* epmd */ cmdlen += strlen(erl_or_epmd) + (*erl_or_epmd != '\0') + 1; ptr = cmdbuf = malloc(cmdlen); if(*erl_or_epmd != '\0') ptr += sprintf(ptr,"%s ",erl_or_epmd); else *(ptr++) = '\0'; } for(i= 0; args[i] != NULL; ++i){ *(ptr++) = ' '; strcpy(ptr,args[i]); ptr += strlen(args[i]); } free_args(args); if (flags & ERL_START_VERBOSE) { fprintf(stderr,"erl_call: commands are %s\n",cmdbuf); } /* OK, one single command line... */ #if defined(VXWORKS) erlfunc = lookup_function((is_erlang) ? ERLANG_SYM : EPMD_SYM); if(erlfunc == NULL){ if (flags & ERL_START_VERBOSE) { fprintf(stderr,"erl_call: failed to find symbol %s\n", (is_erlang) ? ERLANG_SYM : EPMD_SYM); } ret = 0; } else { /* Just call it, it spawns itself... */ ret = (unsigned long) (*erlfunc)((int) cmdbuf,0,0,0,0,0,0,0,0,0); if(ret == (unsigned long) ERROR) ret = 0; } #else /* Windows */ /* Hmmm, hidden or unhidden window??? */ memset(&sinfo,0,sizeof(sinfo)); sinfo.cb = sizeof(STARTUPINFO); sinfo.dwFlags = STARTF_USESHOWWINDOW /*| STARTF_USESTDHANDLES*/; sinfo.wShowWindow = SW_HIDE; /* Hidden! */ sinfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); sinfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); sinfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); sa.nLength = sizeof(sa); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = /*TRUE*/ FALSE; if(!CreateProcess( NULL, cmdbuf, &sa, NULL, /*TRUE*/ FALSE, 0 | CREATE_NEW_CONSOLE, NULL, NULL, &sinfo, &pinfo)) ret = 0; else ret = (unsigned long) pinfo.hProcess; #endif free(cmdbuf); return ret; } /* NOTREACHED */ }