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 $
15 #define PROGRAM_NAME "pptp"
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) \
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); \
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)
43 /*** print a message to syslog ************************************************/
44 void _log(const char *func
, const char *file
, int line
, const char *format
, ...)
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
, ...)
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
, ...)
64 fprintf(stderr
, "%s\n", string
);
65 syslog(LOG_CRIT
, "%s", string
);
69 /*** connect a file to a file descriptor **************************************/
70 int file2fd(const char *path
, const char *mode
, int fd
)
74 file
= fopen(path
, mode
);
75 if (file
!= NULL
&& dup2(fileno(file
), fd
) != -1)
77 if (file
) fclose(file
);
81 /* signal to pipe delivery implementation */
87 /* pipe private to process */
88 static int sigpipe
[2];
90 /* create a signal pipe, returns 0 for success, -1 with errno for failure */
96 if (rc
< 0) return rc
;
98 fcntl(sigpipe
[0], F_SETFD
, FD_CLOEXEC
);
99 fcntl(sigpipe
[1], F_SETFD
, FD_CLOEXEC
);
102 #define FLAG_TO_SET O_NONBLOCK
105 #define FLAG_TO_SET O_NDELAY
107 #define FLAG_TO_SET FNDELAY
111 rc
= fcntl(sigpipe
[1], F_GETFL
);
113 rc
= fcntl(sigpipe
[1], F_SETFL
, rc
| FLAG_TO_SET
);
114 if (rc
< 0) return rc
;
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
)
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) */
142 /* read and return the pending signal from the pipe */
146 read(sigpipe
[0], &signum
, sizeof(signum
));