Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Move Mumble Socket and Overlay to $XDG_RUNTIME_DIR/mumble/ #5961

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "3rdparty/SPSCQueue"]
path = 3rdparty/SPSCQueue
url = https://github.com/rigtorp/SPSCQueue.git
[submodule "3rdparty/cwalk"]
path = 3rdparty/cwalk
url = https://github.com/likle/cwalk.git
1 change: 1 addition & 0 deletions 3rdparty/cwalk
Submodule cwalk added at cfe828
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ endif()

option(lto "Enables link-time optimizations for release builds" ${LTO_DEFAULT})

# option(bundled-cwalk "Build the included version of CWalk instead of looking for one on the system." ON)

include(compiler)
include(os)

Expand Down Expand Up @@ -165,6 +167,29 @@ if(client OR server)
add_subdirectory(src)
endif()

# if(client OR overlay)
# if(bundled-cwalk)
# add_subdirectory("${3RDPARTY_DIR}/cwalk" "${CMAKE_CURRENT_BINARY_DIR}/cwalk" EXCLUDE_FROM_ALL)
# if(overlay-xcompile)
# # Just check for this header file while using a 32bit target as a really small and incomplete check whether g++-multilib seems to be
# # installed. If we don't find it, we can assume it's not there but if we do find it, we still don't know. Thus we still print the
# # message about the 32bit target potentially failing due to missing g++-multilib.
# CHECK_INCLUDE_FILE("sys/cdefs.h" FOUND_CDEFS "-m32")
# if(NOT FOUND_CDEFS)
# message(FATAL_ERROR "Can't find the 32bit version of sys/cdefs.h - did you install g++-multilib?")
# else()
# message(STATUS "\nIf the 32 bit overlay library fails to compile, make sure the requirements are installed (\"g++-multilib\" package on Debian-based distributions).\n")
# endif()


# add_subdirectory("${3RDPARTY_DIR}/cwalk" "${CMAKE_CURRENT_BINARY_DIR}/cwalk_x86" EXCLUDE_FROM_ALL)
# set_target_properties(cwalk PROPERTIES COMPILE_OPTIONS "-m32" LINK_OPTIONS "-m32")
# endif()
# else()
# find_pkg(Cwalk REQUIRED)
# endif()
# endif()

if(g15 AND WIN32)
add_subdirectory("helpers/g15helper")
endif()
Expand Down
2 changes: 2 additions & 0 deletions overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ target_sources(overlay
"dxgi.cpp"
"excludecheck.cpp"
"excludecheck.h"
"ipc_utils.h"
"ipc_utils.c"
"lib.cpp"
"lib.h"
"ods.cpp"
Expand Down
97 changes: 97 additions & 0 deletions overlay/ipc_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "ipc_utils.h"
// #include <cwalk.h>
#include <string.h>

#ifdef _WIN32
# include <direct.h>
# include <stdlib.h>
# include <malloc.h>
#else
# include <pwd.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif

// can't trust POSIX's nor Window's PATH_MAX constants
// see: https://eklitzke.org/path-max-is-tricky
// https://stackoverflow.com/a/56385296
#define MUMBLE_MAX_PATH 1024

char *getRuntimePath__() {
char buffer[MUMBLE_MAX_PATH];
char *path = NULL;

#ifdef _WIN32
path = strdup("\0");
#elif __linux__
char *xdgRuntimeDir = getenv("XDG_RUNTIME_DIR");
if (xdgRuntimeDir == NULL || !xdgRuntimeDir) {
// char uid[10];
// sprintf(uid, "%d", getuid());
xdgRuntimeDir = "/run/user/";
// cwk_path_join(xdgRuntimeDir, uid, buffer, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/%d", xdgRuntimeDir, getuid());
}
// size_t path_len = cwk_path_join(xdgRuntimeDir, "mumble", buffer, sizeof(buffer));
int path_len = snprintf(buffer, sizeof(buffer), "%s/mumble/", xdgRuntimeDir);
#else
char *home = getenv("HOME");
if (home == NULL) {
struct passwd *pwent = getpwuid(getuid());
if (pwent && pwent->pw_dir && pwent->pw_dir[0])
home = pwent->pw_dir;
else
return NULL;
}
if ((path = malloc(strlen(home) + 1)) == NULL)
return NULL;
int path_len = snprintf(buffer, sizeof(buffer), "%s/", home);
#endif
#if !defined _WIN32
// if (path_len != strlen(buffer))
// buffer is too small. Result is truncated
if ((path = malloc(path_len + 1)) == NULL)
return NULL;
strcpy(path, buffer);
#endif
return path;
}

char *getAndCreateOverlayPipePath__() {
char buffer[MUMBLE_MAX_PATH];
char *overlapyPipeFile = "MumbleOverlayPipe";
char *path = NULL;
char *runtimePath = getRuntimePath__();
if (runtimePath == NULL)
return NULL;
#if !defined __linux__ && !defined _WIN32
char *path_template = "%s.%s";
#else
char *path_template = "%s%s";
#endif
#if _WIN32
/*
* on Windows we don't create the directory as getRuntimePath__() returns an empty string.
* _mkdir(runtimePath);
*/
#else
mkdir(runtimePath, 0755);
#endif
// size_t path_len = cwk_path_join(runtimePath, overlapyPipeFile, buffer, sizeof(buffer));
int path_len = snprintf(buffer, sizeof(buffer), path_template, runtimePath, overlapyPipeFile);
// if (path_len != strlen(path))
// path is too small. Result is truncated
if ((path = malloc(path_len + 1)) == NULL)
return NULL;
strcpy(path, buffer);
free(runtimePath);
return path;
}
13 changes: 13 additions & 0 deletions overlay/ipc_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_OVERLAY_IPC_UTILS_H__
#define MUMBLE_OVERLAY_IPC_UTILS_H__

char *getRuntimePath__();

char *getAndCreateOverlayPipePath__();
Comment on lines +9 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the double-underscores in the name? Usually that is reserved for internal library functions and should be avoided in userspace land

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this notation to distinguish it from the functions in src/mumble/IPCUtils[.h, .cpp]. They wrap the C functions in C++.
If you have better a idea, please tell me! I'm short of creativity hahahaha


#endif // MUMBLE_OVERLAY_IPC_UTILS_H__
19 changes: 12 additions & 7 deletions overlay_gl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STRE
endif()
endif()

add_library(overlay_gl SHARED "overlay.c")
set(OVERLAY_SOURCES
"${CMAKE_SOURCE_DIR}/overlay/ipc_utils.c"
"overlay.c"
)
add_library(overlay_gl SHARED ${OVERLAY_SOURCES})

# target_link_libraries(overlay_gl PRIVATE cwalk)

set_target_properties(overlay_gl
PROPERTIES
Expand All @@ -31,10 +37,9 @@ if(NOT APPLE)
"-Wl,-z,lazy"
)

set_target_properties(overlay_gl
PROPERTIES
COMPILE_DEFINITIONS
"TARGET_UNIX"
target_compile_definitions(overlay_gl
PRIVATE
"TARGET_UNIX"
)

if(overlay-xcompile)
Expand All @@ -47,13 +52,13 @@ if(NOT APPLE)
else()
message(STATUS "\nIf the 32 bit overlay library fails to compile, make sure the requirements are installed (\"g++-multilib\" package on Debian-based distributions).\n")
endif()

set_target_properties(overlay_gl
PROPERTIES
OUTPUT_NAME "mumbleoverlay.x86_64"
)

add_library(overlay_gl_x86 SHARED "overlay.c")
add_library(overlay_gl_x86 SHARED ${OVERLAY_SOURCES})
# target_link_libraries(overlay_gl_x86 PRIVATE cwalk)

target_compile_definitions(overlay_gl_x86
PRIVATE
Expand Down
25 changes: 5 additions & 20 deletions overlay_gl/overlay.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <semaphore.h>
#include <stdarg.h>
#include <stdio.h>
Expand Down Expand Up @@ -53,6 +52,7 @@ typedef unsigned char bool;
# include "avail_mac.h"
#endif

#include "../overlay/ipc_utils.h"
#include "../overlay/overlay.h"

static bool bDebug = false;
Expand Down Expand Up @@ -147,25 +147,10 @@ static void newContext(Context *ctx) {
ctx->timeT = clock();
ctx->frameCount = 0;

char *home = getenv("HOME");
if (home == NULL) {
struct passwd *pwent = getpwuid(getuid());
if (pwent && pwent->pw_dir && pwent->pw_dir[0]) {
home = pwent->pw_dir;
}
}

char *xdgRuntimeDir = getenv("XDG_RUNTIME_DIR");

if (xdgRuntimeDir != NULL) {
ctx->saName.sun_family = PF_UNIX;
strcpy(ctx->saName.sun_path, xdgRuntimeDir);
strcat(ctx->saName.sun_path, "/MumbleOverlayPipe");
} else if (home) {
ctx->saName.sun_family = PF_UNIX;
strcpy(ctx->saName.sun_path, home);
strcat(ctx->saName.sun_path, "/.MumbleOverlayPipe");
}
char *path = getAndCreateOverlayPipePath__();
strcpy(ctx->saName.sun_path, path);
free(path);
ctx->saName.sun_family = PF_UNIX;

ods("OpenGL Version %s, Vendor %s, Renderer %s, Shader %s", glGetString(GL_VERSION), glGetString(GL_VENDOR),
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
Expand Down
5 changes: 5 additions & 0 deletions src/mumble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ set(MUMBLE_SOURCES
"GlobalShortcutButtons.ui"
"GlobalShortcutTarget.ui"
"GlobalShortcutTypes.h"
"IPCUtils.cpp"
"IPCUtils.h"
"${CMAKE_SOURCE_DIR}/overlay/ipc_utils.c"
"${CMAKE_SOURCE_DIR}/overlay/ipc_utils.h"
"JSONSerialization.cpp"
"JSONSerialization.h"
"LCD.cpp"
Expand Down Expand Up @@ -508,6 +512,7 @@ else()
find_pkg("nlohmann_json" REQUIRED)
endif()

# target_link_libraries(mumble_client_object_lib PUBLIC cwalk)
target_link_libraries(mumble_client_object_lib PUBLIC nlohmann_json::nlohmann_json)

find_pkg("SndFile;LibSndFile;sndfile" REQUIRED)
Expand Down
51 changes: 51 additions & 0 deletions src/mumble/IPCUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021-2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "IPCUtils.h"
extern "C" {
#include "../../overlay/ipc_utils.h"
}

#include <QDir>
#include <QProcessEnvironment>
#include <QString>

namespace Mumble {
const std::string getRuntimePath(void) {
char *c_mumbleRuntimePath = getRuntimePath__();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to delete the allocated buffer once you are done with it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change this to directly assigning the result to a std::string variable. This way the destructor should be deterministically called thanks to RAII.

I'll wrap the function call into a try-catch because if the C function returns a NULL pointer, C++ throws.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still have to delete the source buffer as converting a C string into a std::string will copy the buffer, not take ownership of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use std::unique_ptr as a wrapper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not possible to create a std::string directly from a char* array without copying. I thus doubt using std::unique_ptr would help, unless I misunderstood your point :\

if (c_mumbleRuntimePath == NULL)
return "";
std::string mumbleRuntimePath = c_mumbleRuntimePath;
std::free(c_mumbleRuntimePath);
return mumbleRuntimePath;
}

const std::string getAndCreateOverlayPipePath(void) {
char *c_pipepath = getAndCreateOverlayPipePath__();
if (c_pipepath == NULL)
return "";
std::string pipepath = c_pipepath;
std::free(c_pipepath);
return pipepath;
}

const std::string getAndCreateSocketPath(const std::string &basename) {
const std::string mumbleRuntimePath = getRuntimePath();
#ifdef Q_OS_WIN
return basename;
#endif
if (mumbleRuntimePath.empty())
return "";
QString qMumbleRuntimePath = QString::fromUtf8(mumbleRuntimePath.data(), int(mumbleRuntimePath.size()));
QDir(qMumbleRuntimePath).mkpath(".");
#ifdef Q_OS_LINUX
std::string pipepath = mumbleRuntimePath + basename + "Socket";
#else // MAC or BSD
const std::string pipepath = mumbleRuntimePath + "." + basename + "Socket";
#endif
return pipepath;
}

}; // namespace Mumble
30 changes: 30 additions & 0 deletions src/mumble/IPCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021-2022 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#ifndef MUMBLE_MUMBLE_IPCUTILS_H_
#define MUMBLE_MUMBLE_IPCUTILS_H_

#include <string>

namespace Mumble {

/**
* @returns The path to MumbleOverlayPipe
*/
const std::string getAndCreateOverlayPipePath();

/**
* @returns The path to MumbleSocket
*/
const std::string getAndCreateSocketPath(const std::string &);

/**
* @returns The path where Mumble sets up MumbleOverlayPipe and MumbleSocket
*/
const std::string getRuntimePath();

}; // namespace Mumble

#endif // MUMBLE_MUMBLE_IPCUTILS_H_
Loading
Loading