]>
Commit | Line | Data |
---|---|---|
419e26e7 | 1 | |
8efc0c15 | 2 | /* |
9cd11896 | 3 | * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> |
bfc9a610 | 4 | * |
9cd11896 | 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. | |
bfc9a610 | 8 | * |
9cd11896 | 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. | |
bfc9a610 | 16 | */ |
8efc0c15 | 17 | |
416ed5a7 | 18 | #include "includes.h" |
b5b88c19 | 19 | |
20 | #include <signal.h> | |
21 | ||
d0104542 | 22 | #include "xmalloc.h" |
8efc0c15 | 23 | |
3cc5d223 | 24 | #ifndef HAVE___PROGNAME |
35cf0057 | 25 | char *__progname; |
26 | #endif | |
27 | ||
d0104542 | 28 | /* |
29 | * NB. duplicate __progname in case it is an alias for argv[0] | |
30 | * Otherwise it may get clobbered by setproctitle() | |
31 | */ | |
fda04d7d | 32 | char *ssh_get_progname(char *argv0) |
260d427b | 33 | { |
34 | #ifdef HAVE___PROGNAME | |
35 | extern char *__progname; | |
36 | ||
d0104542 | 37 | return xstrdup(__progname); |
260d427b | 38 | #else |
39 | char *p; | |
40 | ||
41 | if (argv0 == NULL) | |
9901cb37 | 42 | return ("unknown"); /* XXX */ |
260d427b | 43 | p = strrchr(argv0, '/'); |
44 | if (p == NULL) | |
45 | p = argv0; | |
46 | else | |
47 | p++; | |
d0104542 | 48 | |
9901cb37 | 49 | return (xstrdup(p)); |
260d427b | 50 | #endif |
51 | } | |
52 | ||
a5c9cd31 | 53 | #ifndef HAVE_SETLOGIN |
54 | int setlogin(const char *name) | |
55 | { | |
9901cb37 | 56 | return (0); |
a5c9cd31 | 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 | { | |
9901cb37 | 64 | return (0); |
a5c9cd31 | 65 | } |
66 | #endif /* HAVE_INNETGR */ | |
67 | ||
68 | #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) | |
69 | int seteuid(uid_t euid) | |
70 | { | |
9901cb37 | 71 | return (setreuid(-1, euid)); |
a5c9cd31 | 72 | } |
73 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ | |
819b676f | 74 | |
febd3f8e | 75 | #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) |
76 | int setegid(uid_t egid) | |
77 | { | |
9901cb37 | 78 | return(setresgid(-1, egid, -1)); |
febd3f8e | 79 | } |
80 | #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */ | |
81 | ||
416ed5a7 | 82 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
83 | const char *strerror(int e) | |
819b676f | 84 | { |
416ed5a7 | 85 | extern int sys_nerr; |
86 | extern char *sys_errlist[]; | |
87 | ||
e2bccbcd | 88 | if ((e >= 0) && (e < sys_nerr)) |
9901cb37 | 89 | return (sys_errlist[e]); |
90 | ||
91 | return ("unlisted error"); | |
819b676f | 92 | } |
416ed5a7 | 93 | #endif |
89c7e31c | 94 | |
95 | #ifndef HAVE_UTIMES | |
96 | int utimes(char *filename, struct timeval *tvp) | |
97 | { | |
98 | struct utimbuf ub; | |
99 | ||
6c266dc3 | 100 | ub.actime = tvp[0].tv_sec; |
101 | ub.modtime = tvp[1].tv_sec; | |
89c7e31c | 102 | |
9901cb37 | 103 | return (utime(filename, &ub)); |
89c7e31c | 104 | } |
105 | #endif | |
f25cd32c | 106 | |
107 | #ifndef HAVE_TRUNCATE | |
9901cb37 | 108 | int truncate(const char *path, off_t length) |
f25cd32c | 109 | { |
110 | int fd, ret, saverrno; | |
111 | ||
112 | fd = open(path, O_WRONLY); | |
113 | if (fd < 0) | |
9901cb37 | 114 | return (-1); |
f25cd32c | 115 | |
116 | ret = ftruncate(fd, length); | |
117 | saverrno = errno; | |
9901cb37 | 118 | close(fd); |
f25cd32c | 119 | if (ret == -1) |
120 | errno = saverrno; | |
9901cb37 | 121 | |
f25cd32c | 122 | return(ret); |
123 | } | |
124 | #endif /* HAVE_TRUNCATE */ | |
125 | ||
5271b55c | 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 | } | |
5271b55c | 154 | #endif |
155 | ||
c5d3dd1b | 156 | #ifndef HAVE_TCGETPGRP |
157 | pid_t | |
158 | tcgetpgrp(int fd) | |
159 | { | |
0644eab4 | 160 | int ctty_pgrp; |
c5d3dd1b | 161 | |
f29c37a9 | 162 | if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1) |
c5d3dd1b | 163 | return(-1); |
164 | else | |
165 | return(ctty_pgrp); | |
166 | } | |
167 | #endif /* HAVE_TCGETPGRP */ | |
168 | ||
5af25b1d | 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 */ | |
f00d1f78 | 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 | |
cc1102cb | 212 | #undef signal |
f00d1f78 | 213 | return (signal(sig, act)); |
214 | #endif | |
215 | } | |
35fc74ed | 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) | |
bd2a0801 | 227 | return(memcpy(cp, str, len)); |
228 | return NULL; | |
35fc74ed | 229 | } |
230 | #endif |