]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Use condition variables to wait for threads to finish initializing.
[meshlink] / src / meshlink.c
index 2c1253c79864d10b0cf567b233ffce4523a88191..20dad9f43caf437d15155931f2d528b3e1a30e42 100644 (file)
@@ -599,7 +599,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
        free(mesh->self->name);
        mesh->name = name;
        mesh->self->name = xstrdup(name);
-       mesh->self->devclass = devclass;
+       mesh->self->devclass = devclass == DEV_CLASS_UNKNOWN ? mesh->devclass : devclass;
 
        // Initialize configuration directory
        if(!config_init(mesh, "current")) {
@@ -744,8 +744,9 @@ static bool recvline(meshlink_handle_t *mesh, size_t len) {
 
        return true;
 }
+
 static bool sendline(int fd, char *format, ...) {
-       static char buffer[4096];
+       char buffer[4096];
        char *p = buffer;
        int blen = 0;
        va_list ap;
@@ -1381,16 +1382,16 @@ static void *meshlink_main_loop(void *arg) {
 #ifdef HAVE_SETNS
 
                if(setns(mesh->netns, CLONE_NEWNET) != 0) {
+                       pthread_cond_signal(&mesh->cond);
                        return NULL;
                }
 
 #else
+                       pthread_cond_signal(&mesh->cond);
                return NULL;
 #endif // HAVE_SETNS
        }
 
-       pthread_mutex_lock(&(mesh->mesh_mutex));
-
 #if HAVE_CATTA
 
        if(mesh->discovery) {
@@ -1399,10 +1400,15 @@ static void *meshlink_main_loop(void *arg) {
 
 #endif
 
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+
        logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
+       pthread_cond_broadcast(&mesh->cond);
        main_loop(mesh);
        logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
 
+       pthread_mutex_unlock(&(mesh->mesh_mutex));
+
 #if HAVE_CATTA
 
        // Stop discovery
@@ -1412,7 +1418,6 @@ static void *meshlink_main_loop(void *arg) {
 
 #endif
 
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
        return NULL;
 }
 
@@ -1429,6 +1434,9 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        pthread_mutex_lock(&(mesh->mesh_mutex));
 
+       assert(mesh->self->ecdsa);
+       assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
+
        if(mesh->threadstarted) {
                logger(mesh, MESHLINK_DEBUG, "thread was already running\n");
                pthread_mutex_unlock(&(mesh->mesh_mutex));
@@ -1468,12 +1476,9 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                return false;
        }
 
+       pthread_cond_wait(&mesh->cond, &mesh->mesh_mutex);
        mesh->threadstarted = true;
 
-       assert(mesh->self->ecdsa);
-       assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
-
-
        pthread_mutex_unlock(&(mesh->mesh_mutex));
        return true;
 }
@@ -3112,7 +3117,7 @@ static void __attribute__((destructor)) meshlink_exit(void) {
 }
 
 /// Device class traits
-dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = {
+const dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = {
        { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE
        { .min_connects = 3, .max_connects = 100, .edge_weight = 3 },   // DEV_CLASS_STATIONARY
        { .min_connects = 3, .max_connects = 3, .edge_weight = 6 },             // DEV_CLASS_PORTABLE