]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
9cd11896 | 2 | * Copyright (c) 1999-2004 Damien Miller <djm@mindrot.org> |
bfc9a610 | 3 | * |
9cd11896 | 4 | * 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. | |
bfc9a610 | 7 | * |
9cd11896 | 8 | * 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. | |
bfc9a610 | 15 | */ |
8efc0c15 | 16 | |
416ed5a7 | 17 | #include "includes.h" |
d0104542 | 18 | #include "xmalloc.h" |
8efc0c15 | 19 | |
0b202697 | 20 | RCSID("$Id$"); |
21 | ||
d0104542 | 22 | /* |
23 | * NB. duplicate __progname in case it is an alias for argv[0] | |
24 | * Otherwise it may get clobbered by setproctitle() | |
25 | */ | |
fda04d7d | 26 | char *ssh_get_progname(char *argv0) |
260d427b | 27 | { |
28 | #ifdef HAVE___PROGNAME | |
29 | extern char *__progname; | |
30 | ||
d0104542 | 31 | return xstrdup(__progname); |
260d427b | 32 | #else |
33 | char *p; | |
34 | ||
35 | if (argv0 == NULL) | |
9901cb37 | 36 | return ("unknown"); /* XXX */ |
260d427b | 37 | p = strrchr(argv0, '/'); |
38 | if (p == NULL) | |
39 | p = argv0; | |
40 | else | |
41 | p++; | |
d0104542 | 42 | |
9901cb37 | 43 | return (xstrdup(p)); |
260d427b | 44 | #endif |
45 | } | |
46 | ||
a5c9cd31 | 47 | #ifndef HAVE_SETLOGIN |
48 | int setlogin(const char *name) | |
49 | { | |
9901cb37 | 50 | return (0); |
a5c9cd31 | 51 | } |
52 | #endif /* !HAVE_SETLOGIN */ | |
53 | ||
54 | #ifndef HAVE_INNETGR | |
55 | int innetgr(const char *netgroup, const char *host, | |
56 | const char *user, const char *domain) | |
57 | { | |
9901cb37 | 58 | return (0); |
a5c9cd31 | 59 | } |
60 | #endif /* HAVE_INNETGR */ | |
61 | ||
62 | #if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) | |
63 | int seteuid(uid_t euid) | |
64 | { | |
9901cb37 | 65 | return (setreuid(-1, euid)); |
a5c9cd31 | 66 | } |
67 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ | |
819b676f | 68 | |
febd3f8e | 69 | #if !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) |
70 | int setegid(uid_t egid) | |
71 | { | |
9901cb37 | 72 | return(setresgid(-1, egid, -1)); |
febd3f8e | 73 | } |
74 | #endif /* !defined(HAVE_SETEGID) && defined(HAVE_SETRESGID) */ | |
75 | ||
416ed5a7 | 76 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
77 | const char *strerror(int e) | |
819b676f | 78 | { |
416ed5a7 | 79 | extern int sys_nerr; |
80 | extern char *sys_errlist[]; | |
81 | ||
e2bccbcd | 82 | if ((e >= 0) && (e < sys_nerr)) |
9901cb37 | 83 | return (sys_errlist[e]); |
84 | ||
85 | return ("unlisted error"); | |
819b676f | 86 | } |
416ed5a7 | 87 | #endif |
89c7e31c | 88 | |
89 | #ifndef HAVE_UTIMES | |
90 | int utimes(char *filename, struct timeval *tvp) | |
91 | { | |
92 | struct utimbuf ub; | |
93 | ||
6c266dc3 | 94 | ub.actime = tvp[0].tv_sec; |
95 | ub.modtime = tvp[1].tv_sec; | |
89c7e31c | 96 | |
9901cb37 | 97 | return (utime(filename, &ub)); |
89c7e31c | 98 | } |
99 | #endif | |
f25cd32c | 100 | |
101 | #ifndef HAVE_TRUNCATE | |
9901cb37 | 102 | int truncate(const char *path, off_t length) |
f25cd32c | 103 | { |
104 | int fd, ret, saverrno; | |
105 | ||
106 | fd = open(path, O_WRONLY); | |
107 | if (fd < 0) | |
9901cb37 | 108 | return (-1); |
f25cd32c | 109 | |
110 | ret = ftruncate(fd, length); | |
111 | saverrno = errno; | |
9901cb37 | 112 | close(fd); |
f25cd32c | 113 | if (ret == -1) |
114 | errno = saverrno; | |
9901cb37 | 115 | |
f25cd32c | 116 | return(ret); |
117 | } | |
118 | #endif /* HAVE_TRUNCATE */ | |
119 | ||
246446cd | 120 | #if !defined(HAVE_SETGROUPS) && defined(SETGROUPS_NOOP) |
121 | /* | |
122 | * Cygwin setgroups should be a noop. | |
123 | */ | |
124 | int | |
f2d02e44 | 125 | setgroups(size_t size, const gid_t *list) |
246446cd | 126 | { |
9901cb37 | 127 | return (0); |
246446cd | 128 | } |
129 | #endif | |
130 | ||
5271b55c | 131 | #if !defined(HAVE_NANOSLEEP) && !defined(HAVE_NSLEEP) |
132 | int nanosleep(const struct timespec *req, struct timespec *rem) | |
133 | { | |
134 | int rc, saverrno; | |
135 | extern int errno; | |
136 | struct timeval tstart, tstop, tremain, time2wait; | |
137 | ||
138 | TIMESPEC_TO_TIMEVAL(&time2wait, req) | |
139 | (void) gettimeofday(&tstart, NULL); | |
140 | rc = select(0, NULL, NULL, NULL, &time2wait); | |
141 | if (rc == -1) { | |
142 | saverrno = errno; | |
143 | (void) gettimeofday (&tstop, NULL); | |
144 | errno = saverrno; | |
145 | tremain.tv_sec = time2wait.tv_sec - | |
146 | (tstop.tv_sec - tstart.tv_sec); | |
147 | tremain.tv_usec = time2wait.tv_usec - | |
148 | (tstop.tv_usec - tstart.tv_usec); | |
149 | tremain.tv_sec += tremain.tv_usec / 1000000L; | |
150 | tremain.tv_usec %= 1000000L; | |
151 | } else { | |
152 | tremain.tv_sec = 0; | |
153 | tremain.tv_usec = 0; | |
154 | } | |
155 | TIMEVAL_TO_TIMESPEC(&tremain, rem) | |
156 | ||
157 | return(rc); | |
158 | } | |
5271b55c | 159 | #endif |
160 | ||
c5d3dd1b | 161 | #ifndef HAVE_TCGETPGRP |
162 | pid_t | |
163 | tcgetpgrp(int fd) | |
164 | { | |
0644eab4 | 165 | int ctty_pgrp; |
c5d3dd1b | 166 | |
f29c37a9 | 167 | if (ioctl(fd, TIOCGPGRP, &ctty_pgrp) == -1) |
c5d3dd1b | 168 | return(-1); |
169 | else | |
170 | return(ctty_pgrp); | |
171 | } | |
172 | #endif /* HAVE_TCGETPGRP */ | |
173 | ||
5af25b1d | 174 | #ifndef HAVE_TCSENDBREAK |
175 | int | |
176 | tcsendbreak(int fd, int duration) | |
177 | { | |
178 | # if defined(TIOCSBRK) && defined(TIOCCBRK) | |
179 | struct timeval sleepytime; | |
180 | ||
181 | sleepytime.tv_sec = 0; | |
182 | sleepytime.tv_usec = 400000; | |
183 | if (ioctl(fd, TIOCSBRK, 0) == -1) | |
184 | return (-1); | |
185 | (void)select(0, 0, 0, 0, &sleepytime); | |
186 | if (ioctl(fd, TIOCCBRK, 0) == -1) | |
187 | return (-1); | |
188 | return (0); | |
189 | # else | |
190 | return -1; | |
191 | # endif | |
192 | } | |
193 | #endif /* HAVE_TCSENDBREAK */ | |
f00d1f78 | 194 | |
195 | mysig_t | |
196 | mysignal(int sig, mysig_t act) | |
197 | { | |
198 | #ifdef HAVE_SIGACTION | |
199 | struct sigaction sa, osa; | |
200 | ||
201 | if (sigaction(sig, NULL, &osa) == -1) | |
202 | return (mysig_t) -1; | |
203 | if (osa.sa_handler != act) { | |
204 | memset(&sa, 0, sizeof(sa)); | |
205 | sigemptyset(&sa.sa_mask); | |
206 | sa.sa_flags = 0; | |
207 | #ifdef SA_INTERRUPT | |
208 | if (sig == SIGALRM) | |
209 | sa.sa_flags |= SA_INTERRUPT; | |
210 | #endif | |
211 | sa.sa_handler = act; | |
212 | if (sigaction(sig, &sa, NULL) == -1) | |
213 | return (mysig_t) -1; | |
214 | } | |
215 | return (osa.sa_handler); | |
216 | #else | |
cc1102cb | 217 | #undef signal |
f00d1f78 | 218 | return (signal(sig, act)); |
219 | #endif | |
220 | } |