Skip to content

Commit

Permalink
REFAC(ipc): extract IPCUtils for Socket and OverlayPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
carlocastoldi committed Dec 4, 2022
1 parent 6120405 commit a421e1e
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 117 deletions.
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
107 changes: 107 additions & 0 deletions overlay/ipc_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 <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

char *getRuntimePath__() {
size_t n = 0;
char *path = NULL;

#ifdef __linux__
char *xdgRuntimeDir = getenv("XDG_RUNTIME_DIR");

if (xdgRuntimeDir != NULL) {
if (xdgRuntimeDir)
n += strlen(xdgRuntimeDir);
if (xdgRuntimeDir[(strlen(xdgRuntimeDir) - 1)] != '/')
n++;
n += strlen("mumble/");

if ((path = malloc(n + 1)) == NULL)
return path;
*path = '\0';

strcpy(path, xdgRuntimeDir);
if (xdgRuntimeDir[(strlen(xdgRuntimeDir) - 1)] != '/')
strcat(path, "/");
strcat(path, "mumble/");
} else {
char uid[10];
n += strlen("/run/user//mumble/");
sprintf(uid, "%d", getuid());
n += strlen(uid);

if ((path = malloc(n + 1)) == NULL)
return path;
*path = '\0';

strcpy(path, "/run/user/");
strcat(path, uid);
strcat(path, "/mumble/");
}
#elif defined(_WIN32)
path = strdup("");
#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;
}
if (home == NULL)
return NULL;
n += strlen(home);
if (home[(strlen(home) - 1)] != '/')
n++;
if ((path = malloc(n + 1)) == NULL)
return path;
strcpy(path, home);
if (home[(strlen(home) - 1)] != '/')
strcat(path, "/");
#endif
return path;
}

char *getAndCreateOverlayPipePath__() {
char *runtimePath = getRuntimePath__();
char *path = NULL;
if (runtimePath == NULL)
return runtimePath;
#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 n = 1;
n += strlen(runtimePath);
n += strlen("MumbleOverlayPipe");
path = (char *) malloc(n + 1);
if (path == NULL)
return path;
#if !defined __linux__ && !defined _WIN32
strcat(path, ".");
#endif
strcpy(path, runtimePath);
strcat(path, "MumbleOverlayPipe");
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__();

#endif // MUMBLE_OVERLAY_IPC_UTILS_H__
37 changes: 5 additions & 32 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,37 +147,10 @@ static void newContext(Context *ctx) {
ctx->timeT = clock();
ctx->frameCount = 0;

#ifdef __linux__
char *xdgRuntimeDir = getenv("XDG_RUNTIME_DIR");

if (xdgRuntimeDir != NULL) {
ctx->saName.sun_family = PF_UNIX;
strcpy(ctx->saName.sun_path, xdgRuntimeDir);
if(xdgRuntimeDir[(strlen(xdgRuntimeDir)-1)] != '/')
strcat(ctx->saName.sun_path, "/");
strcat(ctx->saName.sun_path, "mumble/MumbleOverlayPipe");
} else {
char uid[10];
sprintf(uid, "%d", getuid());
ctx->saName.sun_family = PF_UNIX;
strcpy(ctx->saName.sun_path, "/run/user/");
strcat(ctx->saName.sun_path, uid);
strcat(ctx->saName.sun_path, "/mumble/MumbleOverlayPipe");
}
#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;
}
}
if (home) {
ctx->saName.sun_family = PF_UNIX;
strcpy(ctx->saName.sun_path, home);
strcat(ctx->saName.sun_path, "/MumbleOverlayPipe");
}
#endif
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
4 changes: 4 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
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__();
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_
28 changes: 7 additions & 21 deletions src/mumble/Overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Channel.h"
#include "ClientUser.h"
#include "Database.h"
#include "IPCUtils.h"
#include "MainWindow.h"
#include "OverlayClient.h"
#include "OverlayText.h"
Expand Down Expand Up @@ -238,36 +239,21 @@ void Overlay::createPipe() {
// Allow anyone to access the pipe in order to communicate with the overlay
qlsServer->setSocketOptions(QLocalServer::WorldAccessOption);

QString pipepath;
#ifdef Q_OS_WIN
pipepath = QLatin1String("MumbleOverlayPipe");
#else
{
QString xdgRuntimePath = QProcessEnvironment::systemEnvironment().value(QLatin1String("XDG_RUNTIME_DIR"));
QString mumbleRuntimePath;
if (!xdgRuntimePath.isNull()) {
mumbleRuntimePath = QDir(xdgRuntimePath).absolutePath() + QLatin1String("/mumble/");
} else {
mumbleRuntimePath = QLatin1String("/run/user/") + QString::number(getuid()) + QLatin1String("/mumble/");
}
QDir mumbleRuntimeDir = QDir(mumbleRuntimePath);
mumbleRuntimeDir.mkpath(".");
pipepath = mumbleRuntimeDir.absoluteFilePath(QLatin1String("MumbleOverlayPipe"));
}
const std::string pipepath = Mumble::getAndCreateOverlayPipePath();
QString qPipepath = QString::fromUtf8(pipepath.data(), int(pipepath.size()));

{
QFile f(pipepath);
QFile f(qPipepath);
if (f.exists()) {
qWarning() << "Overlay: Removing old socket on" << pipepath;
qWarning() << "Overlay: Removing old socket on" << qPipepath;
f.remove();
}
}
#endif

if (!qlsServer->listen(pipepath)) {
if (!qlsServer->listen(qPipepath)) {
QMessageBox::warning(nullptr, QLatin1String("Mumble"),
tr("Failed to create communication with overlay at %2: %1. No overlay will be available.")
.arg(qlsServer->errorString().toHtmlEscaped(), pipepath.toHtmlEscaped()),
.arg(qlsServer->errorString().toHtmlEscaped(), qPipepath.toHtmlEscaped()),
QMessageBox::Ok, QMessageBox::NoButton);
} else {
qWarning() << "Overlay: Listening on" << qlsServer->fullServerName();
Expand Down
Loading

0 comments on commit a421e1e

Please sign in to comment.