Skip to content

Commit 4ec36dd

Browse files
committed
squashme: Add SIGTERM handling
asdf
1 parent 4dc9e96 commit 4ec36dd

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

frontend/OBSApp.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extern string opt_starting_profile;
7777

7878
#ifndef _WIN32
7979
int OBSApp::sigintFd[2];
80+
int OBSApp::sigtermFd[2];
8081
#endif
8182

8283
// GPU hint exports for AMD/NVIDIA laptops
@@ -928,9 +929,14 @@ OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
928929
socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd);
929930
snInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
930931
connect(snInt, &QSocketNotifier::activated, this, &OBSApp::ProcessSigInt);
931-
#else
932-
connect(qApp, &QGuiApplication::commitDataRequest, this, &OBSApp::commitData, Qt::DirectConnection);
932+
933+
/* Handle SIGTERM */
934+
socketpair(AF_UNIX, SOCK_STREAM, 0, sigtermFd);
935+
snTerm = new QSocketNotifier(sigtermFd[1], QSocketNotifier::Read, this);
936+
connect(snTerm, &QSocketNotifier::activated, this, &OBSApp::ProcessSigTerm);
933937
#endif
938+
connect(qApp, &QGuiApplication::commitDataRequest, this, &OBSApp::commitData, Qt::DirectConnection);
939+
934940
if (multi) {
935941
crashHandler_ = std::make_unique<OBS::CrashHandler>();
936942
} else {
@@ -1810,6 +1816,14 @@ void OBSApp::SigIntSignalHandler(int s)
18101816
char a = 1;
18111817
send(sigintFd[0], &a, sizeof(a), 0);
18121818
}
1819+
1820+
void OBSApp::SigTermSignalHandler(int s)
1821+
{
1822+
UNUSED_PARAMETER(s);
1823+
1824+
char a = 1;
1825+
send(sigtermFd[0], &a, sizeof(a), 0);
1826+
}
18131827
#endif
18141828

18151829
void OBSApp::ProcessSigInt(void)
@@ -1828,15 +1842,33 @@ void OBSApp::ProcessSigInt(void)
18281842
#endif
18291843
}
18301844

1831-
#ifdef _WIN32
1845+
void OBSApp::ProcessSigTerm(void)
1846+
{
1847+
#ifndef _WIN32
1848+
char tmp;
1849+
recv(sigtermFd[1], &tmp, sizeof(tmp), 0);
1850+
1851+
OBSBasic *main = OBSBasic::Get();
1852+
if (main) {
1853+
main->saveAll();
1854+
}
1855+
1856+
quit();
1857+
#endif
1858+
}
1859+
18321860
void OBSApp::commitData(QSessionManager &manager)
18331861
{
18341862
OBSBasic *main = OBSBasic::Get();
18351863
if (main) {
18361864
main->saveAll();
1865+
1866+
if (manager.allowsInteraction() && main->shouldPromptForClose()) {
1867+
blog(LOG_INFO, "[OBSApp] SessionManager::cancel()");
1868+
manager.cancel();
1869+
}
18371870
}
18381871
}
1839-
#endif
18401872

18411873
void OBSApp::applicationShutdown() noexcept
18421874
{
@@ -1848,6 +1880,10 @@ void OBSApp::applicationShutdown() noexcept
18481880
delete snInt;
18491881
close(sigintFd[0]);
18501882
close(sigintFd[1]);
1883+
1884+
delete snTerm;
1885+
close(sigtermFd[0]);
1886+
close(sigtermFd[1]);
18511887
#endif
18521888

18531889
#ifdef __APPLE__

frontend/OBSApp.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ class OBSApp : public QApplication {
125125
#ifndef _WIN32
126126
static int sigintFd[2];
127127
QSocketNotifier *snInt = nullptr;
128-
#else
129-
private slots:
130-
void commitData(QSessionManager &manager);
128+
129+
static int sigtermFd[2];
130+
QSocketNotifier *snTerm = nullptr;
131131
#endif
132132

133133
private slots:
134+
void commitData(QSessionManager &manager);
134135
void addLogLine(int logLevel, const QString &message);
135136
void themeFileChanged(const QString &);
136137
void applicationShutdown() noexcept;
@@ -220,6 +221,7 @@ private slots:
220221
inline void PopUITranslation() { translatorHooks.pop_front(); }
221222
#ifndef _WIN32
222223
static void SigIntSignalHandler(int);
224+
static void SigTermSignalHandler(int);
223225
#endif
224226

225227
void loadAppModules(struct obs_module_failure_info &mfi);
@@ -230,6 +232,7 @@ private slots:
230232
public slots:
231233
void Exec(VoidFunc func);
232234
void ProcessSigInt();
235+
void ProcessSigTerm();
233236

234237
signals:
235238
void logLineAdded(int logLevel, const QString &message);

frontend/obs-main.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,22 @@ int main(int argc, char *argv[])
858858
#ifndef _WIN32
859859
signal(SIGPIPE, SIG_IGN);
860860

861-
struct sigaction sig_handler;
861+
struct sigaction sigint_handler;
862862

863-
sig_handler.sa_handler = OBSApp::SigIntSignalHandler;
864-
sigemptyset(&sig_handler.sa_mask);
865-
sig_handler.sa_flags = 0;
863+
sigint_handler.sa_handler = OBSApp::SigIntSignalHandler;
864+
sigemptyset(&sigint_handler.sa_mask);
865+
sigint_handler.sa_flags = 0;
866866

867-
sigaction(SIGINT, &sig_handler, NULL);
867+
sigaction(SIGINT, &sigint_handler, NULL);
868+
869+
struct sigaction sigterm_handler;
870+
871+
sigterm_handler.sa_handler = OBSApp::SigTermSignalHandler;
872+
sigemptyset(&sigterm_handler.sa_mask);
873+
sigterm_handler.sa_flags = 0;
874+
875+
sigaction(SIGTERM, &sigterm_handler, NULL);
876+
sigaction(SIGHUP, &sigterm_handler, NULL);
868877

869878
/* Block SIGPIPE in all threads, this can happen if a thread calls write on
870879
a closed pipe. */

frontend/widgets/OBSBasic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ OBSBasic::OBSBasic(QWidget *parent) : OBSMainWindow(parent), undo_s(ui), ui(new
558558
connect(ui->scenes, &SceneTree::scenesReordered, []() { OBSProjector::UpdateMultiviewProjectors(); });
559559

560560
connect(App(), &OBSApp::StyleChanged, this, [this]() { OnEvent(OBS_FRONTEND_EVENT_THEME_CHANGED); });
561+
connect(App(), &OBSApp::aboutToQuit, this, &OBSBasic::closeWindow);
561562

562563
QActionGroup *actionGroup = new QActionGroup(this);
563564
actionGroup->addAction(ui->actionSceneListMode);

frontend/widgets/OBSBasic.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ public slots:
329329
void SetDisplayAffinity(QWindow *window);
330330

331331
void saveAll();
332+
bool shouldPromptForClose();
332333
inline bool isClosing() { return isClosing_; }
333334

334335
protected:
335336
bool isReadyToClose();
336-
bool shouldPromptForClose();
337337
bool promptToClose();
338338
void closeWindow();
339339

0 commit comments

Comments
 (0)