Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / pptp-client / util.c
blob02bfd7a74afd6ee08d0a25bc32f78cdb6c9e18ac
1 /* util.c ....... error message utilities.
2 * C. Scott Ananian <cananian@alumni.princeton.edu>
4 * $Id: util.c,v 1.11 2005/08/22 00:49:48 quozl Exp $
5 */
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <syslog.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include "util.h"
14 #ifndef PROGRAM_NAME
15 #define PROGRAM_NAME "pptp"
16 #endif
18 /* implementation of log_string, defined as extern in util.h */
19 char *log_string = "anon";
21 static void open_log(void) __attribute__ ((constructor));
22 static void close_log(void) __attribute__ ((destructor));
24 #define MAKE_STRING(label) \
25 va_list ap; \
26 char buf[256], string[256]; \
27 va_start(ap, format); \
28 vsnprintf(buf, sizeof(buf), format, ap); \
29 snprintf(string, sizeof(string), "%s", buf); \
30 va_end(ap)
32 /*** open log *****************************************************************/
33 static void open_log(void) {
34 openlog(PROGRAM_NAME, LOG_PID, LOG_DAEMON);
37 /*** close log ****************************************************************/
38 static void close_log(void)
40 closelog();
43 /*** print a message to syslog ************************************************/
44 void _log(const char *func, const char *file, int line, const char *format, ...)
46 if (log_level > 0) {
47 MAKE_STRING("log");
48 syslog(LOG_NOTICE, "%s", string);
52 /*** print a warning to syslog ************************************************/
53 void _warn(const char *func, const char *file, int line, const char *format, ...)
55 MAKE_STRING("warn");
56 fprintf(stderr, "%s\n", string);
57 syslog(LOG_WARNING, "%s", string);
60 /*** print a fatal warning to syslog and exit *********************************/
61 void _fatal(const char *func, const char *file, int line, const char *format, ...)
63 MAKE_STRING("fatal");
64 fprintf(stderr, "%s\n", string);
65 syslog(LOG_CRIT, "%s", string);
66 exit(1);
69 /*** connect a file to a file descriptor **************************************/
70 int file2fd(const char *path, const char *mode, int fd)
72 int ok = 0;
73 FILE *file = NULL;
74 file = fopen(path, mode);
75 if (file != NULL && dup2(fileno(file), fd) != -1)
76 ok = 1;
77 if (file) fclose(file);
78 return ok;
81 /* signal to pipe delivery implementation */
82 #include <unistd.h>
83 #include <fcntl.h>
84 #include <signal.h>
85 #include <string.h>
87 /* pipe private to process */
88 static int sigpipe[2];
90 /* create a signal pipe, returns 0 for success, -1 with errno for failure */
91 int sigpipe_create()
93 int rc;
95 rc = pipe(sigpipe);
96 if (rc < 0) return rc;
98 fcntl(sigpipe[0], F_SETFD, FD_CLOEXEC);
99 fcntl(sigpipe[1], F_SETFD, FD_CLOEXEC);
101 #ifdef O_NONBLOCK
102 #define FLAG_TO_SET O_NONBLOCK
103 #else
104 #ifdef SYSV
105 #define FLAG_TO_SET O_NDELAY
106 #else /* BSD */
107 #define FLAG_TO_SET FNDELAY
108 #endif
109 #endif
111 rc = fcntl(sigpipe[1], F_GETFL);
112 if (rc != -1)
113 rc = fcntl(sigpipe[1], F_SETFL, rc | FLAG_TO_SET);
114 if (rc < 0) return rc;
115 return 0;
116 #undef FLAG_TO_SET
119 /* generic handler for signals, writes signal number to pipe */
120 void sigpipe_handler(int signum)
122 write(sigpipe[1], &signum, sizeof(signum));
123 signal(signum, sigpipe_handler);
126 /* assign a signal number to the pipe */
127 void sigpipe_assign(int signum)
129 struct sigaction sa;
131 memset(&sa, 0, sizeof(sa));
132 sa.sa_handler = sigpipe_handler;
133 sigaction(signum, &sa, NULL);
136 /* return the signal pipe read file descriptor for select(2) */
137 int sigpipe_fd()
139 return sigpipe[0];
142 /* read and return the pending signal from the pipe */
143 int sigpipe_read()
145 int signum;
146 read(sigpipe[0], &signum, sizeof(signum));
147 return signum;
150 void sigpipe_close()
152 close(sigpipe[0]);
153 close(sigpipe[1]);