diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2018-04-11 00:36:41 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2018-04-11 00:36:41 (GMT) |
commit | 30009c26869094699c93ff5d922405e3a13fd26f (patch) | |
tree | 4bc9160061d1ab4dc1f8250185242fee1b348439 /src/common/io.c | |
parent | 3fbcabfd38e58dc4c47c9cd6ad95acb82e6e352b (diff) |
Moved all the temporary creations in a temporary directory.
Diffstat (limited to 'src/common/io.c')
-rw-r--r-- | src/common/io.c | 114 |
1 files changed, 109 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; + +} |