]> granicus.if.org Git - strace/blob - util.c
Fix a few spacing style issues
[strace] / util.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *                     Linux for s390 port by D.J. Barrow
8  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9  * Copyright (c) 1999-2017 The strace developers.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "defs.h"
36 #include <sys/param.h>
37 #include <fcntl.h>
38 #include <stdarg.h>
39 #ifdef HAVE_SYS_XATTR_H
40 # include <sys/xattr.h>
41 #endif
42 #include <sys/uio.h>
43 #include <asm/unistd.h>
44
45 #include "scno.h"
46 #include "regs.h"
47 #include "ptrace.h"
48
49 int
50 string_to_uint_ex(const char *const str, char **const endptr,
51                   const unsigned int max_val, const char *const accepted_ending)
52 {
53         char *end;
54         long val;
55
56         if (!*str)
57                 return -1;
58
59         errno = 0;
60         val = strtol(str, &end, 10);
61
62         if (str == end || val < 0 || (unsigned long) val > max_val
63             || (val == LONG_MAX && errno == ERANGE))
64                 return -1;
65
66         if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
67                 return -1;
68
69         if (endptr)
70                 *endptr = end;
71
72         return (int) val;
73 }
74
75 int
76 string_to_uint(const char *const str)
77 {
78         return string_to_uint_upto(str, INT_MAX);
79 }
80
81 int
82 tv_nz(const struct timeval *a)
83 {
84         return a->tv_sec || a->tv_usec;
85 }
86
87 int
88 tv_cmp(const struct timeval *a, const struct timeval *b)
89 {
90         if (a->tv_sec < b->tv_sec
91             || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
92                 return -1;
93         if (a->tv_sec > b->tv_sec
94             || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
95                 return 1;
96         return 0;
97 }
98
99 double
100 tv_float(const struct timeval *tv)
101 {
102         return tv->tv_sec + tv->tv_usec/1000000.0;
103 }
104
105 void
106 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
107 {
108         tv->tv_sec = a->tv_sec + b->tv_sec;
109         tv->tv_usec = a->tv_usec + b->tv_usec;
110         if (tv->tv_usec >= 1000000) {
111                 tv->tv_sec++;
112                 tv->tv_usec -= 1000000;
113         }
114 }
115
116 void
117 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
118 {
119         tv->tv_sec = a->tv_sec - b->tv_sec;
120         tv->tv_usec = a->tv_usec - b->tv_usec;
121         if (((long) tv->tv_usec) < 0) {
122                 tv->tv_sec--;
123                 tv->tv_usec += 1000000;
124         }
125 }
126
127 void
128 tv_div(struct timeval *tv, const struct timeval *a, int n)
129 {
130         tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
131         tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
132         tv->tv_usec %= 1000000;
133 }
134
135 void
136 tv_mul(struct timeval *tv, const struct timeval *a, int n)
137 {
138         tv->tv_usec = a->tv_usec * n;
139         tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
140         tv->tv_usec %= 1000000;
141 }
142
143 #if !defined HAVE_STPCPY
144 char *
145 stpcpy(char *dst, const char *src)
146 {
147         while ((*dst = *src++) != '\0')
148                 dst++;
149         return dst;
150 }
151 #endif
152
153 /* Find a next bit which is set.
154  * Starts testing at cur_bit.
155  * Returns -1 if no more bits are set.
156  *
157  * We never touch bytes we don't need to.
158  * On big-endian, array is assumed to consist of
159  * current_wordsize wide words: for example, is current_wordsize is 4,
160  * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
161  * On little-endian machines, word size is immaterial.
162  */
163 int
164 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
165 {
166         const unsigned endian = 1;
167         int little_endian = *(char *) (void *) &endian;
168
169         const uint8_t *array = bit_array;
170         unsigned pos = cur_bit / 8;
171         unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
172
173         for (;;) {
174                 uint8_t bitmask;
175                 uint8_t cur_byte;
176
177                 if (cur_bit >= size_bits)
178                         return -1;
179                 cur_byte = array[pos ^ pos_xor_mask];
180                 if (cur_byte == 0) {
181                         cur_bit = (cur_bit + 8) & (-8);
182                         pos++;
183                         continue;
184                 }
185                 bitmask = 1 << (cur_bit & 7);
186                 for (;;) {
187                         if (cur_byte & bitmask)
188                                 return cur_bit;
189                         cur_bit++;
190                         if (cur_bit >= size_bits)
191                                 return -1;
192                         bitmask <<= 1;
193                         /* This check *can't be* optimized out: */
194                         if (bitmask == 0)
195                                 break;
196                 }
197                 pos++;
198         }
199 }
200
201 /*
202  * Fetch 64bit argument at position arg_no and
203  * return the index of the next argument.
204  */
205 int
206 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
207 {
208 #if SIZEOF_KERNEL_LONG_T > 4
209 # ifndef current_klongsize
210         if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
211 #  if defined(AARCH64) || defined(POWERPC64)
212                 /* Align arg_no to the next even number. */
213                 arg_no = (arg_no + 1) & 0xe;
214 #  endif /* AARCH64 || POWERPC64 */
215                 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
216                 arg_no += 2;
217         } else
218 # endif /* !current_klongsize */
219         {
220                 *val = tcp->u_arg[arg_no];
221                 arg_no++;
222         }
223 #else /* SIZEOF_KERNEL_LONG_T == 4 */
224 # if defined __ARM_EABI__       \
225   || defined LINUX_MIPSO32      \
226   || defined POWERPC            \
227   || defined XTENSA
228         /* Align arg_no to the next even number. */
229         arg_no = (arg_no + 1) & 0xe;
230 # elif defined SH
231         /*
232          * The SH4 ABI does allow long longs in odd-numbered registers, but
233          * does not allow them to be split between registers and memory - and
234          * there are only four argument registers for normal functions.  As a
235          * result, pread, for example, takes an extra padding argument before
236          * the offset.  This was changed late in the 2.4 series (around 2.4.20).
237          */
238         if (arg_no == 3)
239                 arg_no++;
240 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
241         *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
242         arg_no += 2;
243 #endif
244
245         return arg_no;
246 }
247
248 /*
249  * Print 64bit argument at position arg_no and
250  * return the index of the next argument.
251  */
252 int
253 printllval(struct tcb *tcp, const char *format, int arg_no)
254 {
255         unsigned long long val = 0;
256
257         arg_no = getllval(tcp, &val, arg_no);
258         tprintf(format, val);
259         return arg_no;
260 }
261
262 void
263 printaddr(const kernel_ulong_t addr)
264 {
265         if (!addr)
266                 tprints("NULL");
267         else
268                 tprintf("%#" PRI_klx, addr);
269 }
270
271 #define DEF_PRINTNUM(name, type) \
272 bool                                                                    \
273 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr,     \
274                   const char *const fmt)                                \
275 {                                                                       \
276         type num;                                                       \
277         if (umove_or_printaddr(tcp, addr, &num))                        \
278                 return false;                                           \
279         tprints("[");                                                   \
280         tprintf(fmt, num);                                              \
281         tprints("]");                                                   \
282         return true;                                                    \
283 }
284
285 #define DEF_PRINTNUM_ADDR(name, type) \
286 bool                                                                    \
287 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr)      \
288 {                                                                       \
289         type num;                                                       \
290         if (umove_or_printaddr(tcp, addr, &num))                        \
291                 return false;                                           \
292         tprints("[");                                                   \
293         printaddr(num);                                                 \
294         tprints("]");                                                   \
295         return true;                                                    \
296 }
297
298 #define DEF_PRINTPAIR(name, type) \
299 bool                                                                    \
300 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr,    \
301                    const char *const fmt)                               \
302 {                                                                       \
303         type pair[2];                                                   \
304         if (umove_or_printaddr(tcp, addr, &pair))                       \
305                 return false;                                           \
306         tprints("[");                                                   \
307         tprintf(fmt, pair[0]);                                          \
308         tprints(", ");                                                  \
309         tprintf(fmt, pair[1]);                                          \
310         tprints("]");                                                   \
311         return true;                                                    \
312 }
313
314 DEF_PRINTNUM(int, int)
315 DEF_PRINTNUM_ADDR(int, unsigned int)
316 DEF_PRINTPAIR(int, int)
317 DEF_PRINTNUM(short, short)
318 DEF_PRINTNUM(int64, uint64_t)
319 DEF_PRINTNUM_ADDR(int64, uint64_t)
320 DEF_PRINTPAIR(int64, uint64_t)
321
322 #ifndef current_wordsize
323 bool
324 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
325                   const char *const fmt_long, const char *const fmt_int)
326 {
327         if (current_wordsize > sizeof(int)) {
328                 return printnum_int64(tcp, addr, fmt_long);
329         } else {
330                 return printnum_int(tcp, addr, fmt_int);
331         }
332 }
333
334 bool
335 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
336 {
337         if (current_wordsize > sizeof(int)) {
338                 return printnum_addr_int64(tcp, addr);
339         } else {
340                 return printnum_addr_int(tcp, addr);
341         }
342 }
343 #endif /* !current_wordsize */
344
345 #ifndef current_klongsize
346 bool
347 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
348 {
349         if (current_klongsize > sizeof(int)) {
350                 return printnum_addr_int64(tcp, addr);
351         } else {
352                 return printnum_addr_int(tcp, addr);
353         }
354 }
355 #endif /* !current_klongsize */
356
357 /**
358  * Prints time to a (static internal) buffer and returns pointer to it.
359  *
360  * @param sec           Seconds since epoch.
361  * @param part_sec      Amount of second parts since the start of a second.
362  * @param max_part_sec  Maximum value of a valid part_sec.
363  * @param width         1 + floor(log10(max_part_sec)).
364  */
365 static const char *
366 sprinttime_ex(const long long sec, const unsigned long long part_sec,
367               const unsigned int max_part_sec, const int width)
368 {
369         static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
370                         + sizeof("+0000")];
371
372         if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
373                 return NULL;
374
375         time_t t = (time_t) sec;
376         struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
377         if (!tmp)
378                 return NULL;
379
380         size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
381         if (!pos)
382                 return NULL;
383
384         if (part_sec > 0) {
385                 int ret = snprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
386                                    width, part_sec);
387
388                 if (ret < 0 || (size_t) ret >= sizeof(buf) - pos)
389                         return NULL;
390
391                 pos += ret;
392         }
393
394         return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
395 }
396
397 const char *
398 sprinttime(long long sec)
399 {
400         return sprinttime_ex(sec, 0, 0, 0);
401 }
402
403 const char *
404 sprinttime_usec(long long sec, unsigned long long usec)
405 {
406         return sprinttime_ex(sec, usec, 999999, 6);
407 }
408
409 const char *
410 sprinttime_nsec(long long sec, unsigned long long nsec)
411 {
412         return sprinttime_ex(sec, nsec, 999999999, 9);
413 }
414
415 enum sock_proto
416 getfdproto(struct tcb *tcp, int fd)
417 {
418 #ifdef HAVE_SYS_XATTR_H
419         size_t bufsize = 256;
420         char buf[bufsize];
421         ssize_t r;
422         char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
423
424         if (fd < 0)
425                 return SOCK_PROTO_UNKNOWN;
426
427         sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
428         r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
429         if (r <= 0)
430                 return SOCK_PROTO_UNKNOWN;
431         else {
432                 /*
433                  * This is a protection for the case when the kernel
434                  * side does not append a null byte to the buffer.
435                  */
436                 buf[r] = '\0';
437
438                 return get_proto_by_name(buf);
439         }
440 #else
441         return SOCK_PROTO_UNKNOWN;
442 #endif
443 }
444
445 unsigned long
446 getfdinode(struct tcb *tcp, int fd)
447 {
448         char path[PATH_MAX + 1];
449
450         if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
451                 const char *str = STR_STRIP_PREFIX(path, "socket:[");
452
453                 if (str != path) {
454                         const size_t str_len = strlen(str);
455                         if (str_len && str[str_len - 1] == ']')
456                                 return strtoul(str, NULL, 10);
457                 }
458         }
459
460         return 0;
461 }
462
463 void
464 printfd(struct tcb *tcp, int fd)
465 {
466         char path[PATH_MAX + 1];
467         if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
468                 const char *str;
469                 size_t len;
470                 unsigned long inode;
471
472                 tprintf("%d<", fd);
473                 if (show_fd_path <= 1
474                     || (str = STR_STRIP_PREFIX(path, "socket:[")) == path
475                     || !(len = strlen(str))
476                     || str[len - 1] != ']'
477                     || !(inode = strtoul(str, NULL, 10))
478                     || !print_sockaddr_by_inode(tcp, fd, inode)) {
479                         print_quoted_string(path, strlen(path),
480                                             QUOTE_OMIT_LEADING_TRAILING_QUOTES);
481                 }
482                 tprints(">");
483         } else
484                 tprintf("%d", fd);
485 }
486
487 /*
488  * Quote string `instr' of length `size'
489  * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
490  *
491  * If QUOTE_0_TERMINATED `style' flag is set,
492  * treat `instr' as a NUL-terminated string,
493  * checking up to (`size' + 1) bytes of `instr'.
494  *
495  * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
496  * do not add leading and trailing quoting symbols.
497  *
498  * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
499  * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
500  */
501 int
502 string_quote(const char *instr, char *outstr, const unsigned int size,
503              const unsigned int style)
504 {
505         const unsigned char *ustr = (const unsigned char *) instr;
506         char *s = outstr;
507         unsigned int i;
508         int usehex, c, eol;
509
510         if (style & QUOTE_0_TERMINATED)
511                 eol = '\0';
512         else
513                 eol = 0x100; /* this can never match a char */
514
515         usehex = 0;
516         if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
517                 usehex = 1;
518         } else if (xflag) {
519                 /* Check for presence of symbol which require
520                    to hex-quote the whole string. */
521                 for (i = 0; i < size; ++i) {
522                         c = ustr[i];
523                         /* Check for NUL-terminated string. */
524                         if (c == eol)
525                                 break;
526
527                         /* Force hex unless c is printable or whitespace */
528                         if (c > 0x7e) {
529                                 usehex = 1;
530                                 break;
531                         }
532                         /* In ASCII isspace is only these chars: "\t\n\v\f\r".
533                          * They happen to have ASCII codes 9,10,11,12,13.
534                          */
535                         if (c < ' ' && (unsigned)(c - 9) >= 5) {
536                                 usehex = 1;
537                                 break;
538                         }
539                 }
540         }
541
542         if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
543                 *s++ = '\"';
544
545         if (usehex) {
546                 /* Hex-quote the whole string. */
547                 for (i = 0; i < size; ++i) {
548                         c = ustr[i];
549                         /* Check for NUL-terminated string. */
550                         if (c == eol)
551                                 goto asciz_ended;
552                         *s++ = '\\';
553                         *s++ = 'x';
554                         *s++ = "0123456789abcdef"[c >> 4];
555                         *s++ = "0123456789abcdef"[c & 0xf];
556                 }
557         } else {
558                 for (i = 0; i < size; ++i) {
559                         c = ustr[i];
560                         /* Check for NUL-terminated string. */
561                         if (c == eol)
562                                 goto asciz_ended;
563                         if ((i == (size - 1)) &&
564                             (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
565                                 goto asciz_ended;
566                         switch (c) {
567                                 case '\"': case '\\':
568                                         *s++ = '\\';
569                                         *s++ = c;
570                                         break;
571                                 case '\f':
572                                         *s++ = '\\';
573                                         *s++ = 'f';
574                                         break;
575                                 case '\n':
576                                         *s++ = '\\';
577                                         *s++ = 'n';
578                                         break;
579                                 case '\r':
580                                         *s++ = '\\';
581                                         *s++ = 'r';
582                                         break;
583                                 case '\t':
584                                         *s++ = '\\';
585                                         *s++ = 't';
586                                         break;
587                                 case '\v':
588                                         *s++ = '\\';
589                                         *s++ = 'v';
590                                         break;
591                                 default:
592                                         if (c >= ' ' && c <= 0x7e)
593                                                 *s++ = c;
594                                         else {
595                                                 /* Print \octal */
596                                                 *s++ = '\\';
597                                                 if (i + 1 < size
598                                                     && ustr[i + 1] >= '0'
599                                                     && ustr[i + 1] <= '9'
600                                                 ) {
601                                                         /* Print \ooo */
602                                                         *s++ = '0' + (c >> 6);
603                                                         *s++ = '0' + ((c >> 3) & 0x7);
604                                                 } else {
605                                                         /* Print \[[o]o]o */
606                                                         if ((c >> 3) != 0) {
607                                                                 if ((c >> 6) != 0)
608                                                                         *s++ = '0' + (c >> 6);
609                                                                 *s++ = '0' + ((c >> 3) & 0x7);
610                                                         }
611                                                 }
612                                                 *s++ = '0' + (c & 0x7);
613                                         }
614                                         break;
615                         }
616                 }
617         }
618
619         if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
620                 *s++ = '\"';
621         *s = '\0';
622
623         /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
624         if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
625                 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
626                  * but next char is NUL.
627                  */
628                 return 0;
629         }
630
631         return 1;
632
633  asciz_ended:
634         if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
635                 *s++ = '\"';
636         *s = '\0';
637         /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
638         return 0;
639 }
640
641 #ifndef ALLOCA_CUTOFF
642 # define ALLOCA_CUTOFF  4032
643 #endif
644 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
645
646 /*
647  * Quote string `str' of length `size' and print the result.
648  *
649  * If QUOTE_0_TERMINATED `style' flag is set,
650  * treat `str' as a NUL-terminated string and
651  * quote at most (`size' - 1) bytes.
652  *
653  * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
654  * do not add leading and trailing quoting symbols.
655  *
656  * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
657  * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
658  */
659 int
660 print_quoted_string(const char *str, unsigned int size,
661                     const unsigned int style)
662 {
663         char *buf;
664         char *outstr;
665         unsigned int alloc_size;
666         int rc;
667
668         if (size && style & QUOTE_0_TERMINATED)
669                 --size;
670
671         alloc_size = 4 * size;
672         if (alloc_size / 4 != size) {
673                 error_msg("Out of memory");
674                 tprints("???");
675                 return -1;
676         }
677         alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
678
679         if (use_alloca(alloc_size)) {
680                 outstr = alloca(alloc_size);
681                 buf = NULL;
682         } else {
683                 outstr = buf = malloc(alloc_size);
684                 if (!buf) {
685                         error_msg("Out of memory");
686                         tprints("???");
687                         return -1;
688                 }
689         }
690
691         rc = string_quote(str, outstr, size, style);
692         tprints(outstr);
693
694         free(buf);
695         return rc;
696 }
697
698 /*
699  * Print path string specified by address `addr' and length `n'.
700  * If path length exceeds `n', append `...' to the output.
701  */
702 void
703 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
704 {
705         char path[PATH_MAX + 1];
706         int nul_seen;
707
708         if (!addr) {
709                 tprints("NULL");
710                 return;
711         }
712
713         /* Cap path length to the path buffer size */
714         if (n > sizeof(path) - 1)
715                 n = sizeof(path) - 1;
716
717         /* Fetch one byte more to find out whether path length > n. */
718         nul_seen = umovestr(tcp, addr, n + 1, path);
719         if (nul_seen < 0)
720                 printaddr(addr);
721         else {
722                 path[n++] = '\0';
723                 print_quoted_string(path, n, QUOTE_0_TERMINATED);
724                 if (!nul_seen)
725                         tprints("...");
726         }
727 }
728
729 void
730 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
731 {
732         /* Size must correspond to char path[] size in printpathn */
733         printpathn(tcp, addr, PATH_MAX);
734 }
735
736 /*
737  * Print string specified by address `addr' and length `len'.
738  * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
739  * as a NUL-terminated string.
740  * Pass `user_style' on to `string_quote'.
741  * Append `...' to the output if either the string length exceeds `max_strlen',
742  * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
743  */
744 void
745 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
746             const kernel_ulong_t len, const unsigned int user_style)
747 {
748         static char *str;
749         static char *outstr;
750
751         unsigned int size;
752         unsigned int style = user_style;
753         int rc;
754         int ellipsis;
755
756         if (!addr) {
757                 tprints("NULL");
758                 return;
759         }
760         /* Allocate static buffers if they are not allocated yet. */
761         if (!str) {
762                 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
763
764                 if (outstr_size / 4 != max_strlen)
765                         die_out_of_memory();
766                 str = xmalloc(max_strlen + 1);
767                 outstr = xmalloc(outstr_size);
768         }
769
770         /* Fetch one byte more because string_quote may look one byte ahead. */
771         size = max_strlen + 1;
772
773         if (size > len)
774                 size = len;
775         if (style & QUOTE_0_TERMINATED)
776                 rc = umovestr(tcp, addr, size, str);
777         else
778                 rc = umoven(tcp, addr, size, str);
779
780         if (rc < 0) {
781                 printaddr(addr);
782                 return;
783         }
784
785         if (size > max_strlen)
786                 size = max_strlen;
787         else
788                 str[size] = '\xff';
789
790         /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
791          * or we were requested to print more than -s NUM chars)...
792          */
793         ellipsis = string_quote(str, outstr, size, style)
794                    && len
795                    && ((style & QUOTE_0_TERMINATED)
796                        || len > max_strlen);
797
798         tprints(outstr);
799         if (ellipsis)
800                 tprints("...");
801 }
802
803 void
804 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
805              kernel_ulong_t data_size)
806 {
807 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
808         union {
809                 struct { uint32_t base; uint32_t len; } *iov32;
810                 struct { uint64_t base; uint64_t len; } *iov64;
811         } iovu;
812 #define iov iovu.iov64
813 #define sizeof_iov \
814         (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
815 #define iov_iov_base(i) \
816         (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
817 #define iov_iov_len(i) \
818         (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
819 #else
820         struct iovec *iov;
821 #define sizeof_iov sizeof(*iov)
822 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
823 #define iov_iov_len(i) iov[i].iov_len
824 #endif
825         int i;
826         unsigned size;
827
828         size = sizeof_iov * len;
829         /* Assuming no sane program has millions of iovs */
830         if ((unsigned)len > 1024*1024 /* insane or negative size? */
831             || (iov = malloc(size)) == NULL) {
832                 error_msg("Out of memory");
833                 return;
834         }
835         if (umoven(tcp, addr, size, iov) >= 0) {
836                 for (i = 0; i < len; i++) {
837                         kernel_ulong_t iov_len = iov_iov_len(i);
838                         if (iov_len > data_size)
839                                 iov_len = data_size;
840                         if (!iov_len)
841                                 break;
842                         data_size -= iov_len;
843                         /* include the buffer number to make it easy to
844                          * match up the trace with the source */
845                         tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
846                         dumpstr(tcp, iov_iov_base(i), iov_len);
847                 }
848         }
849         free(iov);
850 #undef sizeof_iov
851 #undef iov_iov_base
852 #undef iov_iov_len
853 #undef iov
854 }
855
856 void
857 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
858 {
859         static int strsize = -1;
860         static unsigned char *str;
861
862         char outbuf[
863                 (
864                         (sizeof(
865                         "xx xx xx xx xx xx xx xx  xx xx xx xx xx xx xx xx  "
866                         "1234567890123456") + /*in case I'm off by few:*/ 4)
867                 /*align to 8 to make memset easier:*/ + 7) & -8
868         ];
869         const unsigned char *src;
870         int i;
871
872         memset(outbuf, ' ', sizeof(outbuf));
873
874         if (strsize < len + 16) {
875                 free(str);
876                 str = malloc(len + 16);
877                 if (!str) {
878                         strsize = -1;
879                         error_msg("Out of memory");
880                         return;
881                 }
882                 strsize = len + 16;
883         }
884
885         if (umoven(tcp, addr, len, str) < 0)
886                 return;
887
888         /* Space-pad to 16 bytes */
889         i = len;
890         while (i & 0xf)
891                 str[i++] = ' ';
892
893         i = 0;
894         src = str;
895         while (i < len) {
896                 char *dst = outbuf;
897                 /* Hex dump */
898                 do {
899                         if (i < len) {
900                                 *dst++ = "0123456789abcdef"[*src >> 4];
901                                 *dst++ = "0123456789abcdef"[*src & 0xf];
902                         } else {
903                                 *dst++ = ' ';
904                                 *dst++ = ' ';
905                         }
906                         dst++; /* space is there by memset */
907                         i++;
908                         if ((i & 7) == 0)
909                                 dst++; /* space is there by memset */
910                         src++;
911                 } while (i & 0xf);
912                 /* ASCII dump */
913                 i -= 16;
914                 src -= 16;
915                 do {
916                         if (*src >= ' ' && *src < 0x7f)
917                                 *dst++ = *src;
918                         else
919                                 *dst++ = '.';
920                         src++;
921                 } while (++i & 0xf);
922                 *dst = '\0';
923                 tprintf(" | %05x  %s |\n", i - 16, outbuf);
924         }
925 }
926
927 static bool process_vm_readv_not_supported;
928
929 #ifndef HAVE_PROCESS_VM_READV
930 /*
931  * Need to do this since process_vm_readv() is not yet available in libc.
932  * When libc is be updated, only "static bool process_vm_readv_not_supported"
933  * line should remain.
934  */
935 /* Have to avoid duplicating with the C library headers. */
936 static ssize_t strace_process_vm_readv(pid_t pid,
937                  const struct iovec *lvec,
938                  unsigned long liovcnt,
939                  const struct iovec *rvec,
940                  unsigned long riovcnt,
941                  unsigned long flags)
942 {
943         return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
944 }
945 # define process_vm_readv strace_process_vm_readv
946 #endif /* !HAVE_PROCESS_VM_READV */
947
948 static ssize_t
949 vm_read_mem(const pid_t pid, void *const laddr,
950             const kernel_ulong_t raddr, const size_t len)
951 {
952         const unsigned long truncated_raddr = raddr;
953
954         if (raddr != (kernel_ulong_t) truncated_raddr) {
955                 errno = EIO;
956                 return -1;
957         }
958
959         const struct iovec local = {
960                 .iov_base = laddr,
961                 .iov_len = len
962         };
963         const struct iovec remote = {
964                 .iov_base = (void *) truncated_raddr,
965                 .iov_len = len
966         };
967
968         return process_vm_readv(pid, &local, 1, &remote, 1, 0);
969 }
970
971 /*
972  * move `len' bytes of data from process `pid'
973  * at address `addr' to our space at `our_addr'
974  */
975 int
976 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
977        void *const our_addr)
978 {
979         char *laddr = our_addr;
980         int pid = tcp->pid;
981         unsigned int n, m, nread;
982         union {
983                 long val;
984                 char x[sizeof(long)];
985         } u;
986
987 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
988         if (current_wordsize < sizeof(addr)
989             && (addr & (~(kernel_ulong_t) -1U))) {
990                 return -1;
991         }
992 #endif
993
994         if (!process_vm_readv_not_supported) {
995                 int r = vm_read_mem(pid, laddr, addr, len);
996                 if ((unsigned int) r == len)
997                         return 0;
998                 if (r >= 0) {
999                         error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1000                                   (unsigned int) r, len, addr);
1001                         return -1;
1002                 }
1003                 switch (errno) {
1004                         case ENOSYS:
1005                                 process_vm_readv_not_supported = 1;
1006                                 break;
1007                         case EPERM:
1008                                 /* operation not permitted, try PTRACE_PEEKDATA */
1009                                 break;
1010                         case ESRCH:
1011                                 /* the process is gone */
1012                                 return -1;
1013                         case EFAULT: case EIO:
1014                                 /* address space is inaccessible */
1015                                 return -1;
1016                         default:
1017                                 /* all the rest is strange and should be reported */
1018                                 perror_msg("process_vm_readv");
1019                                 return -1;
1020                 }
1021         }
1022
1023         nread = 0;
1024         if (addr & (sizeof(long) - 1)) {
1025                 /* addr not a multiple of sizeof(long) */
1026                 n = addr & (sizeof(long) - 1);  /* residue */
1027                 addr &= -sizeof(long);          /* aligned address */
1028                 errno = 0;
1029                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1030                 switch (errno) {
1031                         case 0:
1032                                 break;
1033                         case ESRCH: case EINVAL:
1034                                 /* these could be seen if the process is gone */
1035                                 return -1;
1036                         case EFAULT: case EIO: case EPERM:
1037                                 /* address space is inaccessible */
1038                                 return -1;
1039                         default:
1040                                 /* all the rest is strange and should be reported */
1041                                 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1042                                             pid, addr);
1043                                 return -1;
1044                 }
1045                 m = MIN(sizeof(long) - n, len);
1046                 memcpy(laddr, &u.x[n], m);
1047                 addr += sizeof(long);
1048                 laddr += m;
1049                 nread += m;
1050                 len -= m;
1051         }
1052         while (len) {
1053                 errno = 0;
1054                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1055                 switch (errno) {
1056                         case 0:
1057                                 break;
1058                         case ESRCH: case EINVAL:
1059                                 /* these could be seen if the process is gone */
1060                                 return -1;
1061                         case EFAULT: case EIO: case EPERM:
1062                                 /* address space is inaccessible */
1063                                 if (nread) {
1064                                         perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1065                                                    nread, nread + len, addr - nread);
1066                                 }
1067                                 return -1;
1068                         default:
1069                                 /* all the rest is strange and should be reported */
1070                                 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1071                                             pid, addr);
1072                                 return -1;
1073                 }
1074                 m = MIN(sizeof(long), len);
1075                 memcpy(laddr, u.x, m);
1076                 addr += sizeof(long);
1077                 laddr += m;
1078                 nread += m;
1079                 len -= m;
1080         }
1081
1082         return 0;
1083 }
1084
1085 int
1086 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1087                     const unsigned int len, void *const our_addr)
1088 {
1089         if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1090             umoven(tcp, addr, len, our_addr) < 0) {
1091                 printaddr(addr);
1092                 return -1;
1093         }
1094         return 0;
1095 }
1096
1097 int
1098 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1099                                     const kernel_ulong_t addr,
1100                                     const unsigned int len,
1101                                     void *const our_addr)
1102 {
1103         if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1104                 printaddr(addr);
1105                 return -1;
1106         }
1107         return 0;
1108 }
1109
1110 /*
1111  * Like `umove' but make the additional effort of looking
1112  * for a terminating zero byte.
1113  *
1114  * Returns < 0 on error, > 0 if NUL was seen,
1115  * (TODO if useful: return count of bytes including NUL),
1116  * else 0 if len bytes were read but no NUL byte seen.
1117  *
1118  * Note: there is no guarantee we won't overwrite some bytes
1119  * in laddr[] _after_ terminating NUL (but, of course,
1120  * we never write past laddr[len-1]).
1121  */
1122 int
1123 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1124 {
1125         const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1126         const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1127
1128         int pid = tcp->pid;
1129         unsigned int n, m, nread;
1130         union {
1131                 unsigned long val;
1132                 char x[sizeof(long)];
1133         } u;
1134
1135 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1136         if (current_wordsize < sizeof(addr)
1137             && (addr & (~(kernel_ulong_t) -1U))) {
1138                 return -1;
1139         }
1140 #endif
1141
1142         nread = 0;
1143         if (!process_vm_readv_not_supported) {
1144                 const size_t page_size = get_pagesize();
1145                 const size_t page_mask = page_size - 1;
1146
1147                 while (len > 0) {
1148                         unsigned int chunk_len;
1149                         unsigned int end_in_page;
1150
1151                         /*
1152                          * Don't cross pages, otherwise we can get EFAULT
1153                          * and fail to notice that terminating NUL lies
1154                          * in the existing (first) page.
1155                          */
1156                         chunk_len = len > page_size ? page_size : len;
1157                         end_in_page = (addr + chunk_len) & page_mask;
1158                         if (chunk_len > end_in_page) /* crosses to the next page */
1159                                 chunk_len -= end_in_page;
1160
1161                         int r = vm_read_mem(pid, laddr, addr, chunk_len);
1162                         if (r > 0) {
1163                                 if (memchr(laddr, '\0', r))
1164                                         return 1;
1165                                 addr += r;
1166                                 laddr += r;
1167                                 nread += r;
1168                                 len -= r;
1169                                 continue;
1170                         }
1171                         switch (errno) {
1172                                 case ENOSYS:
1173                                         process_vm_readv_not_supported = 1;
1174                                         goto vm_readv_didnt_work;
1175                                 case ESRCH:
1176                                         /* the process is gone */
1177                                         return -1;
1178                                 case EPERM:
1179                                         /* operation not permitted, try PTRACE_PEEKDATA */
1180                                         if (!nread)
1181                                                 goto vm_readv_didnt_work;
1182                                         /* fall through */
1183                                 case EFAULT: case EIO:
1184                                         /* address space is inaccessible */
1185                                         if (nread) {
1186                                                 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1187                                                            nread, nread + len, addr - nread);
1188                                         }
1189                                         return -1;
1190                                 default:
1191                                         /* all the rest is strange and should be reported */
1192                                         perror_msg("process_vm_readv");
1193                                         return -1;
1194                         }
1195                 }
1196                 return 0;
1197         }
1198  vm_readv_didnt_work:
1199
1200         if (addr & (sizeof(long) - 1)) {
1201                 /* addr not a multiple of sizeof(long) */
1202                 n = addr & (sizeof(long) - 1);  /* residue */
1203                 addr &= -sizeof(long);          /* aligned address */
1204                 errno = 0;
1205                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1206                 switch (errno) {
1207                         case 0:
1208                                 break;
1209                         case ESRCH: case EINVAL:
1210                                 /* these could be seen if the process is gone */
1211                                 return -1;
1212                         case EFAULT: case EIO: case EPERM:
1213                                 /* address space is inaccessible */
1214                                 return -1;
1215                         default:
1216                                 /* all the rest is strange and should be reported */
1217                                 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1218                                             pid, addr);
1219                                 return -1;
1220                 }
1221                 m = MIN(sizeof(long) - n, len);
1222                 memcpy(laddr, &u.x[n], m);
1223                 while (n & (sizeof(long) - 1))
1224                         if (u.x[n++] == '\0')
1225                                 return 1;
1226                 addr += sizeof(long);
1227                 laddr += m;
1228                 nread += m;
1229                 len -= m;
1230         }
1231
1232         while (len) {
1233                 errno = 0;
1234                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1235                 switch (errno) {
1236                         case 0:
1237                                 break;
1238                         case ESRCH: case EINVAL:
1239                                 /* these could be seen if the process is gone */
1240                                 return -1;
1241                         case EFAULT: case EIO: case EPERM:
1242                                 /* address space is inaccessible */
1243                                 if (nread) {
1244                                         perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1245                                                    nread, nread + len, addr - nread);
1246                                 }
1247                                 return -1;
1248                         default:
1249                                 /* all the rest is strange and should be reported */
1250                                 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1251                                            pid, addr);
1252                                 return -1;
1253                 }
1254                 m = MIN(sizeof(long), len);
1255                 memcpy(laddr, u.x, m);
1256                 /* "If a NUL char exists in this word" */
1257                 if ((u.val - x01010101) & ~u.val & x80808080)
1258                         return 1;
1259                 addr += sizeof(long);
1260                 laddr += m;
1261                 nread += m;
1262                 len -= m;
1263         }
1264         return 0;
1265 }
1266
1267 /*
1268  * Iteratively fetch and print up to nmemb elements of elem_size size
1269  * from the array that starts at tracee's address start_addr.
1270  *
1271  * Array elements are being fetched to the address specified by elem_buf.
1272  *
1273  * The fetcher callback function specified by umoven_func should follow
1274  * the same semantics as umoven_or_printaddr function.
1275  *
1276  * The printer callback function specified by print_func is expected
1277  * to print something; if it returns false, no more iterations will be made.
1278  *
1279  * The pointer specified by opaque_data is passed to each invocation
1280  * of print_func callback function.
1281  *
1282  * This function prints:
1283  * - "NULL", if start_addr is NULL;
1284  * - "[]", if nmemb is 0;
1285  * - start_addr, if nmemb * elem_size overflows or wraps around;
1286  * - nothing, if the first element cannot be fetched
1287  *   (if umoven_func returns non-zero), but it is assumed that
1288  *   umoven_func has printed the address it failed to fetch data from;
1289  * - elements of the array, delimited by ", ", with the array itself
1290  *   enclosed with [] brackets.
1291  *
1292  * If abbrev(tcp) is true, then
1293  * - the maximum number of elements printed equals to max_strlen;
1294  * - "..." is printed instead of max_strlen+1 element
1295  *   and no more iterations will be made.
1296  *
1297  * This function returns true only if
1298  * - umoven_func has been called at least once AND
1299  * - umoven_func has not returned false.
1300  */
1301 bool
1302 print_array(struct tcb *const tcp,
1303             const kernel_ulong_t start_addr,
1304             const size_t nmemb,
1305             void *const elem_buf,
1306             const size_t elem_size,
1307             int (*const umoven_func)(struct tcb *,
1308                                      kernel_ulong_t,
1309                                      unsigned int,
1310                                      void *),
1311             bool (*const print_func)(struct tcb *,
1312                                      void *elem_buf,
1313                                      size_t elem_size,
1314                                      void *opaque_data),
1315             void *const opaque_data)
1316 {
1317         if (!start_addr) {
1318                 tprints("NULL");
1319                 return false;
1320         }
1321
1322         if (!nmemb) {
1323                 tprints("[]");
1324                 return false;
1325         }
1326
1327         const size_t size = nmemb * elem_size;
1328         const kernel_ulong_t end_addr = start_addr + size;
1329
1330         if (end_addr <= start_addr || size / elem_size != nmemb) {
1331                 printaddr(start_addr);
1332                 return false;
1333         }
1334
1335         const kernel_ulong_t abbrev_end =
1336                 (abbrev(tcp) && max_strlen < nmemb) ?
1337                         start_addr + elem_size * max_strlen : end_addr;
1338         kernel_ulong_t cur;
1339
1340         for (cur = start_addr; cur < end_addr; cur += elem_size) {
1341                 if (cur != start_addr)
1342                         tprints(", ");
1343
1344                 if (umoven_func(tcp, cur, elem_size, elem_buf))
1345                         break;
1346
1347                 if (cur == start_addr)
1348                         tprints("[");
1349
1350                 if (cur >= abbrev_end) {
1351                         tprints("...");
1352                         cur = end_addr;
1353                         break;
1354                 }
1355
1356                 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1357                         cur = end_addr;
1358                         break;
1359                 }
1360         }
1361         if (cur != start_addr)
1362                 tprints("]");
1363
1364         return cur >= end_addr;
1365 }
1366
1367 int
1368 printargs(struct tcb *tcp)
1369 {
1370         const int n = tcp->s_ent->nargs;
1371         int i;
1372         for (i = 0; i < n; ++i)
1373                 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1374         return RVAL_DECODED;
1375 }
1376
1377 int
1378 printargs_u(struct tcb *tcp)
1379 {
1380         const int n = tcp->s_ent->nargs;
1381         int i;
1382         for (i = 0; i < n; ++i)
1383                 tprintf("%s%u", i ? ", " : "",
1384                         (unsigned int) tcp->u_arg[i]);
1385         return RVAL_DECODED;
1386 }
1387
1388 int
1389 printargs_d(struct tcb *tcp)
1390 {
1391         const int n = tcp->s_ent->nargs;
1392         int i;
1393         for (i = 0; i < n; ++i)
1394                 tprintf("%s%d", i ? ", " : "",
1395                         (int) tcp->u_arg[i]);
1396         return RVAL_DECODED;
1397 }
1398
1399 /* Print abnormal high bits of a kernel_ulong_t value. */
1400 void
1401 print_abnormal_hi(const kernel_ulong_t val)
1402 {
1403         if (current_klongsize > 4) {
1404                 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1405                 if (hi)
1406                         tprintf("%#x<<32|", hi);
1407         }
1408 }
1409
1410 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1411 # define open_file open64
1412 #else
1413 # define open_file open
1414 #endif
1415
1416 int
1417 read_int_from_file(const char *const fname, int *const pvalue)
1418 {
1419         const int fd = open_file(fname, O_RDONLY);
1420         if (fd < 0)
1421                 return -1;
1422
1423         long lval;
1424         char buf[sizeof(lval) * 3];
1425         int n = read(fd, buf, sizeof(buf) - 1);
1426         int saved_errno = errno;
1427         close(fd);
1428
1429         if (n < 0) {
1430                 errno = saved_errno;
1431                 return -1;
1432         }
1433
1434         buf[n] = '\0';
1435         char *endptr = 0;
1436         errno = 0;
1437         lval = strtol(buf, &endptr, 10);
1438         if (!endptr || (*endptr && '\n' != *endptr)
1439 #if INT_MAX < LONG_MAX
1440             || lval > INT_MAX || lval < INT_MIN
1441 #endif
1442             || ERANGE == errno) {
1443                 if (!errno)
1444                         errno = EINVAL;
1445                 return -1;
1446         }
1447
1448         *pvalue = (int) lval;
1449         return 0;
1450 }