diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/io.c | 114 | ||||
| -rw-r--r-- | src/common/io.h | 7 | 
2 files changed, 116 insertions, 5 deletions
diff --git a/src/common/io.c b/src/common/io.c index 8e56379..85be44c 100644 --- a/src/common/io.c +++ b/src/common/io.c @@ -28,10 +28,17 @@  #include <libgen.h>  #include <malloc.h>  #include <stdint.h> +#include <stdio.h> +#include <stdlib.h>  #include <string.h>  #include <unistd.h>  #include <sys/socket.h>  #include <sys/stat.h> +#include <sys/un.h> + + +#include "../core/logs.h" +#include "../core/params.h" @@ -284,11 +291,6 @@ bool safe_send(int sockfd, const void *buf, size_t len, int flags)  } - - - - -  /******************************************************************************  *                                                                             *  *  Paramètres  : path = chemin d'accès à valider.                             * @@ -326,3 +328,105 @@ int ensure_path_exists(const char *path)      return result;  } + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : base     = préfixe du nom du fichier temporaire à créer.     * +*                filename = chemin d'accès complet au nouveau fichier. [OUT]  * +*                                                                             * +*  Description : Met en place un fichier temporaire.                          * +*                                                                             * +*  Retour      : Flux ouvert en lecture et écriture, ou -1 en cas d'erreur.   * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +int make_tmp_file(const char *base, char **filename) +{ +    int result;                             /* Flux ou code à retourner    */ +    const char *tmpdir;                     /* Répertoire d'accueil        */ +    bool status;                            /* Bilan d'un consultation     */ + +    status = g_generic_config_get_value(get_main_configuration(), MPK_TMPDIR, &tmpdir); +    if (!status) return -1; + +    asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX", tmpdir, base, getpid()); + +    result = ensure_path_exists(*filename); + +    if (result == 0) +    { +        result = mkstemp(*filename); +        if (result == -1) perror("mkstemp"); +    } + +    if (result == -1) +        free(*filename); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : base = préfixe du nom du fichier temporaire à créer.         * +*                addr = adresse UNIX constituée. [OUT]                        * +*                                                                             * +*  Description : Met en place un canal UNIX temporaire.                       * +*                                                                             * +*  Retour      : Bilan de la définition.                                      * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +bool build_tmp_socket(const char *base, struct sockaddr_un *addr) +{ +    bool result;                            /* Bilan à retourner           */ +    const char *tmpdir;                     /* Répertoire d'accueil        */ +    bool status;                            /* Bilan d'un consultation     */ +    char *path;                             /* Chemin d'accès au canal     */ +    int ret;                                /* Bilan intermédiaire         */ +    size_t length;                          /* Taille du chemin complet    */ + +    status = g_generic_config_get_value(get_main_configuration(), MPK_TMPDIR, &tmpdir); +    if (!status) return false; + +    result = false; + +    asprintf(&path, "%s" G_DIR_SEPARATOR_S "%s-%d", tmpdir, base, getpid()); + +    ret = ensure_path_exists(path); +    if (ret != 0) goto mts_exit; + +    length = strlen(path) + 1; + +#ifndef UNIX_PATH_MAX +#   define UNIX_PATH_MAX 108 +#endif + +    if (length > UNIX_PATH_MAX) +    { +        log_variadic_message(LMT_ERROR, +                             _("Impossible to use '%s' as UNIX socket path: string is too long ! (%zu vs %u)\n"), +                             path, length, UNIX_PATH_MAX); +        goto mts_exit; +    } + +    memset(addr, 0, sizeof(struct sockaddr_un)); + +    addr->sun_family = AF_UNIX; +    strncpy(addr->sun_path, path, UNIX_PATH_MAX - 1); + +    result = true; + + mts_exit: + +    free(path); + +    return result; + +} diff --git a/src/common/io.h b/src/common/io.h index 0ada366..d70c64a 100644 --- a/src/common/io.h +++ b/src/common/io.h @@ -28,6 +28,7 @@  #include <stdbool.h>  #include <sys/types.h>  #include <sys/socket.h> +#include <sys/un.h> @@ -49,6 +50,12 @@ bool safe_send(int, const void *, size_t, int);  /* S'assure qu'un chemin donné existe dans le système. */  int ensure_path_exists(const char *); +/* Met en place un fichier temporaire. */ +int make_tmp_file(const char *, char **); + +/* Met en place un canal UNIX temporaire. */ +bool build_tmp_socket(const char *, struct sockaddr_un *); +  #endif  /* _COMMON_IO_H */  | 
