- size_t len = strlen(s1) + strlen(s2);
- if (len > max_len)
- len = max_len;
- char *p = MALLOC(len + 1);
- if (!p)
- return NULL;
- snprintf(p, len + 1, "%s%s", s1, s2);
- return p;
-}
-
-static int set_message_queue_names(struct wimfs_context *ctx,
- const char *mount_dir)
-{
- static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
- static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
- char *dir_path;
- char *p;
- int ret;
-
- dir_path = realpath(mount_dir, NULL);
- if (!dir_path) {
- ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
- if (errno == ENOMEM)
- return WIMLIB_ERR_NOMEM;
- else
- return WIMLIB_ERR_NOTDIR;
- }
-
- p = dir_path;
- while (*p) {
- if (*p == '/')
- *p = 0xff;
- p++;
- }
-
- ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
- NAME_MAX);
- if (!ctx->unmount_to_daemon_mq_name) {
- ret = WIMLIB_ERR_NOMEM;
- goto out_free_dir_path;
- }
- ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
- NAME_MAX);
- if (!ctx->daemon_to_unmount_mq_name) {
- ret = WIMLIB_ERR_NOMEM;
- goto out_free_unmount_to_daemon_mq_name;
- }
-
- ret = 0;
- goto out_free_dir_path;
-out_free_unmount_to_daemon_mq_name:
- FREE(ctx->unmount_to_daemon_mq_name);
- ctx->unmount_to_daemon_mq_name = NULL;
-out_free_dir_path:
- FREE(dir_path);
- return ret;
-}
-
-static void free_message_queue_names(struct wimfs_context *ctx)
-{
- FREE(ctx->unmount_to_daemon_mq_name);
- FREE(ctx->daemon_to_unmount_mq_name);
- ctx->unmount_to_daemon_mq_name = NULL;
- ctx->daemon_to_unmount_mq_name = NULL;
-}
-
-/*
- * Opens two POSIX message queue: one for sending messages from the unmount
- * process to the daemon process, and one to go the other way. The names of the
- * message queues, which must be system-wide unique, are be based on the mount
- * point.
- *
- * @daemon specifies whether the calling process is the filesystem daemon or the
- * unmount process.
- */
-static int open_message_queues(struct wimfs_context *ctx, bool daemon)
-{
- int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
- int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
-
- if (daemon)
- swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
-
- DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
- ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
- unmount_to_daemon_mq_flags, 0700, NULL);
-
- if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
- ERROR_WITH_ERRNO("mq_open()");
- return WIMLIB_ERR_MQUEUE;
- }