Skip to content

Commit 5b12bf2

Browse files
committed
Fix crashlog being inconsistent type in main/errorhandler.
Refactor to remove global variable usage so similar thing won't happen in the future.
1 parent 3744061 commit 5b12bf2

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

src/server/errorhandler.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <cstdint>
1515
#include <cstring>
1616
#include <ctime>
17-
#include <string>
17+
#include <filesystem>
1818
#include "fcitx-utils/fs.h"
1919
#include "fcitx-utils/standardpaths.h"
2020
#include "fcitx-utils/unixfd.h"
@@ -31,15 +31,30 @@
3131
#define MINIMAL_BUFFER_SIZE 256
3232
#define BACKTRACE_SIZE 32
3333

34-
extern int selfpipe[2];
35-
extern std::string crashlog;
34+
namespace fcitx {
35+
36+
static int signalPipe;
37+
static std::filesystem::path crashlog;
3638

3739
struct MinimalBuffer {
3840
char buffer[MINIMAL_BUFFER_SIZE];
3941
int offset;
4042
};
4143

42-
void SetMyExceptionHandler() {
44+
static void OnException(int signo);
45+
46+
void SetMyExceptionHandler(int pipeFd) {
47+
48+
auto userDir =
49+
StandardPaths::global().userDirectory(StandardPathsType::PkgConfig);
50+
if (!userDir.empty()) {
51+
if (fs::makePath(userDir)) {
52+
crashlog = userDir / "crash.log";
53+
}
54+
}
55+
56+
signalPipe = pipeFd;
57+
4358
int signo;
4459

4560
for (signo = SIGHUP; signo < SIGUNUSED; signo++) {
@@ -115,7 +130,7 @@ static inline void _write_buffer(int fd, const MinimalBuffer *buffer) {
115130
void OnException(int signo) {
116131
if (signo == SIGCHLD) {
117132
uint8_t sig = (signo & 0xff);
118-
fcitx::fs::safeWrite(selfpipe[1], &sig, 1);
133+
fcitx::fs::safeWrite(signalPipe, &sig, 1);
119134
signal(signo, OnException);
120135
return;
121136
}
@@ -180,10 +195,12 @@ void OnException(int signo) {
180195
if (signo < 0xff) {
181196
sig = (uint8_t)(signo & 0xff);
182197
}
183-
fcitx::fs::safeWrite(selfpipe[1], &sig, 1);
198+
fcitx::fs::safeWrite(signalPipe, &sig, 1);
184199
signal(signo, OnException);
185200
} break;
186201
}
187202
}
188203

204+
} // namespace fcitx
205+
189206
// kate: indent-mode cstyle; space-indent on; indent-width 0;

src/server/errorhandler.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,15 @@
1616
#ifndef SIGUNUSED
1717
#define SIGUNUSED 29
1818
#endif
19-
/* ***********************************************************
20-
// Data structures
21-
// *********************************************************** */
2219

23-
/* ***********************************************************
24-
// Functions
25-
// *********************************************************** */
20+
namespace fcitx {
2621

2722
//
2823
// Set Posix Signal Handler
2924
//
3025
//
31-
void SetMyExceptionHandler(void);
26+
void SetMyExceptionHandler(int pipeFd);
3227

33-
//
34-
// Process Posix signal
35-
//
36-
void OnException(int signo);
28+
} // namespace fcitx
3729

3830
#endif

src/server/main.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,17 @@
88
#include <sys/stat.h>
99
#include <clocale>
1010
#include <cstdio>
11-
#include <cstdlib>
1211
#include <exception>
1312
#include <filesystem>
1413
#include <iostream>
1514
#include <string>
16-
#include <utility>
1715
#include <vector>
1816
#include "fcitx-utils/environ.h"
19-
#include "fcitx-utils/fs.h"
2017
#include "fcitx-utils/log.h"
2118
#include "fcitx-utils/misc.h"
2219
#include "fcitx-utils/misc_p.h"
2320
#include "fcitx-utils/standardpath.h"
2421
#include "fcitx-utils/standardpaths.h"
25-
#include "fcitx-utils/stringutils.h"
2622
#include "fcitx/addonfactory.h"
2723
#include "fcitx/addoninstance.h"
2824
#include "fcitx/addonloader.h"
@@ -31,8 +27,6 @@
3127
#include "errorhandler.h"
3228

3329
using namespace fcitx;
34-
int selfpipe[2];
35-
std::filesystem::path crashlog;
3630

3731
FCITX_DEFINE_STATIC_ADDON_REGISTRY(getStaticAddon)
3832
#ifdef ENABLE_KEYBOARD
@@ -43,7 +37,8 @@ int main(int argc, char *argv[]) {
4337
umask(077);
4438
StandardPath::global().syncUmask();
4539
StandardPaths::global().syncUmask();
46-
if (safePipe(selfpipe) < 0) {
40+
int selfPipe[2];
41+
if (safePipe(selfPipe) < 0) {
4742
fprintf(stderr, "Could not create self-pipe.\n");
4843
return 1;
4944
}
@@ -54,15 +49,7 @@ int main(int argc, char *argv[]) {
5449
return 1;
5550
}
5651

57-
auto userDir =
58-
StandardPaths::global().userDirectory(StandardPathsType::PkgConfig);
59-
if (!userDir.empty()) {
60-
if (fs::makePath(userDir)) {
61-
crashlog = userDir / "crash.log";
62-
}
63-
}
64-
65-
SetMyExceptionHandler();
52+
SetMyExceptionHandler(selfPipe[1]);
6653

6754
setlocale(LC_ALL, "");
6855

@@ -74,7 +61,7 @@ int main(int argc, char *argv[]) {
7461
FCITX_LOG_IF(Info, isInFlatpak()) << "Running inside flatpak.";
7562
Instance instance(argc, argv);
7663
instance.setBinaryMode();
77-
instance.setSignalPipe(selfpipe[0]);
64+
instance.setSignalPipe(selfPipe[0]);
7865
instance.addonManager().registerDefaultLoader(&getStaticAddon());
7966

8067
ret = instance.exec();

0 commit comments

Comments
 (0)