1 /***************************************************************************
2 open_syslog_on_stderr.c - description
5 copyright : (C) 2003,2004,2005 by Noberasco Michele
6 e-mail : noberasco.gnu@disi.unige.it
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
26 ***************************************************************************/
35 #include <sys/types.h>
40 /* this is the thread that will actually read
41 * from our redirected stderr, forwarding to syslog
43 void syslog_writer(char *filename
)
45 FILE *fp
= fopen(filename
, "r");
49 { /* we write this to stdout as stderr is probably compromised at this point */
50 printf("wmpower: syslog_writer thread: fatal error: unable to hook to main thread's stderr!\n");
54 /* inititalize syslog writer */
55 openlog("wmpower", 0, LOG_USER
);
57 /* we remove it so that noone will tamper */
64 while (fgets(buf
, 1023, fp
))
65 syslog(LOG_INFO
, "%s", buf
);
72 * Redirect writes to sterr on syslog...
73 * we achieve this by starting a new thread, creating a file
74 * that the father will write to, and the child read from,
75 * rewriting it to syslog. Also, we unlink() the file as soon
76 * as both father and child have it open, so that no other
77 * process will be able to tamper with it.
79 void open_syslog_on_stderr(void)
85 filename
= (char*) malloc(19*sizeof(char));
89 fprintf(stderr
, "wmpower: fatal error: failed allocating memory!\n");
93 strcpy(filename
, "/tmp/wmpowerXXXXXX");
95 fd
= mkstemp(filename
);
98 fprintf(stderr
, "wmpower: fatal error: could not create temporary file!\n");
102 /* set file mode to 600 */
103 if (chmod(filename
, S_IRUSR
|S_IWUSR
))
105 fprintf(stderr
, "wmpower: fatal error: cannot chmod() file!\n");
109 if (!freopen(filename
, "w", stderr
))
111 fprintf(stderr
, "wmpower: fatal error: unable to redirect stderr!\n");
115 /* close the file descriptor as we have no need for it */
118 /* spawn our syslog_writer baby */
119 if (pthread_create(&thread
, NULL
, (void*)(&syslog_writer
), filename
))
121 fprintf(stderr
, "wmpower: fatal error: failed to create syslog-writer thread!\n");