Ticket #33: mythtv.logrotate.diff
File mythtv.logrotate.diff, 2.5 KB (added by , 19 years ago) |
---|
-
mythtv/programs/mythbackend/main.cpp
40 40 QString pidfile; 41 41 QString lockfile_location; 42 42 HouseKeeper *housekeeping = NULL; 43 char *logfile_c = NULL; 43 44 44 45 bool setupTVs(bool ismaster, bool &error) 45 46 { … … 168 169 unlink(pidfile.ascii()); 169 170 170 171 unlink(lockfile_location.ascii()); 172 173 signal(SIGHUP, SIG_DFL); 174 175 if (logfile_c) 176 free(logfile_c); 171 177 } 172 178 179 int log_rotate(int report_error) 180 { 181 /* http://www.gossamer-threads.com/lists/mythtv/dev/110113 */ 182 183 int new_logfd = open(logfile_c, O_WRONLY|O_CREAT|O_APPEND|O_SYNC, 0664); 184 if (new_logfd < 0) 185 { 186 // If we can't open the new logfile, send data to /dev/null 187 if (report_error) 188 { 189 cerr << "cannot open logfile " << logfile_c << endl; 190 return -1; 191 } 192 new_logfd = open("/dev/null", O_WRONLY); 193 if (new_logfd < 0) 194 { 195 // There's not much we can do, so punt. 196 return -1; 197 } 198 } 199 while (dup2(new_logfd, 1) < 0 && errno == EINTR) ; 200 while (dup2(new_logfd, 2) < 0 && errno == EINTR) ; 201 while (close(new_logfd) < 0 && errno == EINTR) ; 202 return 0; 203 } 204 205 void log_rotate_handler(int) 206 { 207 log_rotate(0); 208 } 209 210 173 211 int main(int argc, char **argv) 174 212 { 175 213 for(int i = 3; i < sysconf(_SC_OPEN_MAX) - 1; ++i) … … 392 430 } 393 431 } 394 432 395 int logfd = -1; 396 397 if (logfile != "") 433 if (logfile != "" && (logfile_c = strdup(logfile.ascii())) != NULL) 398 434 { 399 logfd = open(logfile.ascii(), O_WRONLY|O_CREAT|O_APPEND|O_SYNC, 0664); 400 401 if (logfd < 0) 402 { 403 perror(logfile.ascii()); 404 cerr << "Error opening logfile\n"; 405 return BACKEND_EXIT_OPENING_LOGFILE_ERROR; 406 } 435 if (log_rotate(1) < 0) 436 cerr << "cannot open logfile; using stdout/stderr" << endl; 437 else 438 signal(SIGHUP, &log_rotate_handler); 407 439 } 408 440 409 441 ofstream pidfs; … … 435 467 pidfs.close(); 436 468 } 437 469 438 if (logfd != -1)439 {440 // Send stdout and stderr to the logfile441 dup2(logfd, 1);442 dup2(logfd, 2);443 444 // Close the unduplicated logfd445 if (logfd != 1 && logfd != 2)446 close(logfd);447 }448 449 470 gContext = NULL; 450 471 gContext = new MythContext(MYTH_BINARY_VERSION); 451 472 if (!gContext->Init(false))