~ubuntu-branches/ubuntu/trusty/systemd/trusty

« back to all changes in this revision

Viewing changes to src/shared/util.c

Tags: upstream-202
ImportĀ upstreamĀ versionĀ 202

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
#include <limits.h>
60
60
#include <langinfo.h>
61
61
#include <locale.h>
 
62
#include <libgen.h>
62
63
 
63
64
#include "macro.h"
64
65
#include "util.h"
79
80
static volatile unsigned cached_columns = 0;
80
81
static volatile unsigned cached_lines = 0;
81
82
 
 
83
#define procfs_file_alloca(pid, field)                                  \
 
84
        ({                                                              \
 
85
                pid_t _pid_ = (pid);                                    \
 
86
                char *_r_;                                              \
 
87
                _r_ = alloca(sizeof("/proc/") -1 + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
 
88
                sprintf(_r_, "/proc/%lu/" field, (unsigned long) _pid_); \
 
89
                _r_;                                                    \
 
90
        })
 
91
 
82
92
size_t page_size(void) {
83
93
        static __thread size_t pgsz = 0;
84
94
        long r;
206
216
}
207
217
 
208
218
void close_nointr_nofail(int fd) {
209
 
        int saved_errno = errno;
 
219
        PROTECT_ERRNO;
210
220
 
211
221
        /* like close_nointr() but cannot fail, and guarantees errno
212
222
         * is unchanged */
213
223
 
214
224
        assert_se(close_nointr(fd) == 0);
215
 
 
216
 
        errno = saved_errno;
217
225
}
218
226
 
219
227
void close_many(const int fds[], unsigned n_fd) {
220
228
        unsigned i;
221
229
 
 
230
        assert(fds || n_fd <= 0);
 
231
 
222
232
        for (i = 0; i < n_fd; i++)
223
233
                close_nointr_nofail(fds[i]);
224
234
}
225
235
 
 
236
int unlink_noerrno(const char *path) {
 
237
        PROTECT_ERRNO;
 
238
        int r;
 
239
 
 
240
        r = unlink(path);
 
241
        if (r < 0)
 
242
                return -errno;
 
243
 
 
244
        return 0;
 
245
}
 
246
 
226
247
int parse_boolean(const char *v) {
227
248
        assert(v);
228
249
 
290
311
        l = strtoul(s, &x, 0);
291
312
 
292
313
        if (!x || x == s || *x || errno)
293
 
                return errno ? -errno : -EINVAL;
 
314
                return errno > 0 ? -errno : -EINVAL;
294
315
 
295
316
        if ((unsigned long) (unsigned) l != l)
296
317
                return -ERANGE;
310
331
        l = strtol(s, &x, 0);
311
332
 
312
333
        if (!x || x == s || *x || errno)
313
 
                return errno ? -errno : -EINVAL;
 
334
                return errno > 0 ? -errno : -EINVAL;
314
335
 
315
336
        if ((long) (int) l != l)
316
337
                return -ERANGE;
446
467
int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
447
468
        int r;
448
469
        _cleanup_fclose_ FILE *f = NULL;
449
 
        char fn[PATH_MAX], line[LINE_MAX], *p;
 
470
        char line[LINE_MAX];
450
471
        long unsigned ppid;
 
472
        const char *p;
451
473
 
452
 
        assert(pid > 0);
 
474
        assert(pid >= 0);
453
475
        assert(_ppid);
454
476
 
455
 
        assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
456
 
        char_array_0(fn);
 
477
        if (pid == 0) {
 
478
                *_ppid = getppid();
 
479
                return 0;
 
480
        }
457
481
 
458
 
        f = fopen(fn, "re");
 
482
        p = procfs_file_alloca(pid, "stat");
 
483
        f = fopen(p, "re");
459
484
        if (!f)
460
485
                return -errno;
461
486
 
490
515
 
491
516
int get_starttime_of_pid(pid_t pid, unsigned long long *st) {
492
517
        _cleanup_fclose_ FILE *f = NULL;
493
 
        char fn[PATH_MAX], line[LINE_MAX], *p;
 
518
        char line[LINE_MAX];
 
519
        const char *p;
494
520
 
495
 
        assert(pid > 0);
 
521
        assert(pid >= 0);
496
522
        assert(st);
497
523
 
498
 
        assert_se(snprintf(fn, sizeof(fn)-1, "/proc/%lu/stat", (unsigned long) pid) < (int) (sizeof(fn)-1));
499
 
        char_array_0(fn);
 
524
        if (pid == 0)
 
525
                p = "/proc/self/stat";
 
526
        else
 
527
                p = procfs_file_alloca(pid, "stat");
500
528
 
501
 
        f = fopen(fn, "re");
 
529
        f = fopen(p, "re");
502
530
        if (!f)
503
531
                return -errno;
504
532
 
565
593
}
566
594
 
567
595
int get_process_comm(pid_t pid, char **name) {
568
 
        int r;
 
596
        const char *p;
569
597
 
570
598
        assert(name);
 
599
        assert(pid >= 0);
571
600
 
572
601
        if (pid == 0)
573
 
                r = read_one_line_file("/proc/self/comm", name);
574
 
        else {
575
 
                char *p;
576
 
                if (asprintf(&p, "/proc/%lu/comm", (unsigned long) pid) < 0)
577
 
                        return -ENOMEM;
578
 
 
579
 
                r = read_one_line_file(p, name);
580
 
                free(p);
581
 
        }
582
 
 
583
 
        return r;
 
602
                p = "/proc/self/comm";
 
603
        else
 
604
                p = procfs_file_alloca(pid, "comm");
 
605
 
 
606
        return read_one_line_file(p, name);
584
607
}
585
608
 
586
609
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line) {
 
610
        _cleanup_fclose_ FILE *f = NULL;
587
611
        char *r = NULL, *k;
 
612
        const char *p;
588
613
        int c;
589
 
        FILE *f;
590
614
 
591
615
        assert(line);
 
616
        assert(pid >= 0);
592
617
 
593
618
        if (pid == 0)
594
 
                f = fopen("/proc/self/cmdline", "re");
595
 
        else {
596
 
                char *p;
597
 
                if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
598
 
                        return -ENOMEM;
599
 
 
600
 
                f = fopen(p, "re");
601
 
                free(p);
602
 
        }
603
 
 
 
619
                p = "/proc/self/cmdline";
 
620
        else
 
621
                p = procfs_file_alloca(pid, "cmdline");
 
622
 
 
623
        f = fopen(p, "re");
604
624
        if (!f)
605
625
                return -errno;
 
626
 
606
627
        if (max_length == 0) {
607
 
                size_t len = 1;
 
628
                size_t len = 0, allocated = 0;
 
629
 
608
630
                while ((c = getc(f)) != EOF) {
609
 
                        k = realloc(r, len+1);
610
 
                        if (k == NULL) {
 
631
 
 
632
                        if (!GREEDY_REALLOC(r, allocated, len+2)) {
611
633
                                free(r);
612
 
                                fclose(f);
613
634
                                return -ENOMEM;
614
635
                        }
615
 
                        r = k;
616
 
                        r[len-1] = isprint(c) ? c : ' ';
617
 
                        r[len] = 0;
618
 
                        len++;
 
636
 
 
637
                        r[len++] = isprint(c) ? c : ' ';
619
638
                }
 
639
 
 
640
                if (len > 0)
 
641
                        r[len-1] = 0;
 
642
 
620
643
        } else {
621
644
                bool space = false;
622
645
                size_t left;
 
646
 
623
647
                r = new(char, max_length);
624
 
                if (!r) {
625
 
                        fclose(f);
 
648
                if (!r)
626
649
                        return -ENOMEM;
627
 
                }
628
650
 
629
651
                k = r;
630
652
                left = max_length;
657
679
                        *k = 0;
658
680
        }
659
681
 
660
 
        fclose(f);
661
 
 
662
682
        /* Kernel threads have no argv[] */
663
683
        if (r == NULL || r[0] == 0) {
664
684
                char *t;
685
705
}
686
706
 
687
707
int is_kernel_thread(pid_t pid) {
688
 
        char *p;
 
708
        const char *p;
689
709
        size_t count;
690
710
        char c;
691
711
        bool eof;
694
714
        if (pid == 0)
695
715
                return 0;
696
716
 
697
 
        if (asprintf(&p, "/proc/%lu/cmdline", (unsigned long) pid) < 0)
698
 
                return -ENOMEM;
 
717
        assert(pid > 0);
699
718
 
 
719
        p = procfs_file_alloca(pid, "cmdline");
700
720
        f = fopen(p, "re");
701
 
        free(p);
702
 
 
703
721
        if (!f)
704
722
                return -errno;
705
723
 
715
733
        return 0;
716
734
}
717
735
 
 
736
 
718
737
int get_process_exe(pid_t pid, char **name) {
719
 
        int r;
 
738
        const char *p;
720
739
 
 
740
        assert(pid >= 0);
721
741
        assert(name);
722
742
 
723
743
        if (pid == 0)
724
 
                r = readlink_malloc("/proc/self/exe", name);
725
 
        else {
726
 
                char *p;
727
 
                if (asprintf(&p, "/proc/%lu/exe", (unsigned long) pid) < 0)
728
 
                        return -ENOMEM;
729
 
 
730
 
                r = readlink_malloc(p, name);
731
 
                free(p);
732
 
        }
733
 
 
734
 
        return r;
 
744
                p = "/proc/self/exe";
 
745
        else
 
746
                p = procfs_file_alloca(pid, "exe");
 
747
 
 
748
        return readlink_malloc(p, name);
735
749
}
736
750
 
737
751
static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
738
752
        _cleanup_fclose_ FILE *f = NULL;
739
 
        _cleanup_free_ char *p = NULL;
740
753
        char line[LINE_MAX];
 
754
        const char *p;
741
755
 
742
756
        assert(field);
743
757
        assert(uid);
745
759
        if (pid == 0)
746
760
                return getuid();
747
761
 
748
 
        if (asprintf(&p, "/proc/%lu/status", (unsigned long) pid) < 0)
749
 
                return -ENOMEM;
750
 
 
 
762
        p = procfs_file_alloca(pid, "status");
751
763
        f = fopen(p, "re");
752
764
        if (!f)
753
765
                return -errno;
775
787
}
776
788
 
777
789
int get_process_gid(pid_t pid, gid_t *gid) {
 
790
        assert_cc(sizeof(uid_t) == sizeof(gid_t));
778
791
        return get_process_id(pid, "Gid:", gid);
779
792
}
780
793
 
890
903
        int sig;
891
904
 
892
905
        for (sig = 1; sig < _NSIG; sig++) {
893
 
                struct sigaction sa;
 
906
                struct sigaction sa = {
 
907
                        .sa_handler = SIG_DFL,
 
908
                        .sa_flags = SA_RESTART,
 
909
                };
894
910
 
895
911
                if (sig == SIGKILL || sig == SIGSTOP)
896
912
                        continue;
897
913
 
898
 
                zero(sa);
899
 
                sa.sa_handler = SIG_DFL;
900
 
                sa.sa_flags = SA_RESTART;
901
 
 
902
914
                /* On Linux the first two RT signals are reserved by
903
915
                 * glibc, and sigaction() will return EINVAL for them. */
904
916
                if ((sigaction(sig, &sa, NULL) < 0))
1029
1041
        return 0;
1030
1042
}
1031
1043
 
1032
 
 
1033
1044
char hexchar(int x) {
1034
1045
        static const char table[16] = "0123456789abcdef";
1035
1046
 
1050
1061
        return -1;
1051
1062
}
1052
1063
 
 
1064
char *hexmem(const void *p, size_t l) {
 
1065
        char *r, *z;
 
1066
        const uint8_t *x;
 
1067
 
 
1068
        z = r = malloc(l * 2 + 1);
 
1069
        if (!r)
 
1070
                return NULL;
 
1071
 
 
1072
        for (x = p; x < (const uint8_t*) p + l; x++) {
 
1073
                *(z++) = hexchar(*x >> 4);
 
1074
                *(z++) = hexchar(*x & 15);
 
1075
        }
 
1076
 
 
1077
        *z = 0;
 
1078
        return r;
 
1079
}
 
1080
 
 
1081
void *unhexmem(const char *p, size_t l) {
 
1082
        uint8_t *r, *z;
 
1083
        const char *x;
 
1084
 
 
1085
        assert(p);
 
1086
 
 
1087
        z = r = malloc((l + 1) / 2 + 1);
 
1088
        if (!r)
 
1089
                return NULL;
 
1090
 
 
1091
        for (x = p; x < p + l; x += 2) {
 
1092
                int a, b;
 
1093
 
 
1094
                a = unhexchar(x[0]);
 
1095
                if (x+1 < p + l)
 
1096
                        b = unhexchar(x[1]);
 
1097
                else
 
1098
                        b = 0;
 
1099
 
 
1100
                *(z++) = (uint8_t) a << 4 | (uint8_t) b;
 
1101
        }
 
1102
 
 
1103
        *z = 0;
 
1104
        return r;
 
1105
}
 
1106
 
1053
1107
char octchar(int x) {
1054
1108
        return '0' + (x & 7);
1055
1109
}
1817
1871
}
1818
1872
 
1819
1873
int flush_fd(int fd) {
1820
 
        struct pollfd pollfd;
1821
 
 
1822
 
        zero(pollfd);
1823
 
        pollfd.fd = fd;
1824
 
        pollfd.events = POLLIN;
 
1874
        struct pollfd pollfd = {
 
1875
                .fd = fd,
 
1876
                .events = POLLIN,
 
1877
        };
1825
1878
 
1826
1879
        for (;;) {
1827
1880
                char buf[LINE_MAX];
1828
1881
                ssize_t l;
1829
1882
                int r;
1830
1883
 
1831
 
                if ((r = poll(&pollfd, 1, 0)) < 0) {
1832
 
 
 
1884
                r = poll(&pollfd, 1, 0);
 
1885
                if (r < 0) {
1833
1886
                        if (errno == EINTR)
1834
1887
                                continue;
1835
1888
 
1836
1889
                        return -errno;
1837
 
                }
1838
1890
 
1839
 
                if (r == 0)
 
1891
                } else if (r == 0)
1840
1892
                        return 0;
1841
1893
 
1842
 
                if ((l = read(fd, buf, sizeof(buf))) < 0) {
 
1894
                l = read(fd, buf, sizeof(buf));
 
1895
                if (l < 0) {
1843
1896
 
1844
1897
                        if (errno == EINTR)
1845
1898
                                continue;
1848
1901
                                return 0;
1849
1902
 
1850
1903
                        return -errno;
1851
 
                }
1852
 
 
1853
 
                if (l <= 0)
 
1904
                } else if (l == 0)
1854
1905
                        return 0;
1855
1906
        }
1856
1907
}
1864
1915
 
1865
1916
        int fd = -1, notify = -1, r = 0, wd = -1;
1866
1917
        usec_t ts = 0;
1867
 
        struct sigaction sa_old, sa_new;
1868
1918
 
1869
1919
        assert(name);
1870
1920
 
1899
1949
        }
1900
1950
 
1901
1951
        for (;;) {
 
1952
                struct sigaction sa_old, sa_new = {
 
1953
                        .sa_handler = SIG_IGN,
 
1954
                        .sa_flags = SA_RESTART,
 
1955
                };
 
1956
 
1902
1957
                if (notify >= 0) {
1903
1958
                        r = flush_fd(notify);
1904
1959
                        if (r < 0)
1914
1969
 
1915
1970
                /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
1916
1971
                 * if we already own the tty. */
1917
 
                zero(sa_new);
1918
 
                sa_new.sa_handler = SIG_IGN;
1919
 
                sa_new.sa_flags = SA_RESTART;
1920
1972
                assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
1921
1973
 
1922
1974
                /* First, try to get the tty */
2023
2075
}
2024
2076
 
2025
2077
int release_terminal(void) {
2026
 
        int r = 0, fd;
2027
 
        struct sigaction sa_old, sa_new;
 
2078
        int r = 0;
 
2079
        struct sigaction sa_old, sa_new = {
 
2080
                .sa_handler = SIG_IGN,
 
2081
                .sa_flags = SA_RESTART,
 
2082
        };
 
2083
        _cleanup_close_ int fd;
2028
2084
 
2029
 
        if ((fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC)) < 0)
 
2085
        fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC);
 
2086
        if (fd < 0)
2030
2087
                return -errno;
2031
2088
 
2032
2089
        /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
2033
2090
         * by our own TIOCNOTTY */
2034
 
 
2035
 
        zero(sa_new);
2036
 
        sa_new.sa_handler = SIG_IGN;
2037
 
        sa_new.sa_flags = SA_RESTART;
2038
2091
        assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0);
2039
2092
 
2040
2093
        if (ioctl(fd, TIOCNOTTY) < 0)
2042
2095
 
2043
2096
        assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0);
2044
2097
 
2045
 
        close_nointr_nofail(fd);
2046
2098
        return r;
2047
2099
}
2048
2100
 
2060
2112
}
2061
2113
 
2062
2114
int ignore_signals(int sig, ...) {
2063
 
        struct sigaction sa;
 
2115
        struct sigaction sa = {
 
2116
                .sa_handler = SIG_IGN,
 
2117
                .sa_flags = SA_RESTART,
 
2118
        };
2064
2119
        va_list ap;
2065
2120
        int r = 0;
2066
2121
 
2067
 
        zero(sa);
2068
 
        sa.sa_handler = SIG_IGN;
2069
 
        sa.sa_flags = SA_RESTART;
2070
2122
 
2071
2123
        if (sigaction(sig, &sa, NULL) < 0)
2072
2124
                r = -errno;
2081
2133
}
2082
2134
 
2083
2135
int default_signals(int sig, ...) {
2084
 
        struct sigaction sa;
 
2136
        struct sigaction sa = {
 
2137
                .sa_handler = SIG_DFL,
 
2138
                .sa_flags = SA_RESTART,
 
2139
        };
2085
2140
        va_list ap;
2086
2141
        int r = 0;
2087
2142
 
2088
 
        zero(sa);
2089
 
        sa.sa_handler = SIG_DFL;
2090
 
        sa.sa_flags = SA_RESTART;
2091
 
 
2092
2143
        if (sigaction(sig, &sa, NULL) < 0)
2093
2144
                r = -errno;
2094
2145
 
2137
2188
                                continue;
2138
2189
 
2139
2190
                        if (k < 0 && errno == EAGAIN && do_poll) {
2140
 
                                struct pollfd pollfd;
2141
 
 
2142
 
                                zero(pollfd);
2143
 
                                pollfd.fd = fd;
2144
 
                                pollfd.events = POLLIN;
 
2191
                                struct pollfd pollfd = {
 
2192
                                        .fd = fd,
 
2193
                                        .events = POLLIN,
 
2194
                                };
2145
2195
 
2146
2196
                                if (poll(&pollfd, 1, -1) < 0) {
2147
2197
                                        if (errno == EINTR)
2186
2236
                                continue;
2187
2237
 
2188
2238
                        if (k < 0 && errno == EAGAIN && do_poll) {
2189
 
                                struct pollfd pollfd;
2190
 
 
2191
 
                                zero(pollfd);
2192
 
                                pollfd.fd = fd;
2193
 
                                pollfd.events = POLLOUT;
 
2239
                                struct pollfd pollfd = {
 
2240
                                        .fd = fd,
 
2241
                                        .events = POLLOUT,
 
2242
                                };
2194
2243
 
2195
2244
                                if (poll(&pollfd, 1, -1) < 0) {
2196
2245
                                        if (errno == EINTR)
2246
2295
                errno = 0;
2247
2296
                l = strtoll(p, &e, 10);
2248
2297
 
2249
 
                if (errno != 0)
 
2298
                if (errno > 0)
2250
2299
                        return -errno;
2251
2300
 
2252
2301
                if (l < 0)
2338
2387
        }
2339
2388
}
2340
2389
 
 
2390
char* dirname_malloc(const char *path) {
 
2391
        char *d, *dir, *dir2;
 
2392
 
 
2393
        d = strdup(path);
 
2394
        if (!d)
 
2395
                return NULL;
 
2396
        dir = dirname(d);
 
2397
        assert(dir);
 
2398
 
 
2399
        if (dir != d) {
 
2400
                dir2 = strdup(dir);
 
2401
                free(d);
 
2402
                return dir2;
 
2403
        }
 
2404
 
 
2405
        return dir;
 
2406
}
 
2407
 
2341
2408
unsigned long long random_ull(void) {
2342
2409
        _cleanup_close_ int fd;
2343
2410
        uint64_t ull;
2506
2573
}
2507
2574
 
2508
2575
int get_ctty_devnr(pid_t pid, dev_t *d) {
2509
 
        int k;
2510
 
        char line[LINE_MAX], *p, *fn;
 
2576
        _cleanup_fclose_ FILE *f = NULL;
 
2577
        char line[LINE_MAX], *p;
2511
2578
        unsigned long ttynr;
2512
 
        FILE *f;
2513
 
 
2514
 
        if (asprintf(&fn, "/proc/%lu/stat", (unsigned long) (pid <= 0 ? getpid() : pid)) < 0)
2515
 
                return -ENOMEM;
 
2579
        const char *fn;
 
2580
        int k;
 
2581
 
 
2582
        assert(pid >= 0);
 
2583
        assert(d);
 
2584
 
 
2585
        if (pid == 0)
 
2586
                fn = "/proc/self/stat";
 
2587
        else
 
2588
                fn = procfs_file_alloca(pid, "stat");
2516
2589
 
2517
2590
        f = fopen(fn, "re");
2518
 
        free(fn);
2519
2591
        if (!f)
2520
2592
                return -errno;
2521
2593
 
2522
2594
        if (!fgets(line, sizeof(line), f)) {
2523
2595
                k = feof(f) ? -EIO : -errno;
2524
 
                fclose(f);
2525
2596
                return k;
2526
2597
        }
2527
2598
 
2528
 
        fclose(f);
2529
 
 
2530
2599
        p = strrchr(line, ')');
2531
2600
        if (!p)
2532
2601
                return -EIO;
2551
2620
 
2552
2621
int get_ctty(pid_t pid, dev_t *_devnr, char **r) {
2553
2622
        int k;
2554
 
        char fn[PATH_MAX], *s, *b, *p;
 
2623
        char fn[sizeof("/dev/char/")-1 + 2*DECIMAL_STR_MAX(unsigned) + 1 + 1], *s, *b, *p;
2555
2624
        dev_t devnr;
2556
2625
 
2557
2626
        assert(r);
2561
2630
                return k;
2562
2631
 
2563
2632
        snprintf(fn, sizeof(fn), "/dev/char/%u:%u", major(devnr), minor(devnr));
2564
 
        char_array_0(fn);
2565
2633
 
2566
2634
        k = readlink_malloc(fn, &s);
2567
2635
        if (k < 0) {
2711
2779
 
2712
2780
static int is_temporary_fs(struct statfs *s) {
2713
2781
        assert(s);
2714
 
        return s->f_type == TMPFS_MAGIC ||
2715
 
                (long)s->f_type == (long)RAMFS_MAGIC;
 
2782
        return s->f_type == (__SWORD_TYPE) TMPFS_MAGIC ||
 
2783
                s->f_type == (__SWORD_TYPE) RAMFS_MAGIC;
2716
2784
}
2717
2785
 
2718
2786
int rm_rf_children(int fd, bool only_dirs, bool honour_sticky, struct stat *root_dev) {
2876
2944
        static const char status_indent[] = "         "; /* "[" STATUS "] " */
2877
2945
        _cleanup_free_ char *s = NULL;
2878
2946
        _cleanup_close_ int fd = -1;
2879
 
        struct iovec iovec[6];
 
2947
        struct iovec iovec[6] = {};
2880
2948
        int n = 0;
2881
2949
        static bool prev_ephemeral;
2882
2950
 
2914
2982
                }
2915
2983
        }
2916
2984
 
2917
 
        zero(iovec);
2918
 
 
2919
2985
        if (prev_ephemeral)
2920
2986
                IOVEC_SET_STRING(iovec[n++], "\r" ANSI_ERASE_TO_END_OF_LINE);
2921
2987
        prev_ephemeral = ephemeral;
2963
3029
        if (r < 0 && r != -ENOENT)
2964
3030
                log_warning("Failed to read /etc/os-release: %s", strerror(-r));
2965
3031
 
2966
 
        return status_printf(NULL, false,
 
3032
        return status_printf(NULL, false, false,
2967
3033
                             "\nWelcome to \x1B[%sm%s\x1B[0m!\n",
2968
3034
                             isempty(ansi_color) ? "1" : ansi_color,
2969
3035
                             isempty(pretty_name) ? "Linux" : pretty_name);
3105
3171
}
3106
3172
 
3107
3173
int fd_columns(int fd) {
3108
 
        struct winsize ws;
3109
 
        zero(ws);
 
3174
        struct winsize ws = {};
3110
3175
 
3111
3176
        if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
3112
3177
                return -errno;
3140
3205
}
3141
3206
 
3142
3207
int fd_lines(int fd) {
3143
 
        struct winsize ws;
3144
 
        zero(ws);
 
3208
        struct winsize ws = {};
3145
3209
 
3146
3210
        if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
3147
3211
                return -errno;
3190
3254
}
3191
3255
 
3192
3256
int running_in_chroot(void) {
3193
 
        struct stat a, b;
3194
 
 
3195
 
        zero(a);
3196
 
        zero(b);
 
3257
        struct stat a = {}, b = {};
3197
3258
 
3198
3259
        /* Only works as root */
3199
 
 
3200
3260
        if (stat("/proc/1/root", &a) < 0)
3201
3261
                return -errno;
3202
3262
 
3674
3734
 
3675
3735
        while (!hashmap_isempty(pids)) {
3676
3736
                pid_t pid = PTR_TO_UINT(hashmap_first_key(pids));
3677
 
                siginfo_t si;
 
3737
                siginfo_t si = {};
3678
3738
                char *path;
3679
3739
 
3680
 
                zero(si);
3681
3740
                if (waitid(P_PID, pid, &si, WEXITED) < 0) {
3682
3741
 
3683
3742
                        if (errno == EINTR)
3757
3816
 
3758
3817
bool hostname_is_valid(const char *s) {
3759
3818
        const char *p;
 
3819
        bool dot;
3760
3820
 
3761
3821
        if (isempty(s))
3762
3822
                return false;
3763
3823
 
3764
 
        for (p = s; *p; p++)
3765
 
                if (!hostname_valid_char(*p))
3766
 
                        return false;
 
3824
        for (p = s, dot = true; *p; p++) {
 
3825
                if (*p == '.') {
 
3826
                        if (dot)
 
3827
                                return false;
 
3828
 
 
3829
                        dot = true;
 
3830
                } else {
 
3831
                        if (!hostname_valid_char(*p))
 
3832
                                return false;
 
3833
 
 
3834
                        dot = false;
 
3835
                }
 
3836
        }
 
3837
 
 
3838
        if (dot)
 
3839
                return false;
3767
3840
 
3768
3841
        if (p-s > HOST_NAME_MAX)
3769
3842
                return false;
3773
3846
 
3774
3847
char* hostname_cleanup(char *s) {
3775
3848
        char *p, *d;
3776
 
 
3777
 
        for (p = s, d = s; *p; p++)
3778
 
                if ((*p >= 'a' && *p <= 'z') ||
3779
 
                    (*p >= 'A' && *p <= 'Z') ||
3780
 
                    (*p >= '0' && *p <= '9') ||
3781
 
                    *p == '-' ||
3782
 
                    *p == '_' ||
3783
 
                    *p == '.')
 
3849
        bool dot;
 
3850
 
 
3851
        for (p = s, d = s, dot = true; *p; p++) {
 
3852
                if (*p == '.') {
 
3853
                        if (dot || p[1] == 0)
 
3854
                                continue;
 
3855
 
 
3856
                        dot = true;
 
3857
                } else
 
3858
                        dot = false;
 
3859
 
 
3860
                if (hostname_valid_char(*p))
3784
3861
                        *(d++) = *p;
 
3862
        }
3785
3863
 
3786
3864
        *d = 0;
3787
 
 
3788
3865
        strshorten(s, HOST_NAME_MAX);
 
3866
 
3789
3867
        return s;
3790
3868
}
3791
3869
 
3792
3870
int pipe_eof(int fd) {
3793
 
        struct pollfd pollfd;
3794
3871
        int r;
3795
 
 
3796
 
        zero(pollfd);
3797
 
        pollfd.fd = fd;
3798
 
        pollfd.events = POLLIN|POLLHUP;
 
3872
        struct pollfd pollfd = {
 
3873
                .fd = fd,
 
3874
                .events = POLLIN|POLLHUP,
 
3875
        };
3799
3876
 
3800
3877
        r = poll(&pollfd, 1, 0);
3801
3878
        if (r < 0)
3808
3885
}
3809
3886
 
3810
3887
int fd_wait_for_event(int fd, int event, usec_t t) {
3811
 
        struct pollfd pollfd;
3812
3888
        int r;
3813
 
 
3814
 
        zero(pollfd);
3815
 
        pollfd.fd = fd;
3816
 
        pollfd.events = event;
 
3889
        struct pollfd pollfd = {
 
3890
                .fd = fd,
 
3891
                .events = event,
 
3892
        };
3817
3893
 
3818
3894
        r = poll(&pollfd, 1, t == (usec_t) -1 ? -1 : (int) (t / USEC_PER_MSEC));
3819
3895
        if (r < 0)
4140
4216
        }
4141
4217
 
4142
4218
        if (!p)
4143
 
                return errno != 0 ? -errno : -ESRCH;
 
4219
                return errno > 0 ? -errno : -ESRCH;
4144
4220
 
4145
4221
        if (uid)
4146
4222
                *uid = p->pw_uid;
4174
4250
        return r;
4175
4251
}
4176
4252
 
 
4253
char* gid_to_name(gid_t gid) {
 
4254
        struct group *p;
 
4255
        char *r;
 
4256
 
 
4257
        if (gid == 0)
 
4258
                return strdup("root");
 
4259
 
 
4260
        p = getgrgid(gid);
 
4261
        if (p)
 
4262
                return strdup(p->gr_name);
 
4263
 
 
4264
        if (asprintf(&r, "%lu", (unsigned long) gid) < 0)
 
4265
                return NULL;
 
4266
 
 
4267
        return r;
 
4268
}
 
4269
 
4177
4270
int get_group_creds(const char **groupname, gid_t *gid) {
4178
4271
        struct group *g;
4179
4272
        gid_t id;
4204
4297
        }
4205
4298
 
4206
4299
        if (!g)
4207
 
                return errno != 0 ? -errno : -ESRCH;
 
4300
                return errno > 0 ? -errno : -ESRCH;
4208
4301
 
4209
4302
        if (gid)
4210
4303
                *gid = g->gr_gid;
4212
4305
        return 0;
4213
4306
}
4214
4307
 
4215
 
int in_group(const char *name) {
4216
 
        gid_t gid, *gids;
 
4308
int in_gid(gid_t gid) {
 
4309
        gid_t *gids;
4217
4310
        int ngroups_max, r, i;
4218
4311
 
4219
 
        r = get_group_creds(&name, &gid);
4220
 
        if (r < 0)
4221
 
                return r;
4222
 
 
4223
4312
        if (getgid() == gid)
4224
4313
                return 1;
4225
4314
 
4242
4331
        return 0;
4243
4332
}
4244
4333
 
 
4334
int in_group(const char *name) {
 
4335
        int r;
 
4336
        gid_t gid;
 
4337
 
 
4338
        r = get_group_creds(&name, &gid);
 
4339
        if (r < 0)
 
4340
                return r;
 
4341
 
 
4342
        return in_gid(gid);
 
4343
}
 
4344
 
4245
4345
int glob_exists(const char *path) {
4246
 
        glob_t g;
 
4346
        _cleanup_globfree_ glob_t g = {};
4247
4347
        int r, k;
4248
4348
 
4249
4349
        assert(path);
4250
4350
 
4251
 
        zero(g);
4252
4351
        errno = 0;
4253
4352
        k = glob(path, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
4254
4353
 
4261
4360
        else
4262
4361
                r = errno ? -errno : -EIO;
4263
4362
 
4264
 
        globfree(&g);
4265
 
 
4266
4363
        return r;
4267
4364
}
4268
4365
 
4664
4761
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
4665
4762
 
4666
4763
const char *signal_to_string(int signo) {
4667
 
        static __thread char buf[12];
 
4764
        static __thread char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
4668
4765
        const char *name;
4669
4766
 
4670
4767
        name = __signal_to_string(signo);
4672
4769
                return name;
4673
4770
 
4674
4771
        if (signo >= SIGRTMIN && signo <= SIGRTMAX)
4675
 
                snprintf(buf, sizeof(buf) - 1, "RTMIN+%d", signo - SIGRTMIN);
 
4772
                snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
4676
4773
        else
4677
 
                snprintf(buf, sizeof(buf) - 1, "%d", signo);
4678
 
        char_array_0(buf);
 
4774
                snprintf(buf, sizeof(buf), "%d", signo);
 
4775
 
4679
4776
        return buf;
4680
4777
}
4681
4778
 
4943
5040
}
4944
5041
 
4945
5042
int getenv_for_pid(pid_t pid, const char *field, char **_value) {
4946
 
        char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL;
 
5043
        _cleanup_fclose_ FILE *f = NULL;
 
5044
        char *value = NULL;
4947
5045
        int r;
4948
 
        FILE *f;
4949
5046
        bool done = false;
4950
5047
        size_t l;
 
5048
        const char *path;
4951
5049
 
 
5050
        assert(pid >= 0);
4952
5051
        assert(field);
4953
5052
        assert(_value);
4954
5053
 
4955
5054
        if (pid == 0)
4956
 
                pid = getpid();
4957
 
 
4958
 
        snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
4959
 
        char_array_0(path);
 
5055
                path = "/proc/self/environ";
 
5056
        else
 
5057
                path = procfs_file_alloca(pid, "environ");
4960
5058
 
4961
5059
        f = fopen(path, "re");
4962
5060
        if (!f)
4985
5083
 
4986
5084
                if (memcmp(line, field, l) == 0 && line[l] == '=') {
4987
5085
                        value = strdup(line + l + 1);
4988
 
                        if (!value) {
4989
 
                                r = -ENOMEM;
4990
 
                                break;
4991
 
                        }
 
5086
                        if (!value)
 
5087
                                return -ENOMEM;
4992
5088
 
4993
5089
                        r = 1;
4994
5090
                        break;
4996
5092
 
4997
5093
        } while (!done);
4998
5094
 
4999
 
        fclose(f);
5000
 
 
5001
 
        if (r >= 0)
5002
 
                *_value = value;
5003
 
 
 
5095
        *_value = value;
5004
5096
        return r;
5005
5097
}
5006
5098
 
5177
5269
        errno = 0;
5178
5270
        p = getpwuid(u);
5179
5271
        if (!p)
5180
 
                return errno ? -errno : -ESRCH;
 
5272
                return errno > 0 ? -errno : -ESRCH;
5181
5273
 
5182
5274
        if (!path_is_absolute(p->pw_dir))
5183
5275
                return -EINVAL;
5190
5282
        return 0;
5191
5283
}
5192
5284
 
5193
 
int get_shell(char **_sh) {
5194
 
        char *sh;
5195
 
        const char *e;
5196
 
        uid_t u;
5197
 
        struct passwd *p;
5198
 
 
5199
 
        assert(_sh);
5200
 
 
5201
 
        /* Take the user specified one */
5202
 
        e = getenv("SHELL");
5203
 
        if (e) {
5204
 
                sh = strdup(e);
5205
 
                if (!sh)
5206
 
                        return -ENOMEM;
5207
 
 
5208
 
                *_sh = sh;
5209
 
                return 0;
5210
 
        }
5211
 
 
5212
 
        /* Hardcode home directory for root to avoid NSS */
5213
 
        u = getuid();
5214
 
        if (u == 0) {
5215
 
                sh = strdup("/bin/sh");
5216
 
                if (!sh)
5217
 
                        return -ENOMEM;
5218
 
 
5219
 
                *_sh = sh;
5220
 
                return 0;
5221
 
        }
5222
 
 
5223
 
        /* Check the database... */
5224
 
        errno = 0;
5225
 
        p = getpwuid(u);
5226
 
        if (!p)
5227
 
                return errno ? -errno : -ESRCH;
5228
 
 
5229
 
        if (!path_is_absolute(p->pw_shell))
5230
 
                return -EINVAL;
5231
 
 
5232
 
        sh = strdup(p->pw_shell);
5233
 
        if (!sh)
5234
 
                return -ENOMEM;
5235
 
 
5236
 
        *_sh = sh;
5237
 
        return 0;
5238
 
}
5239
 
 
5240
 
void freep(void *p) {
5241
 
        free(*(void**) p);
5242
 
}
5243
 
 
5244
 
void fclosep(FILE **f) {
5245
 
        if (*f)
5246
 
                fclose(*f);
5247
 
}
5248
 
 
5249
 
void pclosep(FILE **f) {
5250
 
        if (*f)
5251
 
                pclose(*f);
5252
 
}
5253
 
 
5254
 
void closep(int *fd) {
5255
 
        if (*fd >= 0)
5256
 
                close_nointr_nofail(*fd);
5257
 
}
5258
 
 
5259
 
void closedirp(DIR **d) {
5260
 
        if (*d)
5261
 
                closedir(*d);
5262
 
}
5263
 
 
5264
 
void umaskp(mode_t *u) {
5265
 
        umask(*u);
5266
 
}
5267
 
 
5268
5285
bool filename_is_safe(const char *p) {
5269
5286
 
5270
5287
        if (isempty(p))
5375
5392
                goto out;
5376
5393
        }
5377
5394
 
5378
 
        cached_answer = streq(set, "UTF-8");
 
5395
        if(streq(set, "UTF-8")) {
 
5396
                cached_answer = true;
 
5397
                goto out;
 
5398
        }
 
5399
 
 
5400
        /* For LC_CTYPE=="C" return true,
 
5401
         * because CTYPE is effectly unset and
 
5402
         * everything defaults to UTF-8 nowadays. */
 
5403
 
 
5404
        set = setlocale(LC_CTYPE, NULL);
 
5405
        if (!set) {
 
5406
                cached_answer = true;
 
5407
                goto out;
 
5408
        }
 
5409
 
 
5410
        cached_answer = streq(set, "C");
 
5411
 
5379
5412
out:
5380
5413
        return (bool)cached_answer;
5381
5414
}
5545
5578
        for (;;) {
5546
5579
                struct dirent *de;
5547
5580
                union dirent_storage buf;
5548
 
                _cleanup_free_ char *p = NULL;
5549
5581
                _cleanup_close_ int fd = -1, device = -1;
5550
5582
                char contents[6];
5551
5583
                ssize_t n;
5690
5722
 
5691
5723
        return search_and_fopen_internal(path, mode, s, _f);
5692
5724
}
 
5725
 
 
5726
int create_tmp_dir(char template[], char** dir_name) {
 
5727
        int r = 0;
 
5728
        char *d, *dt;
 
5729
 
 
5730
        assert(dir_name);
 
5731
 
 
5732
        RUN_WITH_UMASK(0077) {
 
5733
                d = mkdtemp(template);
 
5734
        }
 
5735
        if (!d) {
 
5736
                log_error("Can't create directory %s: %m", template);
 
5737
                return -errno;
 
5738
        }
 
5739
 
 
5740
        dt = strjoin(d, "/tmp", NULL);
 
5741
        if (!dt) {
 
5742
                r = log_oom();
 
5743
                goto fail3;
 
5744
        }
 
5745
 
 
5746
        RUN_WITH_UMASK(0000) {
 
5747
                r = mkdir(dt, 0777);
 
5748
        }
 
5749
        if (r < 0) {
 
5750
                log_error("Can't create directory %s: %m", dt);
 
5751
                r = -errno;
 
5752
                goto fail2;
 
5753
        }
 
5754
        log_debug("Created temporary directory %s", dt);
 
5755
 
 
5756
        r = chmod(dt, 0777 | S_ISVTX);
 
5757
        if (r < 0) {
 
5758
                log_error("Failed to chmod %s: %m", dt);
 
5759
                r = -errno;
 
5760
                goto fail1;
 
5761
        }
 
5762
        log_debug("Set sticky bit on %s", dt);
 
5763
 
 
5764
        *dir_name = dt;
 
5765
 
 
5766
        return 0;
 
5767
fail1:
 
5768
        rmdir(dt);
 
5769
fail2:
 
5770
        free(dt);
 
5771
fail3:
 
5772
        rmdir(template);
 
5773
        return r;
 
5774
}
 
5775
 
 
5776
char *strextend(char **x, ...) {
 
5777
        va_list ap;
 
5778
        size_t f, l;
 
5779
        char *r, *p;
 
5780
 
 
5781
        assert(x);
 
5782
 
 
5783
        l = f = *x ? strlen(*x) : 0;
 
5784
 
 
5785
        va_start(ap, x);
 
5786
        for (;;) {
 
5787
                const char *t;
 
5788
                size_t n;
 
5789
 
 
5790
                t = va_arg(ap, const char *);
 
5791
                if (!t)
 
5792
                        break;
 
5793
 
 
5794
                n = strlen(t);
 
5795
                if (n > ((size_t) -1) - l) {
 
5796
                        va_end(ap);
 
5797
                        return NULL;
 
5798
                }
 
5799
 
 
5800
                l += n;
 
5801
        }
 
5802
        va_end(ap);
 
5803
 
 
5804
        r = realloc(*x, l+1);
 
5805
        if (!r)
 
5806
                return NULL;
 
5807
 
 
5808
        p = r + f;
 
5809
 
 
5810
        va_start(ap, x);
 
5811
        for (;;) {
 
5812
                const char *t;
 
5813
 
 
5814
                t = va_arg(ap, const char *);
 
5815
                if (!t)
 
5816
                        break;
 
5817
 
 
5818
                p = stpcpy(p, t);
 
5819
        }
 
5820
        va_end(ap);
 
5821
 
 
5822
        *p = 0;
 
5823
        *x = r;
 
5824
 
 
5825
        return r + l;
 
5826
}
 
5827
 
 
5828
char *strrep(const char *s, unsigned n) {
 
5829
        size_t l;
 
5830
        char *r, *p;
 
5831
        unsigned i;
 
5832
 
 
5833
        assert(s);
 
5834
 
 
5835
        l = strlen(s);
 
5836
        p = r = malloc(l * n + 1);
 
5837
        if (!r)
 
5838
                return NULL;
 
5839
 
 
5840
        for (i = 0; i < n; i++)
 
5841
                p = stpcpy(p, s);
 
5842
 
 
5843
        *p = 0;
 
5844
        return r;
 
5845
}
 
5846
 
 
5847
void* greedy_realloc(void **p, size_t *allocated, size_t need) {
 
5848
        size_t a;
 
5849
        void *q;
 
5850
 
 
5851
        if (*allocated >= need)
 
5852
                return *p;
 
5853
 
 
5854
        a = MAX(64u, need * 2);
 
5855
        q = realloc(*p, a);
 
5856
        if (!q)
 
5857
                return NULL;
 
5858
 
 
5859
        *p = q;
 
5860
        *allocated = a;
 
5861
        return q;
 
5862
}