Centralise calls to fcntl into functions that carefully check the
[sgt/putty] / unix / uxmisc.c
CommitLineData
f7f27309 1/*
2 * PuTTY miscellaneous Unix stuff
3 */
4
db9b7dce 5#include <fcntl.h>
f7f27309 6#include <stdio.h>
2ac3322e 7#include <stdlib.h>
c6940f12 8#include <assert.h>
f9520103 9#include <errno.h>
d0912d1f 10#include <unistd.h>
92fb929a 11#include <time.h>
f7f27309 12#include <sys/time.h>
799dfcfa 13#include <sys/types.h>
14#include <pwd.h>
f7f27309 15
9a30e26b 16#include "putty.h"
17
f7f27309 18unsigned long getticks(void)
19{
fe7b25bd 20 /*
92fb929a 21 * We want to use milliseconds rather than the microseconds or
22 * nanoseconds given by the underlying clock functions, because we
23 * need a decent number of them to fit into a 32-bit word so it
24 * can be used for keepalives.
fe7b25bd 25 */
92fb929a 26#if defined HAVE_CLOCK_GETTIME && defined HAVE_DECL_CLOCK_MONOTONIC
27 {
28 /* Use CLOCK_MONOTONIC if available, so as to be unconfused if
29 * the system clock changes. */
30 struct timespec ts;
31 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
32 return ts.tv_sec * TICKSPERSEC +
39439af6 33 ts.tv_nsec / (1000000000 / TICKSPERSEC);
92fb929a 34 }
35#endif
36 {
37 struct timeval tv;
38 gettimeofday(&tv, NULL);
39 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
40 }
f7f27309 41}
9a30e26b 42
962468d4 43Filename *filename_from_str(const char *str)
9a30e26b 44{
962468d4 45 Filename *ret = snew(Filename);
46 ret->path = dupstr(str);
9a30e26b 47 return ret;
48}
49
962468d4 50Filename *filename_copy(const Filename *fn)
51{
52 return filename_from_str(fn->path);
53}
54
9fab77dc 55const char *filename_to_str(const Filename *fn)
9a30e26b 56{
9fab77dc 57 return fn->path;
9a30e26b 58}
59
962468d4 60int filename_equal(const Filename *f1, const Filename *f2)
61{
62 return !strcmp(f1->path, f2->path);
63}
64
65int filename_is_null(const Filename *fn)
66{
67 return !fn->path[0];
68}
69
70void filename_free(Filename *fn)
9a30e26b 71{
962468d4 72 sfree(fn->path);
73 sfree(fn);
9a30e26b 74}
75
962468d4 76int filename_serialise(const Filename *f, void *vdata)
77{
78 char *data = (char *)vdata;
79 int len = strlen(f->path) + 1; /* include trailing NUL */
80 if (data) {
81 strcpy(data, f->path);
82 }
83 return len;
84}
85Filename *filename_deserialise(void *vdata, int maxsize, int *used)
9a30e26b 86{
962468d4 87 char *data = (char *)vdata;
88 char *end;
89 end = memchr(data, '\0', maxsize);
90 if (!end)
91 return NULL;
92 end++;
93 *used = end - data;
94 return filename_from_str(data);
9a30e26b 95}
d0912d1f 96
97#ifdef DEBUG
98static FILE *debug_fp = NULL;
99
100void dputs(char *buf)
101{
102 if (!debug_fp) {
103 debug_fp = fopen("debug.log", "w");
104 }
105
106 write(1, buf, strlen(buf));
107
108 fputs(buf, debug_fp);
109 fflush(debug_fp);
110}
111#endif
799dfcfa 112
113char *get_username(void)
114{
115 struct passwd *p;
116 uid_t uid = getuid();
117 char *user, *ret = NULL;
118
119 /*
120 * First, find who we think we are using getlogin. If this
121 * agrees with our uid, we'll go along with it. This should
122 * allow sharing of uids between several login names whilst
123 * coping correctly with people who have su'ed.
124 */
125 user = getlogin();
126 setpwent();
127 if (user)
128 p = getpwnam(user);
129 else
130 p = NULL;
131 if (p && p->pw_uid == uid) {
132 /*
133 * The result of getlogin() really does correspond to
134 * our uid. Fine.
135 */
136 ret = user;
137 } else {
138 /*
139 * If that didn't work, for whatever reason, we'll do
140 * the simpler version: look up our uid in the password
141 * file and map it straight to a name.
142 */
143 p = getpwuid(uid);
144 if (!p)
145 return NULL;
146 ret = p->pw_name;
147 }
148 endpwent();
149
150 return dupstr(ret);
151}
2285d016 152
153/*
154 * Display the fingerprints of the PGP Master Keys to the user.
155 * (This is here rather than in uxcons because it's appropriate even for
156 * Unix GUI apps.)
157 */
158void pgp_fingerprints(void)
159{
160 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
161 "be used to establish a trust path from this executable to another\n"
162 "one. See the manual for more information.\n"
163 "(Note: these fingerprints have nothing to do with SSH!)\n"
164 "\n"
165 "PuTTY Master Key (RSA), 1024-bit:\n"
166 " " PGP_RSA_MASTER_KEY_FP "\n"
167 "PuTTY Master Key (DSA), 1024-bit:\n"
168 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
169}
db9b7dce 170
171/*
f9520103 172 * Set and clear fcntl options on a file descriptor. We don't
173 * realistically expect any of these operations to fail (the most
174 * plausible error condition is EBADF, but we always believe ourselves
175 * to be passing a valid fd so even that's an assertion-fail sort of
176 * response), so we don't make any effort to return sensible error
177 * codes to the caller - we just log to standard error and die
178 * unceremoniously. However, nonblock and no_nonblock do return the
179 * previous state of O_NONBLOCK.
db9b7dce 180 */
f9520103 181void cloexec(int fd) {
db9b7dce 182 int fdflags;
183
184 fdflags = fcntl(fd, F_GETFD);
f9520103 185 if (fdflags < 0) {
186 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
187 exit(1);
188 }
189 if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
190 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
191 exit(1);
192 }
193}
194void noncloexec(int fd) {
195 int fdflags;
196
197 fdflags = fcntl(fd, F_GETFD);
198 if (fdflags < 0) {
199 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
200 exit(1);
201 }
202 if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
203 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
204 exit(1);
205 }
206}
207int nonblock(int fd) {
208 int fdflags;
209
210 fdflags = fcntl(fd, F_GETFL);
211 if (fdflags < 0) {
212 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
213 exit(1);
214 }
215 if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
216 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
217 exit(1);
218 }
219
220 return fdflags & O_NONBLOCK;
221}
222int no_nonblock(int fd) {
223 int fdflags;
224
225 fdflags = fcntl(fd, F_GETFL);
226 if (fdflags < 0) {
227 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
228 exit(1);
229 }
230 if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
231 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
232 exit(1);
233 }
234
235 return fdflags & O_NONBLOCK;
db9b7dce 236}
c6940f12 237
962468d4 238FILE *f_open(const Filename *filename, char const *mode, int is_private)
c6940f12 239{
240 if (!is_private) {
962468d4 241 return fopen(filename->path, mode);
c6940f12 242 } else {
deb0e1cf 243 int fd;
b078de41 244 assert(mode[0] == 'w'); /* is_private is meaningless for read,
245 and tricky for append */
e41356ff 246 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
c6940f12 247 if (fd < 0)
248 return NULL;
249 return fdopen(fd, mode);
250 }
251}
ae62eaeb 252
253FontSpec *fontspec_new(const char *name)
254{
255 FontSpec *f = snew(FontSpec);
256 f->name = dupstr(name);
257 return f;
258}
259FontSpec *fontspec_copy(const FontSpec *f)
260{
261 return fontspec_new(f->name);
262}
263void fontspec_free(FontSpec *f)
264{
265 sfree(f->name);
266 sfree(f);
267}
268int fontspec_serialise(FontSpec *f, void *data)
269{
270 int len = strlen(f->name);
271 if (data)
272 strcpy(data, f->name);
273 return len + 1; /* include trailing NUL */
274}
275FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
276{
277 char *data = (char *)vdata;
278 char *end = memchr(data, '\0', maxsize);
279 if (!end)
280 return NULL;
281 *used = end - data + 1;
282 return fontspec_new(data);
283}