wmdots: Rename local sincos() to avoid conflicting types warning.
[dockapps.git] / wmpower / src / open_syslog_on_stderr.c
blob8e4b44096a48dda95938d845a76c529c9a2cdf82
1 /***************************************************************************
2 open_syslog_on_stderr.c - description
3 -------------------
4 begin : Feb 10 2003
5 copyright : (C) 2003,2004,2005 by Noberasco Michele
6 e-mail : noberasco.gnu@disi.unige.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
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. *
15 * *
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. *
20 * *
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. *
25 * *
26 ***************************************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <pthread.h>
34 #include <syslog.h>
35 #include <sys/types.h>
36 #include <sys/stat.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");
46 char buf[1024];
48 if (!fp)
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");
51 abort();
54 /* inititalize syslog writer */
55 openlog("wmpower", 0, LOG_USER);
57 /* we remove it so that noone will tamper */
58 unlink(filename);
60 while (1)
62 fflush(NULL);
64 while (fgets(buf, 1023, fp))
65 syslog(LOG_INFO, "%s", buf);
67 sleep(1);
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)
81 pthread_t thread;
82 char *filename;
83 int fd;
85 filename = (char*) malloc(19*sizeof(char));
87 if (!filename)
89 fprintf(stderr, "wmpower: fatal error: failed allocating memory!\n");
90 abort();
93 strcpy(filename, "/tmp/wmpowerXXXXXX");
95 fd = mkstemp(filename);
96 if (fd == -1)
98 fprintf(stderr, "wmpower: fatal error: could not create temporary file!\n");
99 abort();
102 /* set file mode to 600 */
103 if (chmod(filename, S_IRUSR|S_IWUSR))
105 fprintf(stderr, "wmpower: fatal error: cannot chmod() file!\n");
106 abort();
109 if (!freopen(filename, "w", stderr))
111 fprintf(stderr, "wmpower: fatal error: unable to redirect stderr!\n");
112 abort();
115 /* close the file descriptor as we have no need for it */
116 close(fd);
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");
122 abort();