-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFAC(ipc): extract IPCUtils for Socket and OverlayPipe
- Loading branch information
1 parent
6120405
commit fedacf0
Showing
10 changed files
with
240 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2007-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 __unix__ | ||
# include <pwd.h> | ||
# include <stdio.h> | ||
# include <stdlib.h> | ||
# include <sys/stat.h> | ||
# include <sys/types.h> | ||
# include <unistd.h> | ||
#else | ||
# include <direct.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 __unix__ | ||
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, "/"); | ||
#else | ||
path = strdup(""); | ||
#endif | ||
return path; | ||
} | ||
|
||
char *getAndCreateOverlayPipePath__() { | ||
char *runtimePath = getRuntimePath__(); | ||
char *path = NULL; | ||
if (runtimePath == NULL) | ||
return runtimePath; | ||
#ifdef __unix__ | ||
mkdir(runtimePath, 0755); | ||
#else | ||
_mkdir(runtimePath); | ||
#endif | ||
size_t n = 1; | ||
n += strlen(runtimePath); | ||
n += strlen("MumbleOverlayPipe"); | ||
if ((path = malloc(n + 1)) == NULL) | ||
return path; | ||
#if !defined __linux__ && !defined _WIN32 | ||
strcat(path, "."); | ||
#endif | ||
strcpy(path, runtimePath); | ||
strcat(path, "MumbleOverlayPipe"); | ||
free(runtimePath); | ||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2015-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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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; | ||
return mumbleRuntimePath; | ||
} | ||
|
||
const std::string getAndCreateOverlayPipePath(void) { | ||
char *c_pipepath = getAndCreateOverlayPipePath__(); | ||
if (c_pipepath == NULL) | ||
return ""; | ||
std::string pipepath = 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.