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