2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2011 Jason King. All rights reserved.
20 #include <sys/types.h>
27 #define MCS "/usr/bin/mcs"
30 static const char elf_signature
[] = "\177ELF";
31 static posix_spawnattr_t attr
;
32 static const char *cmd
[] = { MCS
, "-d", "-n", ".SUNW_ctf", NULL
, NULL
};
34 extern char **environ
;
36 static boolean_t
check_file(const char *, mode_t
*);
37 static boolean_t
fix_file(const char *, mode_t
);
38 static void usage(const char *);
41 main(int argc
, const char **argv
)
50 rc
= posix_spawnattr_init(&attr
);
52 errx(EXIT_FAILURE
, "Spawn attribute initialization failed: %s",
56 for (p
= argv
+ 1; *p
!= NULL
; p
++) {
57 if (!check_file(*p
, &mode
))
59 if (!fix_file(*p
, mode
))
67 check_file(const char *filename
, mode_t
*mode
)
73 fd
= open(filename
, O_RDONLY
);
75 warn("Unable to open %s", filename
);
79 if (fstat(fd
, &sb
) == -1) {
80 warn("stat(2) failed on %s", filename
);
85 if (!S_ISREG(sb
.st_mode
)) {
86 warnx("%s is not a regular file", filename
);
91 if (sb
.st_size
< ELFLEN
) {
92 warnx("%s is not an ELF file", filename
);
97 if (read(fd
, elfbuf
, ELFLEN
) != ELFLEN
) {
98 warn("Error reading %s", filename
);
103 if (strncmp(elfbuf
, elf_signature
, ELFLEN
) != 0) {
104 warnx("%s is not an ELF file", filename
);
109 *mode
= sb
.st_mode
& S_IAMB
;
115 fix_file(const char *filename
, mode_t mode
)
121 if ((mode
& S_IWUSR
) == 0) {
122 if (chmod(filename
, mode
| S_IWUSR
) == -1) {
123 warn("failed to make %s writable", filename
);
129 if ((rc
= posix_spawn(&pid
, MCS
, NULL
, &attr
,
130 (char *const *)cmd
, environ
)) != 0) {
131 warnx("could not exec mcs: %s", strerror(rc
));
135 waitpid(pid
, &stat
, 0);
136 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
) != 0) {
137 warnx("Removing CTF information from %s failed", filename
);
141 if ((mode
& S_IWUSR
) == 0) {
142 if (chmod(filename
, mode
) == -1) {
143 warn("could not reset permissions of %s", filename
);
152 usage(const char *name
)
154 (void) fprintf(stderr
, "Usage: %s file...\n", name
);