114 lines
2.1 KiB
C
114 lines
2.1 KiB
C
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
|
|
*
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <ei.h>
|
|
#include <erl_interface.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
#include <uv.h>
|
|
#include <uv/unix.h>
|
|
#include <uv/bsd.h>
|
|
#include "erluv.h"
|
|
|
|
/* global variables */
|
|
short creation = 0;
|
|
int id = 1;
|
|
char cookie[COOKIE_SIZE];
|
|
char node_name[NODE_NAME_SIZE];
|
|
char node_target[NODE_NAME_SIZE];
|
|
|
|
/* set the node cookie from environment */
|
|
int
|
|
init_cookie() {
|
|
if (getenv("COOKIE") == NULL)
|
|
return 1;
|
|
|
|
/* the last char must be set to 0. */
|
|
strncpy(cookie, getenv("COOKIE"), COOKIE_SIZE-1);
|
|
return 0;
|
|
}
|
|
|
|
/* set the node name from environment
|
|
* todo: ensure the node name is valid
|
|
*/
|
|
int
|
|
init_node_name() {
|
|
|
|
if (getenv("NODE_NAME") == NULL) {
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stdout, "set node_name ");
|
|
/* the last char must be set to 0. */
|
|
strncpy(node_name, getenv("NODE_NAME"), NODE_NAME_SIZE-1);
|
|
fprintf(stdout, "%s\n", node_name);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
init_node_target() {
|
|
|
|
if (getenv("NODE_TARGET") == NULL) {
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stdout, "set node_target");
|
|
/* the last char must be set to 0. */
|
|
strncpy(node_target, getenv("NODE_TARGET"), NODE_NAME_SIZE-1);
|
|
fprintf(stdout, "%s\n", node_target);
|
|
return 0;
|
|
}
|
|
|
|
/* set the node id from environment
|
|
*/
|
|
int
|
|
init_id() {
|
|
int tmpid = 1;
|
|
const char *errstr;
|
|
|
|
if (getenv("ID") == NULL) {
|
|
return 1;
|
|
}
|
|
|
|
tmpid = (int)strtonum(getenv("ID"), 1, 0xffff, &errstr);
|
|
if (errstr != NULL) {
|
|
fprintf(stderr, "id is %s\n", errstr);
|
|
return 1;
|
|
}
|
|
|
|
id = tmpid;
|
|
return 0;
|
|
}
|
|
|
|
/* set the creation time
|
|
*/
|
|
int
|
|
init_creation() {
|
|
fprintf(stdout, "init creation...\n");
|
|
creation = time(NULL)+1;
|
|
return 0;
|
|
}
|
|
|
|
/* sanitize global variables and configure them with
|
|
* environment variable
|
|
*/
|
|
void
|
|
init() {
|
|
bzero(&creation, sizeof(short));
|
|
bzero(&id, sizeof(int));
|
|
bzero(&cookie, sizeof(char)*COOKIE_SIZE);
|
|
bzero(&node_name, sizeof(char)*NODE_NAME_SIZE);
|
|
|
|
init_cookie();
|
|
init_node_name();
|
|
init_node_target();
|
|
init_id();
|
|
init_creation();
|
|
|
|
ei_init();
|
|
}
|