Print this page
10075 make usr/src/tools smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/ctf/ctfstrip/ctfstrip.c
+++ new/usr/src/tools/ctf/ctfstrip/ctfstrip.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2011 Jason King. All rights reserved.
14 + * Copyright (c) 2018, Joyent, Inc.
14 15 */
15 16
16 17 #include <stdlib.h>
17 18 #include <stdio.h>
18 19 #include <string.h>
19 20 #include <unistd.h>
20 21 #include <sys/types.h>
21 22 #include <sys/stat.h>
22 23 #include <sys/wait.h>
23 24 #include <fcntl.h>
24 25 #include <err.h>
25 26 #include <spawn.h>
26 27
27 28 #define MCS "/usr/bin/mcs"
28 29
29 30 #define ELFLEN 4
30 31 static const char elf_signature[] = "\177ELF";
31 32 static posix_spawnattr_t attr;
32 33 static const char *cmd[] = { MCS, "-d", "-n", ".SUNW_ctf", NULL, NULL };
33 34
34 35 extern char **environ;
35 36
36 37 static boolean_t check_file(const char *, mode_t *);
37 38 static boolean_t fix_file(const char *, mode_t);
38 39 static void usage(const char *);
39 40
40 41 int
41 42 main(int argc, const char **argv)
42 43 {
43 44 const char **p;
44 45 int rc = 0;
45 46 mode_t mode;
46 47
47 48 if (argc < 2)
48 49 usage(argv[0]);
49 50
50 51 rc = posix_spawnattr_init(&attr);
51 52 if (rc != 0) {
52 53 errx(EXIT_FAILURE, "Spawn attribute initialization failed: %s",
53 54 strerror(rc));
54 55 }
55 56
56 57 for (p = argv + 1; *p != NULL; p++) {
57 58 if (!check_file(*p, &mode))
58 59 continue;
59 60 if (!fix_file(*p, mode))
60 61 rc = 1;
61 62 }
62 63
63 64 return (rc);
64 65 }
65 66
66 67 static boolean_t
67 68 check_file(const char *filename, mode_t *mode)
68 69 {
69 70 char elfbuf[4];
70 71 struct stat sb;
71 72 int fd;
72 73
73 74 fd = open(filename, O_RDONLY);
74 75 if (fd == -1) {
75 76 warn("Unable to open %s", filename);
76 77 return (B_FALSE);
77 78 }
78 79
79 80 if (fstat(fd, &sb) == -1) {
80 81 warn("stat(2) failed on %s", filename);
81 82 (void) close(fd);
82 83 return (B_FALSE);
83 84 }
84 85
85 86 if (!S_ISREG(sb.st_mode)) {
86 87 warnx("%s is not a regular file", filename);
87 88 (void) close(fd);
88 89 return (B_FALSE);
89 90 }
90 91
91 92 if (sb.st_size < ELFLEN) {
92 93 warnx("%s is not an ELF file", filename);
93 94 (void) close(fd);
94 95 return (B_FALSE);
95 96 }
96 97
97 98 if (read(fd, elfbuf, ELFLEN) != ELFLEN) {
98 99 warn("Error reading %s", filename);
99 100 (void) close(fd);
100 101 return (B_FALSE);
101 102 }
102 103
103 104 if (strncmp(elfbuf, elf_signature, ELFLEN) != 0) {
104 105 warnx("%s is not an ELF file", filename);
105 106 (void) close(fd);
106 107 return (B_FALSE);
107 108 }
108 109
109 110 *mode = sb.st_mode & S_IAMB;
110 111 (void) close(fd);
111 112 return (B_TRUE);
112 113 }
113 114
114 115 static boolean_t
115 116 fix_file(const char *filename, mode_t mode)
116 117 {
117 118 pid_t pid;
118 119 int i, rc;
119 120 int stat = 0;
120 121
121 122 if ((mode & S_IWUSR) == 0) {
122 123 if (chmod(filename, mode | S_IWUSR) == -1) {
123 124 warn("failed to make %s writable", filename);
124 125 return (B_FALSE);
↓ open down ↓ |
101 lines elided |
↑ open up ↑ |
125 126 }
126 127 }
127 128
128 129 cmd[4] = filename;
129 130 if ((rc = posix_spawn(&pid, MCS, NULL, &attr,
130 131 (char *const *)cmd, environ)) != 0) {
131 132 warnx("could not exec mcs: %s", strerror(rc));
132 133 return (B_FALSE);
133 134 }
134 135
135 - waitpid(pid, &stat, 0);
136 + (void) waitpid(pid, &stat, 0);
136 137 if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0) {
137 138 warnx("Removing CTF information from %s failed", filename);
138 139 return (B_FALSE);
139 140 }
140 141
141 142 if ((mode & S_IWUSR) == 0) {
142 143 if (chmod(filename, mode) == -1) {
143 144 warn("could not reset permissions of %s", filename);
144 145 return (B_FALSE);
145 146 }
146 147 }
147 148
148 149 return (B_TRUE);
149 150 }
150 151
151 152 static void
152 153 usage(const char *name)
153 154 {
154 155 (void) fprintf(stderr, "Usage: %s file...\n", name);
155 156 exit(EXIT_FAILURE);
156 157 }
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX