Ticket #1882: mythtv-0.19.housekeeper.patch.txt

File mythtv-0.19.housekeeper.patch.txt, 3.2 KB (added by mrsam@…, 19 years ago)

Patch to myth 0.19

Line 
1--- programs/mythbackend/housekeeper.cpp.housekeeper 2005-12-27 00:20:47.000000000 -0500
2+++ programs/mythbackend/housekeeper.cpp 2006-05-22 23:43:48.000000000 -0400
3@@ -2,6 +2,7 @@
4 #include <sys/types.h>
5 #include <sys/wait.h>
6 #include <signal.h>
7+#include <fcntl.h>
8 #include <unistd.h>
9 #include <qsqldatabase.h>
10 #include <qsqlquery.h>
11@@ -17,15 +18,10 @@
12
13 #include "libmyth/mythcontext.h"
14 #include "libmyth/mythdbcon.h"
15+#include "libmyth/exitcodes.h"
16
17 bool HouseKeeper_filldb_running = false;
18
19-void reapChild(int /* sig */)
20-{
21- (void)wait(0);
22- HouseKeeper_filldb_running = false;
23-}
24-
25 HouseKeeper::HouseKeeper(bool runthread, bool master)
26 {
27 isMaster = master;
28@@ -276,6 +272,13 @@
29 }
30 }
31
32+struct housekeep_thread_info {
33+ const char *cmd;
34+ int cmd_started;
35+};
36+
37+static void *doRunFillDatabase(void *);
38+
39 void HouseKeeper::runFillDatabase()
40 {
41 QString command;
42@@ -291,15 +294,94 @@
43 else
44 command = QString("%1 %2 >>%3 2>&1").arg(mfpath).arg(mfarg).arg(mflog);
45
46- signal(SIGCHLD, &reapChild);
47+ pthread_t housekeep_thread;
48+
49+ pthread_attr_t attr;
50+ pthread_attr_init(&attr);
51+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
52+
53+ struct housekeep_thread_info housekeep_info;
54+
55+ housekeep_info.cmd=command.ascii();
56+ housekeep_info.cmd_started=0;
57+
58+ pthread_create(&housekeep_thread, &attr, doRunFillDatabase,
59+ &housekeep_info);
60+ pthread_attr_destroy(&attr);
61+
62+ while (!housekeep_info.cmd_started)
63+ {
64+ sleep(1);
65+ }
66+}
67+
68+static void filldb_done(void *)
69+{
70+ HouseKeeper_filldb_running = false;
71+}
72+
73+static void *doRunFillDatabase(void *arg)
74+{
75+ struct housekeep_thread_info *hinfo=
76+ (struct housekeep_thread_info *)arg;
77+
78+ pid_t p=fork();
79+
80+ if (p < 0)
81+ {
82+ hinfo->cmd_started=1;
83+
84+ VERBOSE(VB_IMPORTANT,
85+ QString("doRunFillDatabase(): Error, fork() failed because %1")
86+ .arg(strerror(errno)));
87+ return 0;
88+ }
89+
90+ if (p == 0)
91+ {
92+ for(int i = 3; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
93+ close(i);
94+
95+ /* Attach stdin to /dev/null */
96+ int fd = open("/dev/null", O_RDONLY);
97+ dup2(fd, 0);
98+
99+ if (fd != 0)
100+ close(fd);
101+
102+ execl("/bin/sh", "sh", "-c", hinfo->cmd, NULL);
103+ if (errno)
104+ {
105+ VERBOSE(VB_IMPORTANT,
106+ QString("doRunFillDatabase(): Error, execl() failed because %1")
107+ .arg(strerror(errno)));
108+ }
109+ _exit(MYTHSYSTEM__EXIT__EXECL_ERROR);
110+ }
111+
112 HouseKeeper_filldb_running = true;
113- if (fork() == 0)
114+
115+ pthread_cleanup_push(filldb_done, NULL);
116+
117+ hinfo->cmd_started=1;
118+
119+ int status;
120+
121+ if (waitpid(p, &status, 0) < 0)
122 {
123- for(int i = 3; i < sysconf(_SC_OPEN_MAX) - 1; ++i)
124- close(i);
125- system(command.ascii());
126- _exit(0); // this exit is ok, non-error exit from system command.
127+ VERBOSE(VB_IMPORTANT,
128+ QString("doRunFillDatabase(): Error, waitpid() failed because %1")
129+ .arg(strerror(errno)));
130 }
131+ else if (status != 0)
132+ {
133+ VERBOSE(VB_IMPORTANT,
134+ QString("doRunFillDatabase(): mythfilldatabase terminated with exit code %1")
135+ .arg(status));
136+ }
137+
138+ pthread_cleanup_pop(1);
139+ return 0;
140 }
141
142 void HouseKeeper::CleanupMyOldRecordings(void)