]> andersk Git - openssh.git/blob - openbsd-compat/bsd-misc.c
- (djm) [auth-pam.c clientloop.c includes.h monitor.c session.c]
[openssh.git] / openbsd-compat / bsd-misc.c
1
2 /*
3  * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "includes.h"
19
20 #include <signal.h>
21
22 #include "xmalloc.h"
23
24 RCSID("$Id$");
25
26 #ifndef HAVE___PROGNAME
27 char *__progname;
28 #endif
29
30 /*
31  * NB. duplicate __progname in case it is an alias for argv[0]
32  * Otherwise it may get clobbered by setproctitle()
33  */
34 char *ssh_get_progname(char *argv0)
35 {
36 #ifdef HAVE___PROGNAME
37         extern char *__progname;
38
39         return xstrdup(__progname);
40 #else
41         char *p;
42
43         if (argv0 == NULL)
44                 return ("unknown");     /* XXX */
45         p = strrchr(argv0, '/');
46         if (p == NULL)
47                 p = argv0;
48         else
49                 p++;
50
51         return (xstrdup(p));
52 #endif
53 }
54
55 #ifndef HAVE_SETLOGIN
56 int setlogin(const char *name)
57 {
58         return (0);
59 }
60 #endif /* !HAVE_SETLOGIN */
61
62 #ifndef HAVE_INNETGR
63 int innetgr(const char *netgroup, const char *host, 
64             const char *user, const char *domain)
65 {
66         return (0);
67 }
68 #endif /* HAVE_INNETGR */
69
70 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
71 int seteuid(uid_t euid)
72 {
73         return (setreuid(-1, euid));
74 }
75 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
76
77 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
78 int setegid(uid_t egid)
79 {
80         return(setresgid(-1, egid, -1));
81 }
82 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
83
84 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
85 const char *strerror(int e)
86 {
87         extern int sys_nerr;
88         extern char *sys_errlist[];
89         
90         if ((e >= 0) && (e < sys_nerr))
91                 return (sys_errlist[e]);
92
93         return ("unlisted error");
94 }
95 #endif
96
97 #ifndef HAVE_UTIMES
98 int utimes(char *filename, struct timeval *tvp)
99 {
100         struct utimbuf ub;
101
102         ub.actime = tvp[0].tv_sec;
103         ub.modtime = tvp[1].tv_sec;
104         
105         return (utime(filename, &ub));
106 }
107 #endif 
108
109 #ifndef HAVE_TRUNCATE
110 int truncate(const char *path, off_t length)
111 {
112         int fd, ret, saverrno;
113
114         fd = open(path, O_WRONLY);
115         if (fd < 0)
116                 return (-1);
117
118         ret = ftruncate(fd, length);
119         saverrno = errno;
120         close(fd);
121         if (ret == -1)
122                 errno = saverrno;
123
124         return(ret);
125 }
126 #endif /* HAVE_TRUNCATE */
127
128 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
129 int nanosleep(const struct timespec *req, struct timespec *rem)
130 {
131         int rc, saverrno;
132         extern int errno;
133         struct timeval tstart, tstop, tremain, time2wait;
134
135         TIMESPEC_TO_TIMEVAL(&time2wait, req)
136         (void) gettimeofday(&tstart, NULL);
137         rc = select(0, NULL, NULL, NULL, &time2wait);
138         if (rc == -1) {
139                 saverrno = errno;
140                 (void) gettimeofday (&tstop, NULL);
141                 errno = saverrno;
142                 tremain.tv_sec = time2wait.tv_sec - 
143                         (tstop.tv_sec - tstart.tv_sec);
144                 tremain.tv_usec = time2wait.tv_usec - 
145                         (tstop.tv_usec - tstart.tv_usec);
146                 tremain.tv_sec += tremain.tv_usec / 1000000L;
147                 tremain.tv_usec %= 1000000L;
148         } else {
149                 tremain.tv_sec = 0;
150                 tremain.tv_usec = 0;
151         }
152         TIMEVAL_TO_TIMESPEC(&tremain, rem)
153
154         return(rc);
155 }
156 #endif
157
158 #ifndef HAVE_TCGETPGRP
159 pid_t
160 tcgetpgrp(int fd)
161 {
162         int ctty_pgrp;
163
164         if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
165                 return(-1);
166         else
167                 return(ctty_pgrp);
168 }
169 #endif /* HAVE_TCGETPGRP */
170
171 #ifndef HAVE_TCSENDBREAK
172 int
173 tcsendbreak(int fd, int duration)
174 {
175 # if defined(TIOCSBRK) && defined(TIOCCBRK)
176         struct timeval sleepytime;
177
178         sleepytime.tv_sec = 0;
179         sleepytime.tv_usec = 400000;
180         if (ioctl(fd, TIOCSBRK, 0) == -1)
181                 return (-1);
182         (void)select(0, 0, 0, 0, &sleepytime);
183         if (ioctl(fd, TIOCCBRK, 0) == -1)
184                 return (-1);
185         return (0);
186 # else
187         return -1;
188 # endif
189 }
190 #endif /* HAVE_TCSENDBREAK */
191
192 mysig_t
193 mysignal(int sig, mysig_t act)
194 {
195 #ifdef HAVE_SIGACTION
196         struct sigaction sa, osa;
197
198         if (sigaction(sig, NULL, &osa) == -1)
199                 return (mysig_t) -1;
200         if (osa.sa_handler != act) {
201                 memset(&sa, 0, sizeof(sa));
202                 sigemptyset(&sa.sa_mask);
203                 sa.sa_flags = 0;
204 #ifdef SA_INTERRUPT
205                 if (sig == SIGALRM)
206                         sa.sa_flags |= SA_INTERRUPT;
207 #endif
208                 sa.sa_handler = act;
209                 if (sigaction(sig, &sa, NULL) == -1)
210                         return (mysig_t) -1;
211         }
212         return (osa.sa_handler);
213 #else
214         #undef signal
215         return (signal(sig, act));
216 #endif
217 }
218
219 #ifndef HAVE_STRDUP
220 char *
221 strdup(const char *str)
222 {
223         size_t len;
224         char *cp;
225
226         len = strlen(str) + 1;
227         cp = malloc(len);
228         if (cp != NULL)
229                 return(memcpy(cp, str, len));
230         return NULL;
231 }
232 #endif
This page took 0.054544 seconds and 5 git commands to generate.