blob: 3a30b6e4fff87c270462955d815581f37d62891d [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller98225c22004-02-17 16:49:41 +11002 * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
Damien Miller040f3832000-04-03 14:50:43 +10003 *
Damien Miller98225c22004-02-17 16:49:41 +11004 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
Damien Miller040f3832000-04-03 14:50:43 +10007 *
Damien Miller98225c22004-02-17 16:49:41 +11008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller040f3832000-04-03 14:50:43 +100015 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Damien Miller11fa2cc2000-08-16 10:35:58 +100017#include "includes.h"
Damien Millera8ed44b2003-01-10 09:53:12 +110018#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Darren Tucker03669a32004-08-13 18:37:21 +100020RCSID("$Id: bsd-misc.c,v 1.24 2004/08/13 08:37:21 dtucker Exp $");
Darren Tuckerba6de952004-07-17 14:07:42 +100021
Darren Tucker03669a32004-08-13 18:37:21 +100022#ifndef HAVE___PROGNAME
Darren Tuckerba6de952004-07-17 14:07:42 +100023char *__progname;
24#endif
Damien Millere9cf3572001-02-09 12:55:35 +110025
Damien Millera8ed44b2003-01-10 09:53:12 +110026/*
27 * NB. duplicate __progname in case it is an alias for argv[0]
28 * Otherwise it may get clobbered by setproctitle()
29 */
Damien Miller59d3d5b2003-08-22 09:34:41 +100030char *ssh_get_progname(char *argv0)
Ben Lindstrom49a79c02000-11-17 03:47:20 +000031{
32#ifdef HAVE___PROGNAME
33 extern char *__progname;
34
Damien Millera8ed44b2003-01-10 09:53:12 +110035 return xstrdup(__progname);
Ben Lindstrom49a79c02000-11-17 03:47:20 +000036#else
37 char *p;
38
39 if (argv0 == NULL)
Damien Miller31741252003-05-19 00:13:38 +100040 return ("unknown"); /* XXX */
Ben Lindstrom49a79c02000-11-17 03:47:20 +000041 p = strrchr(argv0, '/');
42 if (p == NULL)
43 p = argv0;
44 else
45 p++;
Damien Millera8ed44b2003-01-10 09:53:12 +110046
Damien Miller31741252003-05-19 00:13:38 +100047 return (xstrdup(p));
Ben Lindstrom49a79c02000-11-17 03:47:20 +000048#endif
49}
50
Damien Millere72b7af1999-12-30 15:08:44 +110051#ifndef HAVE_SETLOGIN
52int setlogin(const char *name)
53{
Damien Miller31741252003-05-19 00:13:38 +100054 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110055}
56#endif /* !HAVE_SETLOGIN */
57
58#ifndef HAVE_INNETGR
59int innetgr(const char *netgroup, const char *host,
60 const char *user, const char *domain)
61{
Damien Miller31741252003-05-19 00:13:38 +100062 return (0);
Damien Millere72b7af1999-12-30 15:08:44 +110063}
64#endif /* HAVE_INNETGR */
65
66#if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
67int seteuid(uid_t euid)
68{
Damien Miller31741252003-05-19 00:13:38 +100069 return (setreuid(-1, euid));
Damien Millere72b7af1999-12-30 15:08:44 +110070}
71#endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
Damien Millerecbb26d2000-07-15 14:59:14 +100072
Kevin Stevescb17e992001-04-09 14:50:52 +000073#if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
74int setegid(uid_t egid)
75{
Damien Miller31741252003-05-19 00:13:38 +100076 return(setresgid(-1, egid, -1));
Kevin Stevescb17e992001-04-09 14:50:52 +000077}
78#endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
79
Damien Miller11fa2cc2000-08-16 10:35:58 +100080#if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
81const char *strerror(int e)
Damien Millerecbb26d2000-07-15 14:59:14 +100082{
Damien Miller11fa2cc2000-08-16 10:35:58 +100083 extern int sys_nerr;
84 extern char *sys_errlist[];
85
Ben Lindstrom46e55aa2001-03-13 23:38:20 +000086 if ((e >= 0) && (e < sys_nerr))
Damien Miller31741252003-05-19 00:13:38 +100087 return (sys_errlist[e]);
88
89 return ("unlisted error");
Damien Millerecbb26d2000-07-15 14:59:14 +100090}
Damien Miller11fa2cc2000-08-16 10:35:58 +100091#endif
Ben Lindstrom42202bc2001-01-15 02:34:37 +000092
93#ifndef HAVE_UTIMES
94int utimes(char *filename, struct timeval *tvp)
95{
96 struct utimbuf ub;
97
Ben Lindstrom100d5862002-07-08 21:09:41 +000098 ub.actime = tvp[0].tv_sec;
99 ub.modtime = tvp[1].tv_sec;
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000100
Damien Miller31741252003-05-19 00:13:38 +1000101 return (utime(filename, &ub));
Ben Lindstrom42202bc2001-01-15 02:34:37 +0000102}
103#endif
Tim Rice4bd2a192002-05-07 19:51:31 -0700104
105#ifndef HAVE_TRUNCATE
Damien Miller31741252003-05-19 00:13:38 +1000106int truncate(const char *path, off_t length)
Tim Rice4bd2a192002-05-07 19:51:31 -0700107{
108 int fd, ret, saverrno;
109
110 fd = open(path, O_WRONLY);
111 if (fd < 0)
Damien Miller31741252003-05-19 00:13:38 +1000112 return (-1);
Tim Rice4bd2a192002-05-07 19:51:31 -0700113
114 ret = ftruncate(fd, length);
115 saverrno = errno;
Damien Miller31741252003-05-19 00:13:38 +1000116 close(fd);
Tim Rice4bd2a192002-05-07 19:51:31 -0700117 if (ret == -1)
118 errno = saverrno;
Damien Miller31741252003-05-19 00:13:38 +1000119
Tim Rice4bd2a192002-05-07 19:51:31 -0700120 return(ret);
121}
122#endif /* HAVE_TRUNCATE */
123
Ben Lindstrom837461b2002-06-12 16:57:14 +0000124#if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP)
125/*
126 * Cygwin setgroups should be a noop.
127 */
128int
Ben Lindstrom0e23ebc2002-06-13 21:34:57 +0000129setgroups(size_t size, const gid_t *list)
Ben Lindstrom837461b2002-06-12 16:57:14 +0000130{
Damien Miller31741252003-05-19 00:13:38 +1000131 return (0);
Ben Lindstrom837461b2002-06-12 16:57:14 +0000132}
133#endif
134
Tim Rice4e4dc562003-03-18 10:21:40 -0800135#if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
136int nanosleep(const struct timespec *req, struct timespec *rem)
137{
138 int rc, saverrno;
139 extern int errno;
140 struct timeval tstart, tstop, tremain, time2wait;
141
142 TIMESPEC_TO_TIMEVAL(&time2wait, req)
143 (void) gettimeofday(&tstart, NULL);
144 rc = select(0, NULL, NULL, NULL, &time2wait);
145 if (rc == -1) {
146 saverrno = errno;
147 (void) gettimeofday (&tstop, NULL);
148 errno = saverrno;
149 tremain.tv_sec = time2wait.tv_sec -
150 (tstop.tv_sec - tstart.tv_sec);
151 tremain.tv_usec = time2wait.tv_usec -
152 (tstop.tv_usec - tstart.tv_usec);
153 tremain.tv_sec += tremain.tv_usec / 1000000L;
154 tremain.tv_usec %= 1000000L;
155 } else {
156 tremain.tv_sec = 0;
157 tremain.tv_usec = 0;
158 }
159 TIMEVAL_TO_TIMESPEC(&tremain, rem)
160
161 return(rc);
162}
Tim Rice4e4dc562003-03-18 10:21:40 -0800163#endif
164
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000165#ifndef HAVE_TCGETPGRP
166pid_t
167tcgetpgrp(int fd)
168{
Darren Tucker048737c2003-08-02 23:33:48 +1000169 int ctty_pgrp;
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000170
Darren Tuckerbdf571b2003-08-03 00:36:16 +1000171 if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
Darren Tucker2e9c9cf2003-08-02 23:31:42 +1000172 return(-1);
173 else
174 return(ctty_pgrp);
175}
176#endif /* HAVE_TCGETPGRP */
177
Darren Tuckerf38ea772003-08-13 20:48:07 +1000178#ifndef HAVE_TCSENDBREAK
179int
180tcsendbreak(int fd, int duration)
181{
182# if defined(TIOCSBRK) && defined(TIOCCBRK)
183 struct timeval sleepytime;
184
185 sleepytime.tv_sec = 0;
186 sleepytime.tv_usec = 400000;
187 if (ioctl(fd, TIOCSBRK, 0) == -1)
188 return (-1);
189 (void)select(0, 0, 0, 0, &sleepytime);
190 if (ioctl(fd, TIOCCBRK, 0) == -1)
191 return (-1);
192 return (0);
193# else
194 return -1;
195# endif
196}
197#endif /* HAVE_TCSENDBREAK */
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000198
Darren Tucker60bd4092004-06-25 14:03:34 +1000199#ifndef HAVE_CLOSEFROM
200int
201closefrom(int fd)
202{
203 int i, result = 0, err = 0;
204
205 for (i = fd; i < 128; i++)
206 if (close(i) != 0) {
207 err = errno;
208 result = -1;
209 }
210 errno = err;
211 return result;
212}
213#endif /* HAVE_CLOSEFROM */
214
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000215mysig_t
216mysignal(int sig, mysig_t act)
217{
218#ifdef HAVE_SIGACTION
219 struct sigaction sa, osa;
220
221 if (sigaction(sig, NULL, &osa) == -1)
222 return (mysig_t) -1;
223 if (osa.sa_handler != act) {
224 memset(&sa, 0, sizeof(sa));
225 sigemptyset(&sa.sa_mask);
226 sa.sa_flags = 0;
227#ifdef SA_INTERRUPT
228 if (sig == SIGALRM)
229 sa.sa_flags |= SA_INTERRUPT;
230#endif
231 sa.sa_handler = act;
232 if (sigaction(sig, &sa, NULL) == -1)
233 return (mysig_t) -1;
234 }
235 return (osa.sa_handler);
236#else
Ben Lindstrom563eb992003-12-18 00:34:06 +0000237 #undef signal
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000238 return (signal(sig, act));
239#endif
240}