/* Chrysalide - Outil d'analyse de fichiers binaires * cdb.h - prototypes pour la manipulation des archives au format CDB * * Copyright (C) 2014-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see . */ #include "cdb.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "collection.h" #include "protocol.h" #include "../../common/cpp.h" #include "../../common/extstr.h" #include "../../common/io.h" #include "../../common/pathname.h" #include "../../common/xml.h" #include "../../core/collections.h" /* Fixe le tampon pour la lecture des fichiers à inclure */ #define ARCHIVE_RBUF_SIZE 2048 /* Informations relatives à un client */ typedef struct _cdb_client { int fd; /* Canal de communication */ rle_string user; /* Utilisateur à l'autre bout */ uint64_t last_time; /* Date de dernier envoi */ } cdb_client; /* Description d'une archive d'éléments utilisateur (instance) */ struct _GCdbArchive { GObject parent; /* A laisser en premier */ rle_string hash; /* Empreinte cryptographique */ char *filename; /* Chemin d'accès à l'archive */ char *xml_desc; /* Fichier de description */ char *sql_db; /* Base de données SQLite */ xmlDocPtr xdoc; /* Document XML à créer */ xmlXPathContextPtr context; /* Contexte pour les recherches*/ sqlite3 *db; /* Base de données à manipuler */ GList *collections; /* Ensemble de modifications */ cdb_client *clients; /* Connexions en place */ size_t count; /* Quantité de clients */ GMutex clients_access; /* Verrou pour l'accès */ GThread *process; /* Procédure de traitement */ GMutex id_access; /* Accès à l'identifiant */ GCond id_cond; /* Condition d'attente */ pthread_t process_id; /* Identifiant de la procédure */ }; /* Description d'une archive d'éléments utilisateur (classe) */ struct _GCdbArchiveClass { GObjectClass parent; /* A laisser en premier */ }; /* Initialise la classe des archives d'éléments utilisateur. */ static void g_cdb_archive_class_init(GCdbArchiveClass *); /* Initialise une archive d'éléments utilisateur. */ static void g_cdb_archive_init(GCdbArchive *); /* Supprime toutes les références externes. */ static void g_cdb_archive_dispose(GCdbArchive *); /* Procède à la libération totale de la mémoire. */ static void g_cdb_archive_finalize(GCdbArchive *); /* Ouvre une archive avec tous les éléments à conserver. */ static bool g_cdb_archive_read(GCdbArchive *); /* -------------------------- MANIPULATION DES PARTIES XML -------------------------- */ /* Crée la description XML correspondant à l'archive. */ static bool g_cdb_archive_create_xml_desc(GCdbArchive *, const rle_string *); /* Vérifie la conformité d'une description XML avec le serveur. */ static bool g_cdb_archive_check_xml_version(const GCdbArchive *); /* ------------------------- ACCES A LA BASE DE DONNEES SQL ------------------------- */ /* Crée la base de données correspondant à l'archive. */ static bool g_cdb_archive_create_db(const GCdbArchive *); /////////////////////////: /* Crée et remplit les collections à partir de leurs bases. */ static bool g_cdb_archive_load_collections(GCdbArchive *); /* Réagit à une modification au sein d'une collection donnée. */ static void on_collection_changed(GDbCollection *, DBAction, GDbItem *, GCdbArchive *); /* Assure le traitement des requêtes de clients. */ static void *g_cdb_archive_process(GCdbArchive *); /* Indique le type défini pour une une archive d'éléments utilisateur. */ G_DEFINE_TYPE(GCdbArchive, g_cdb_archive, G_TYPE_OBJECT); /****************************************************************************** * * * Paramètres : klass = classe à initialiser. * * * * Description : Initialise la classe des archives d'éléments utilisateur. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_cdb_archive_class_init(GCdbArchiveClass *klass) { GObjectClass *object; /* Autre version de la classe */ object = G_OBJECT_CLASS(klass); object->dispose = (GObjectFinalizeFunc/* ! */)g_cdb_archive_dispose; object->finalize = (GObjectFinalizeFunc)g_cdb_archive_finalize; } /****************************************************************************** * * * Paramètres : archive = instance à initialiser. * * * * Description : Initialise une archive d'éléments utilisateur. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_cdb_archive_init(GCdbArchive *archive) { archive->collections = create_collections_list(); g_mutex_init(&archive->clients_access); g_mutex_init(&archive->id_access); g_cond_init(&archive->id_cond); } /****************************************************************************** * * * Paramètres : archive = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_cdb_archive_dispose(GCdbArchive *archive) { g_cond_clear(&archive->id_cond); g_mutex_clear(&archive->id_access); g_mutex_clear(&archive->clients_access); G_OBJECT_CLASS(g_cdb_archive_parent_class)->dispose(G_OBJECT(archive)); } /****************************************************************************** * * * Paramètres : archive = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_cdb_archive_finalize(GCdbArchive *archive) { //void close_xml_file(xmlDocPtr, xmlXPathContextPtr); free(archive->filename); G_OBJECT_CLASS(g_cdb_archive_parent_class)->finalize(G_OBJECT(archive)); } /****************************************************************************** * * * Paramètres : basedir = répertoire de stockage des enregistrements. * * hash = empreinte du binaire à représenter. * * user = désignation d'un éventuel nouveau créateur. * * error = indication éventuelle en cas d'échec. [OUT] * * * * Description : Définit ou ouvre une archive d'éléments utilisateur. * * * * Retour : Structure mise en plae ou NULL en cas d'échec. * * * * Remarques : Les chaînes sont assurées d'être non vides ; la procédure * * assume un transfert de propriété. * * * ******************************************************************************/ GCdbArchive *g_cdb_archive_new(const char *basedir, const rle_string *hash, const rle_string *user, DBError *error) { GCdbArchive *result; /* Adresse à retourner */ struct stat finfo; /* Information sur l'archive */ int ret; /* Retour d'un appel */ result = g_object_new(G_TYPE_CDB_ARCHIVE, NULL); dup_into_rle_string(&result->hash, get_rle_string(hash)); /* Chemin de l'archive */ result->filename = strdup(basedir); result->filename = stradd(result->filename, G_DIR_SEPARATOR_S); result->filename = stradd(result->filename, hash->data); result->filename = stradd(result->filename, ".tar.xz"); if (!mkpath(result->filename)) goto gcan_error; /* Chemin des enregistrements temporaires */ result->xml_desc = strdup(g_get_tmp_dir()); if (result->xml_desc[strlen(result->xml_desc) - 1] != G_DIR_SEPARATOR) result->xml_desc = stradd(result->xml_desc, G_DIR_SEPARATOR_S); result->xml_desc = stradd(result->xml_desc, result->hash.data); result->xml_desc = stradd(result->xml_desc, "_desc.xml"); result->sql_db = strdup(g_get_tmp_dir()); if (result->sql_db[strlen(result->sql_db) - 1] != G_DIR_SEPARATOR) result->sql_db = stradd(result->sql_db, G_DIR_SEPARATOR_S); result->sql_db = stradd(result->sql_db, result->hash.data); result->sql_db = stradd(result->sql_db, "_db.sql"); /* Création de l'archive si elle n'existe pas */ ret = stat(result->filename, &finfo); if (ret != 0) { /* Le soucis ne vient pas de l'absence du fichier... */ if (errno != ENOENT) goto gcan_error; g_cdb_archive_create_xml_desc(result, user); g_cdb_archive_create_db(result); *error = g_cdb_archive_write(result); if (*error != DBE_NONE) goto gcan_error; } else if (!S_ISREG(finfo.st_mode)) goto gcan_error; /* Ouverture de l'archive */ if (!g_cdb_archive_read(result)) goto gcan_error; if (!g_cdb_archive_check_xml_version(result)) { *error = DBE_XML_VERSION_ERROR; goto gcan_error; } /* Chargement des éléments sauvegardés */ if (!g_cdb_archive_load_collections(result)) { *error = DBE_DB_LOADING_ERROR; goto gcan_error; } return result; gcan_error: g_object_unref(G_OBJECT(result)); return NULL; } /****************************************************************************** * * * Paramètres : archive = informations quant à l'archive à interpréter. * * * * Description : Ouvre une archive avec tous les éléments à conserver. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_cdb_archive_read(GCdbArchive *archive) { bool result; /* Conclusion à retourner */ struct archive *in; /* Archive à consulter */ int ret; /* Bilan d'un appel */ int flags; /* Propriétés à extraire */ struct archive *out; /* Extracteur générique */ struct archive_entry *entry; /* Elément de l'archive */ const char *path; /* Désignation d'un fichier */ result = false; in = archive_read_new(); archive_read_support_filter_all(in); archive_read_support_format_all(in); ret = archive_read_open_filename(in, archive->filename, 10240 /* ?! */); if (ret != ARCHIVE_OK) goto gcar_exit; /* Propriétés à restaurer */ flags = ARCHIVE_EXTRACT_TIME; flags |= ARCHIVE_EXTRACT_PERM; flags |= ARCHIVE_EXTRACT_ACL; flags |= ARCHIVE_EXTRACT_FFLAGS; out = archive_write_disk_new(); archive_write_disk_set_options(out, flags); archive_write_disk_set_standard_lookup(out); for (ret = archive_read_next_header(in, &entry); ret == ARCHIVE_OK; ret = archive_read_next_header(in, &entry)) { bool dump_arch_data(struct archive_entry *ent, struct archive *input, struct archive *output) { const void *buff; /* Tampon de copie */ size_t size; /* Quantité copiée */ __LA_INT64_T offset; /* Position de lecture */ ret = archive_write_header(output, entry); if (ret != ARCHIVE_OK) return false; for (ret = archive_read_data_block(input, &buff, &size, &offset); ret == ARCHIVE_OK; ret = archive_read_data_block(input, &buff, &size, &offset)) { ret = archive_write_data_block(output, buff, size, offset); if (ret != ARCHIVE_OK) return false; } if (ret != ARCHIVE_EOF) return false; ret = archive_write_finish_entry(output); return (ret == ARCHIVE_OK); } path = archive_entry_pathname(entry); if (strcmp(path, "desc.xml") == 0) { archive_entry_set_pathname(entry, archive->xml_desc); if (!dump_arch_data(entry, in, out)) goto gcar_exit; if (!open_xml_file(archive->xml_desc, &archive->xdoc, &archive->context)) goto gcar_exit; } else if (strcmp(path, "sql.db") == 0) { archive_entry_set_pathname(entry, archive->sql_db); if (!dump_arch_data(entry, in, out)) goto gcar_exit; ret = sqlite3_open(archive->sql_db, &archive->db); if (ret != SQLITE_OK) goto gcar_exit; } } archive_read_close(in); archive_read_free(in); archive_write_close(out); archive_write_free(out); result = true; gcar_exit: return result; } /****************************************************************************** * * * Paramètres : archive = informations quant à l'archive à créer. * * * * Description : Enregistre une archive avec tous les éléments à conserver. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ DBError g_cdb_archive_write(const GCdbArchive *archive) { DBError result; /* Conclusion à retourner */ struct archive *out; /* Archive à constituer */ int ret; /* Bilan d'un appel */ result = DBE_ARCHIVE_ERROR; out = archive_write_new(); archive_write_add_filter_xz(out); archive_write_set_format_gnutar(out); ret = archive_write_open_filename(out, archive->filename); if (ret != ARCHIVE_OK) goto gcaw_exit; DBError add_file_to_archive(struct archive *out, const char *src, const char *path) { DBError status; /* Bilan à renvoyer */ struct stat info; /* Informations d'origine */ struct archive_entry *entry; /* Elément de l'archive */ int fd; /* Flux ouvert en lecture */ char buffer[ARCHIVE_RBUF_SIZE]; /* Tampon pour les transferts */ ssize_t len; /* Quantité de données lues */ status = DBE_ARCHIVE_ERROR; ret = stat(src, &info); if (ret != 0) return DBE_SYS_ERROR; entry = archive_entry_new(); archive_entry_copy_stat(entry, &info); archive_entry_set_pathname(entry, path); ret = archive_write_header(out, entry); if (ret != 0) goto afta_error; fd = open(src, O_RDONLY); if (fd == -1) { status = DBE_SYS_ERROR; goto afta_error; } for (len = safe_read(fd, buffer, ARCHIVE_RBUF_SIZE); len > 0; len = safe_read(fd, buffer, ARCHIVE_RBUF_SIZE)) { if (archive_write_data(out, buffer, len) != len) goto afta_error; } close(fd); archive_entry_free(entry); return DBE_NONE; afta_error: archive_entry_free(entry); return status; } result = add_file_to_archive(out, archive->xml_desc, "desc.xml"); if (result == DBE_NONE) result = add_file_to_archive(out, archive->sql_db, "sql.db"); gcaw_exit: archive_write_free(out); return result; } /****************************************************************************** * * * Paramètres : archive = informations quant à l'archive à consulter. * * hash = empreinte extérieure à comparer. * * * * Description : Détermine si une empreinte correspond à celle d'une archive. * * * * Retour : Résultat de la comparaison : -1, 0 ou 1. * * * * Remarques : - * * * ******************************************************************************/ int g_cdb_archive_compare_hash(const GCdbArchive *archive, const rle_string *hash) { return cmp_rle_string(&archive->hash, hash); } /* ---------------------------------------------------------------------------------- */ /* MANIPULATION DES PARTIES XML */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : archive = archive à constituer. * * user = désignation d'un éventuel nouveau créateur. * * * * Description : Crée la description XML correspondant à l'archive. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_cdb_archive_create_xml_desc(GCdbArchive *archive, const rle_string *user) { bool result; /* Bilan à retourner */ timestamp_t timestamp; /* Date de création */ char tmp[sizeof(XSTR(UINT64_MAX))]; /* Stockage temporaire */ result = create_new_xml_file(&archive->xdoc, &archive->context); if (!result) return false; result &= add_content_to_node(archive->xdoc, archive->context, "/ChrysalideBinary/Version", PACKAGE_VERSION); result &= add_content_to_node(archive->xdoc, archive->context, "/ChrysalideBinary/Protocol", XSTR(CDB_PROTOCOL_VERSION)); result &= add_content_to_node(archive->xdoc, archive->context, "/ChrysalideBinary/Hash", archive->hash.data); result &= add_content_to_node(archive->xdoc, archive->context, "/ChrysalideBinary/Creation/Author", user->data); init_timestamp(×tamp); snprintf(tmp, sizeof(tmp), "%" PRIu64, timestamp); result &= add_content_to_node(archive->xdoc, archive->context, "/ChrysalideBinary/Creation/Date", tmp); save_xml_file(archive->xdoc, archive->xml_desc); return result; } /****************************************************************************** * * * Paramètres : archive = archive à consulter. * * * * Description : Vérifie la conformité d'une description XML avec le serveur. * * * * Retour : Bilan de la vérification. * * * * Remarques : - * * * ******************************************************************************/ static bool g_cdb_archive_check_xml_version(const GCdbArchive *archive) { bool result; /* Bilan à retourner */ char *version; /* Version protocolaire */ unsigned long int used; /* Version utilisée */ result = NULL; version = get_node_text_value(archive->context, "/ChrysalideBinary/Protocol"); if (version == NULL) return false; used = strtoul(version, NULL, 16); result = (used == CDB_PROTOCOL_VERSION); free(version); return result; } /* ---------------------------------------------------------------------------------- */ /* ACCES A LA BASE DE DONNEES SQL */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : archive = archive à constituer. * * * * Description : Crée la base de données correspondant à l'archive. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_cdb_archive_create_db(const GCdbArchive *archive) { bool result; /* Bilan à retourner */ sqlite3 *db; /* Base de données à constituer*/ int ret; /* Bilan de la création */ GList *iter; /* Boucle de parcours */ GDbCollection *collec; /* Collection visée manipulée */ ret = sqlite3_open(archive->sql_db, &db); if (ret != SQLITE_OK) { fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(db)); return false; } result = true; for (iter = g_list_first(archive->collections); iter != NULL && result; iter = g_list_next(iter)) { collec = G_DB_COLLECTION(iter->data); result = g_db_collection_create_db_table(collec, db); } sqlite3_close(db); return result; } /* ---------------------------------------------------------------------------------- */ /* ACCES A LA BASE DE DONNEES SQL */ /* ACCES A LA BASE DE DONNEES SQL */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : archive = archive dont les collections sont à initialiser. * * * * Description : Crée et remplit les collections à partir de leurs bases. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ static bool g_cdb_archive_load_collections(GCdbArchive *archive) { GList *iter; /* Boucle de parcours */ GDbCollection *collec; /* Collection visée manipulée */ for (iter = g_list_first(archive->collections); iter != NULL; iter = g_list_next(iter)) { collec = G_DB_COLLECTION(iter->data); g_signal_connect(collec, "content-changed", G_CALLBACK(on_collection_changed), archive); if (!g_db_collection_load_all_items(collec, archive->db)) return false; } return true; } /****************************************************************************** * * * Paramètres : collec = collection dont le contenu a évolué. * * action = type d'évolution rencontrée. * * item = élément ajouté, modifié ou supprimé. * * archive = centralisation de tous les savoirs. * * * * Description : Réagit à une modification au sein d'une collection donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void on_collection_changed(GDbCollection *collec, DBAction action, GDbItem *item, GCdbArchive *archive) { packed_buffer pbuf; /* Tampon d'émission */ size_t i; /* Boucle de parcours */ bool status; /* Bilan d'un envoi de retour */ init_packed_buffer(&pbuf); status = g_db_collection_pack(collec, &pbuf, action, item); g_mutex_lock(&archive->clients_access); for (i = 0; i < archive->count && status; i++) status = send_packed_buffer(&pbuf, archive->clients[i].fd); g_mutex_unlock(&archive->clients_access); exit_packed_buffer(&pbuf); if (!status) goto occ_error; printf("CHANGED for %d clients !!\n", (int)archive->count); occ_error: /* TODO : close() */ ; } /****************************************************************************** * * * Paramètres : archive = centralisation de tous les savoirs. * * * * Description : Assure le traitement des requêtes de clients. * * * * Retour : NULL. * * * * Remarques : - * * * ******************************************************************************/ static void *g_cdb_archive_process(GCdbArchive *archive) { struct pollfd *fds; /* Surveillance des flux */ nfds_t nfds; /* Quantité de ces flux */ nfds_t i; /* Boucle de parcours */ int ret; /* Bilan d'un appel */ packed_buffer in_pbuf; /* Tampon de réception */ uint32_t tmp32; /* Valeur sur 32 bits */ bool status; /* Bilan de lecture initiale */ uint32_t command; /* Commande de la requête */ DBError error; /* Bilan d'une opération */ packed_buffer out_pbuf; /* Tampon d'émission */ GDbCollection *collec; /* Collection visée au final */ void interrupt_poll_with_sigusr1(int sig) { }; signal(SIGUSR1, interrupt_poll_with_sigusr1); g_mutex_lock(&archive->id_access); archive->process_id = pthread_self(); g_cond_signal(&archive->id_cond); g_mutex_unlock(&archive->id_access); fds = NULL; while (1) { /* Reconstitution d'une liste à jour */ g_mutex_lock(&archive->clients_access); nfds = archive->count; fds = (struct pollfd *)realloc(fds, nfds * sizeof(struct pollfd)); for (i = 0; i < nfds; i++) { fds[i].fd = archive->clients[i].fd; fds[i].events = POLLIN | POLLPRI; } g_mutex_unlock(&archive->clients_access); if (nfds == 0) goto gcap_no_more_clients; /* Lancement d'une phase de surveillance */ ret = poll(fds, nfds, -1); if (ret == -1) { if (errno == EINTR) continue; perror("poll"); break; } /* Traitement des requêtes reçues */ for (i = 0; i < nfds; i++) { /* Le canal est fermé, une sortie doit être demandée... */ if (fds[i].revents & POLLNVAL) goto gcap_bad_exchange; /* Données présentes en entrée */ if (fds[i].revents & (POLLIN | POLLPRI)) { status = recv_packed_buffer(&in_pbuf, fds[i].fd); if (!status) goto gcap_bad_exchange; status = extract_packed_buffer(&in_pbuf, &tmp32, sizeof(uint32_t), true); if (!status) goto gcap_bad_exchange; command = tmp32; switch (command) { case DBC_SAVE: error = g_cdb_archive_write(archive); init_packed_buffer(&out_pbuf); status = extend_packed_buffer(&out_pbuf, (uint32_t []) { DBC_SAVE }, sizeof(uint32_t), true); if (!status) goto gcap_bad_reply; status = extend_packed_buffer(&out_pbuf, (uint32_t []) { error }, sizeof(uint32_t), true); if (!status) goto gcap_bad_reply; status = send_packed_buffer(&out_pbuf, fds[i].fd); if (!status) goto gcap_bad_reply; exit_packed_buffer(&out_pbuf); break; case DBC_COLLECTION: status = extract_packed_buffer(&in_pbuf, &tmp32, sizeof(uint32_t), true); if (!status) goto gcap_bad_exchange; collec = find_collection_in_list(archive->collections, tmp32); if (collec == NULL) goto gcap_bad_exchange; status = g_db_collection_unpack(collec, &in_pbuf, archive->db); if (!status) goto gcap_bad_exchange; printf("## CDB ## Got something for collection %p...\n", collec); //GDbCollection *find_collection_in_list(GList *, uint32_t); //static GGenConfig *find_collection_in_list(GList *list, uint32_t id) break; case DBC_SET_LAST_ACTIVE: status = update_activity_in_collections(archive->collections, &in_pbuf, archive->db); if (!status) goto gcap_bad_exchange; break; default: printf("bad command :: 0x%08x\n", command); goto gcap_bad_exchange; break; } exit_packed_buffer(&in_pbuf); continue; gcap_bad_reply: exit_packed_buffer(&out_pbuf); gcap_bad_exchange: printf("Bad exchange...\n"); exit_packed_buffer(&in_pbuf); /* TODO : close conn */ ; } } } /* On disparaît des écrans... */ gcap_no_more_clients: archive->process = NULL; g_mutex_lock(&archive->id_access); archive->process_id = 0; g_cond_signal(&archive->id_cond); g_mutex_unlock(&archive->id_access); if (fds != NULL) free(fds); return NULL; } /****************************************************************************** * * * Paramètres : archive = archive à connecter avec un utilisateur. * * fd = canal de communication réseau ouvert. * * user = désignation de l'utilisateur associé. * * * * Description : Associe un nouvel utilisateur à l'archive. * * * * Retour : Indication d'une éventuelle erreur lors de l'opération. * * * * Remarques : - * * * ******************************************************************************/ DBError g_cdb_archive_add_client(GCdbArchive *archive, int fd, const rle_string *user) { GList *iter; /* Boucle de parcours */ GDbCollection *collec; /* Collection visée manipulée */ volatile pthread_t *process_id; /* Identifiant de la procédure */ packed_buffer out_pbuf; /* Tampon d'émission */ bool status; /* Bilan d'un envoi de retour */ printf("Add '%s' for archive...\n", user->data); g_mutex_lock(&archive->clients_access); /* Ajout dans la liste officielle */ archive->clients = (cdb_client *)realloc(archive->clients, ++archive->count * sizeof(cdb_client)); archive->clients[archive->count - 1].fd = fd; dup_into_rle_string(&archive->clients[archive->count - 1].user, get_rle_string(user)); /* Démarrage ou redémarrage du processus d'écoute */ if (archive->process == NULL) { archive->process = g_thread_new("cdb_process", (GThreadFunc)g_cdb_archive_process, archive); /* On attend que le processus parallèle soit prêt */ process_id = &archive->process_id; g_mutex_lock(&archive->id_access); while (process_id == 0) g_cond_wait(&archive->id_cond, &archive->id_access); g_mutex_unlock(&archive->id_access); } else pthread_kill(archive->process_id, SIGUSR1); g_mutex_unlock(&archive->clients_access); /* Envoi des mises à jour au nouveau client... */ init_packed_buffer(&out_pbuf); status = true; /* TODO : lock ? */ for (iter = g_list_first(archive->collections); iter != NULL && status; iter = g_list_next(iter)) { collec = G_DB_COLLECTION(iter->data); status = g_db_collection_pack_all_updates(collec, &out_pbuf); } if (status) status = send_packed_buffer(&out_pbuf, fd); exit_packed_buffer(&out_pbuf); return DBE_NONE; //// }