1/* 2 * compat.c 3 * 4 * Compatibility functions for different OSes 5 * 6 * $Id: compat.c,v 1.6 2005/08/22 00:48:34 quozl Exp $ 7 */ 8 9#if HAVE_CONFIG_H 10#include "config.h" 11#endif 12 13#include "compat.h" 14 15#ifndef HAVE_STRLCPY 16#include <string.h> 17#include <stdio.h> 18 19void strlcpy(char *dst, const char *src, size_t size) 20{ 21 strncpy(dst, src, size - 1); 22 dst[size - 1] = '\0'; 23} 24#endif 25 26#ifndef HAVE_MEMMOVE 27void *memmove(void *dst, const void *src, size_t size) 28{ 29 bcopy(src, dst, size); 30 return dst; 31} 32#endif 33 34#ifndef HAVE_SETPROCTITLE 35#include "inststr.h" 36#endif 37 38#define __USE_BSD 1 39#include <stdarg.h> 40#include <stdio.h> 41 42void my_setproctitle(int argc, char **argv, const char *format, ...) { 43 char proctitle[64]; 44 va_list parms; 45 va_start(parms, format); 46 vsnprintf(proctitle, sizeof(proctitle), format, parms); 47 48#ifndef HAVE_SETPROCTITLE 49 inststr(argc, argv, proctitle); 50#else 51 setproctitle(proctitle); 52#endif 53 va_end(parms); 54} 55 56/* signal to pipe delivery implementation */ 57#include <unistd.h> 58#include <fcntl.h> 59#include <signal.h> 60 61/* pipe private to process */ 62static int sigpipe[2]; 63 64/* create a signal pipe, returns 0 for success, -1 with errno for failure */ 65int sigpipe_create() 66{ 67 int rc; 68 69 rc = pipe(sigpipe); 70 if (rc < 0) return rc; 71 72 fcntl(sigpipe[0], F_SETFD, FD_CLOEXEC); 73 fcntl(sigpipe[1], F_SETFD, FD_CLOEXEC); 74 75#ifdef O_NONBLOCK 76#define FLAG_TO_SET O_NONBLOCK 77#else 78#ifdef SYSV 79#define FLAG_TO_SET O_NDELAY 80#else /* BSD */ 81#define FLAG_TO_SET FNDELAY 82#endif 83#endif 84 85 rc = fcntl(sigpipe[1], F_GETFL); 86 if (rc != -1) 87 rc = fcntl(sigpipe[1], F_SETFL, rc | FLAG_TO_SET); 88 if (rc < 0) return rc; 89 return 0; 90#undef FLAG_TO_SET 91} 92 93/* generic handler for signals, writes signal number to pipe */ 94void sigpipe_handler(int signum) 95{ 96 write(sigpipe[1], &signum, sizeof(signum)); 97 signal(signum, sigpipe_handler); 98} 99 100/* assign a signal number to the pipe */ 101void sigpipe_assign(int signum) 102{ 103 struct sigaction sa; 104 105 memset(&sa, 0, sizeof(sa)); 106 sa.sa_handler = sigpipe_handler; 107 sigaction(signum, &sa, NULL); 108} 109 110/* return the signal pipe read file descriptor for select(2) */ 111int sigpipe_fd() 112{ 113 return sigpipe[0]; 114} 115 116/* read and return the pending signal from the pipe */ 117int sigpipe_read() 118{ 119 int signum; 120 read(sigpipe[0], &signum, sizeof(signum)); 121 return signum; 122} 123 124void sigpipe_close() 125{ 126 close(sigpipe[0]); 127 close(sigpipe[1]); 128} 129 130