]> andersk Git - openssh.git/blob - openbsd-compat/bsd-misc.c
- (djm) [acss.c auth-krb5.c auth-options.c auth-pam.c auth-shadow.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 <string.h>
21 #include <signal.h>
22
23 #include "xmalloc.h"
24
25 #ifndef HAVE___PROGNAME
26 char *__progname;
27 #endif
28
29 /*
30  * NB. duplicate __progname in case it is an alias for argv[0]
31  * Otherwise it may get clobbered by setproctitle()
32  */
33 char *ssh_get_progname(char *argv0)
34 {
35 #ifdef HAVE___PROGNAME
36         extern char *__progname;
37
38         return xstrdup(__progname);
39 #else
40         char *p;
41
42         if (argv0 == NULL)
43                 return ("unknown");     /* XXX */
44         p = strrchr(argv0, '/');
45         if (p == NULL)
46                 p = argv0;
47         else
48                 p++;
49
50         return (xstrdup(p));
51 #endif
52 }
53
54 #ifndef HAVE_SETLOGIN
55 int setlogin(const char *name)
56 {
57         return (0);
58 }
59 #endif /* !HAVE_SETLOGIN */
60
61 #ifndef HAVE_INNETGR
62 int innetgr(const char *netgroup, const char *host, 
63             const char *user, const char *domain)
64 {
65         return (0);
66 }
67 #endif /* HAVE_INNETGR */
68
69 #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
70 int seteuid(uid_t euid)
71 {
72         return (setreuid(-1, euid));
73 }
74 #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */
75
76 #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID)
77 int setegid(uid_t egid)
78 {
79         return(setresgid(-1, egid, -1));
80 }
81 #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */
82
83 #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR)
84 const char *strerror(int e)
85 {
86         extern int sys_nerr;
87         extern char *sys_errlist[];
88         
89         if ((e >= 0) && (e < sys_nerr))
90                 return (sys_errlist[e]);
91
92         return ("unlisted error");
93 }
94 #endif
95
96 #ifndef HAVE_UTIMES
97 int utimes(char *filename, struct timeval *tvp)
98 {
99         struct utimbuf ub;
100
101         ub.actime = tvp[0].tv_sec;
102         ub.modtime = tvp[1].tv_sec;
103         
104         return (utime(filename, &ub));
105 }
106 #endif 
107
108 #ifndef HAVE_TRUNCATE
109 int truncate(const char *path, off_t length)
110 {
111         int fd, ret, saverrno;
112
113         fd = open(path, O_WRONLY);
114         if (fd < 0)
115                 return (-1);
116
117         ret = ftruncate(fd, length);
118         saverrno = errno;
119         close(fd);
120         if (ret == -1)
121                 errno = saverrno;
122
123         return(ret);
124 }
125 #endif /* HAVE_TRUNCATE */
126
127 #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP)
128 int nanosleep(const struct timespec *req, struct timespec *rem)
129 {
130         int rc, saverrno;
131         extern int errno;
132         struct timeval tstart, tstop, tremain, time2wait;
133
134         TIMESPEC_TO_TIMEVAL(&time2wait, req)
135         (void) gettimeofday(&tstart, NULL);
136         rc = select(0, NULL, NULL, NULL, &time2wait);
137         if (rc == -1) {
138                 saverrno = errno;
139                 (void) gettimeofday (&tstop, NULL);
140                 errno = saverrno;
141                 tremain.tv_sec = time2wait.tv_sec - 
142                         (tstop.tv_sec - tstart.tv_sec);
143                 tremain.tv_usec = time2wait.tv_usec - 
144                         (tstop.tv_usec - tstart.tv_usec);
145                 tremain.tv_sec += tremain.tv_usec / 1000000L;
146                 tremain.tv_usec %= 1000000L;
147         } else {
148                 tremain.tv_sec = 0;
149                 tremain.tv_usec = 0;
150         }
151         TIMEVAL_TO_TIMESPEC(&tremain, rem)
152
153         return(rc);
154 }
155 #endif
156
157 #ifndef HAVE_TCGETPGRP
158 pid_t
159 tcgetpgrp(int fd)
160 {
161         int ctty_pgrp;
162
163         if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1)
164                 return(-1);
165         else
166                 return(ctty_pgrp);
167 }
168 #endif /* HAVE_TCGETPGRP */
169
170 #ifndef HAVE_TCSENDBREAK
171 int
172 tcsendbreak(int fd, int duration)
173 {
174 # if defined(TIOCSBRK) && defined(TIOCCBRK)
175         struct timeval sleepytime;
176
177         sleepytime.tv_sec = 0;
178         sleepytime.tv_usec = 400000;
179         if (ioctl(fd, TIOCSBRK, 0) == -1)
180                 return (-1);
181         (void)select(0, 0, 0, 0, &sleepytime);
182         if (ioctl(fd, TIOCCBRK, 0) == -1)
183                 return (-1);
184         return (0);
185 # else
186         return -1;
187 # endif
188 }
189 #endif /* HAVE_TCSENDBREAK */
190
191 mysig_t
192 mysignal(int sig, mysig_t act)
193 {
194 #ifdef HAVE_SIGACTION
195         struct sigaction sa, osa;
196
197         if (sigaction(sig, NULL, &osa) == -1)
198                 return (mysig_t) -1;
199         if (osa.sa_handler != act) {
200                 memset(&sa, 0, sizeof(sa));
201                 sigemptyset(&sa.sa_mask);
202                 sa.sa_flags = 0;
203 #ifdef SA_INTERRUPT
204                 if (sig == SIGALRM)
205                         sa.sa_flags |= SA_INTERRUPT;
206 #endif
207                 sa.sa_handler = act;
208                 if (sigaction(sig, &sa, NULL) == -1)
209                         return (mysig_t) -1;
210         }
211         return (osa.sa_handler);
212 #else
213         #undef signal
214         return (signal(sig, act));
215 #endif
216 }
217
218 #ifndef HAVE_STRDUP
219 char *
220 strdup(const char *str)
221 {
222         size_t len;
223         char *cp;
224
225         len = strlen(str) + 1;
226         cp = malloc(len);
227         if (cp != NULL)
228                 return(memcpy(cp, str, len));
229         return NULL;
230 }
231 #endif
This page took 0.054028 seconds and 5 git commands to generate.