2 dropin.c -- a set of drop-in replacements for libc functions
3 Copyright (C) 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: dropin.c,v 1.1.2.7 2001/11/16 17:36:56 zarq Exp $
25 #include <sys/types.h>
40 Replacement for the daemon() function.
42 The daemon() function is for programs wishing to detach themselves
43 from the controlling terminal and run in the background as system
46 Unless the argument nochdir is non-zero, daemon() changes the
47 current working directory to the root (``/'').
49 Unless the argument noclose is non-zero, daemon() will redirect
50 standard input, standard output and standard error to /dev/null.
52 int daemon(int nochdir, int noclose)
59 /* Check if forking failed */
66 /* If we are the parent, terminate */
70 /* Detach by becoming the new process group leader */
77 /* Change working directory to the root (to avoid keeping mount
84 /* Redirect stdin/out/err to /dev/null */
87 fd = open("/dev/null", O_RDWR);
91 perror("opening /dev/null");
109 #ifndef HAVE_GET_CURRENT_DIR_NAME
111 Replacement for the GNU get_current_dir_name function:
113 get_current_dir_name will malloc(3) an array big enough to hold the
114 current directory name. If the environment variable PWD is set, and
115 its value is correct, then that value will be returned.
117 char *get_current_dir_name(void)
123 /* Start with 100 bytes. If this turns out to be insufficient to
124 contain the working directory, double the size. */
128 errno = 0; /* Success */
129 r = getcwd(buf, size);
130 /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
131 is insufficient to contain the entire working directory. */
132 while(r == NULL && errno == ERANGE)
135 size <<= 1; /* double the size */
137 r = getcwd(buf, size);
144 #ifndef HAVE_ASPRINTF
145 int asprintf(char **buf, const char *fmt, ...)
155 status = vsnprintf (*buf, len, fmt, ap);
159 *buf = xrealloc(*buf, status);
165 status = vsnprintf (*buf, len, fmt, ap);
175 * fake library for ssh
177 * This file is included in getaddrinfo.c and getnameinfo.c.
178 * See getaddrinfo.c and getnameinfo.c.
181 /* $Id: dropin.c,v 1.1.2.7 2001/11/16 17:36:56 zarq Exp $ */
183 /* for old netdb.h */
190 * fake library for ssh
192 * This file includes getaddrinfo(), freeaddrinfo() and gai_strerror().
193 * These funtions are defined in rfc2133.
195 * But these functions are not implemented correctly. The minimum subset
196 * is implemented for ssh use only. For exapmle, this routine assumes
197 * that ai_family is AF_INET. Don't use it for another purpose.
200 #ifndef HAVE_GAI_STRERROR
201 char *gai_strerror(int ecode)
205 return "no address associated with hostname.";
207 return "memory allocation failure.";
209 return "unknown error.";
212 #endif /* !HAVE_GAI_STRERROR */
214 #ifndef HAVE_FREEADDRINFO
215 void freeaddrinfo(struct addrinfo *ai)
217 struct addrinfo *next;
222 } while (NULL != (ai = next));
224 #endif /* !HAVE_FREEADDRINFO */
226 #ifndef HAVE_GETADDRINFO
227 static struct addrinfo *malloc_ai(int port, u_long addr)
231 ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
235 memset(ai, 0, sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
237 ai->ai_addr = (struct sockaddr *)(ai + 1);
238 /* XXX -- ssh doesn't use sa_len */
239 ai->ai_addrlen = sizeof(struct sockaddr_in);
240 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
242 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
243 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
248 int getaddrinfo(const char *hostname, const char *servname,
249 const struct addrinfo *hints, struct addrinfo **res)
251 struct addrinfo *cur, *prev = NULL;
257 port = htons(atoi(servname));
261 if (hints && hints->ai_flags & AI_PASSIVE) {
262 if (NULL != (*res = malloc_ai(port, htonl(0x00000000))))
269 if (NULL != (*res = malloc_ai(port, htonl(0x7f000001))))
275 if (inet_aton(hostname, &in)) {
276 if (NULL != (*res = malloc_ai(port, in.s_addr)))
282 hp = gethostbyname(hostname);
283 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
284 for (i = 0; hp->h_addr_list[i]; i++) {
285 cur = malloc_ai(port, ((struct in_addr *)hp->h_addr_list[i])->s_addr);
304 #endif /* !HAVE_GETADDRINFO */
308 * fake library for ssh
310 * This file includes getnameinfo().
311 * These funtions are defined in rfc2133.
313 * But these functions are not implemented correctly. The minimum subset
314 * is implemented for ssh use only. For exapmle, this routine assumes
315 * that ai_family is AF_INET. Don't use it for another purpose.
318 #ifndef HAVE_GETNAMEINFO
319 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
320 size_t hostlen, char *serv, size_t servlen, int flags)
322 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
327 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
328 if (strlen(tmpserv) >= servlen)
331 strcpy(serv, tmpserv);
335 if (flags & NI_NUMERICHOST) {
336 if (strlen(inet_ntoa(sin->sin_addr)) >= hostlen)
339 strcpy(host, inet_ntoa(sin->sin_addr));
342 hp = gethostbyaddr((char *)&sin->sin_addr,
343 sizeof(struct in_addr), AF_INET);
347 if (strlen(hp->h_name) >= hostlen)
350 strcpy(host, hp->h_name);
356 #endif /* !HAVE_GETNAMEINFO */