| 1 |
/* |
| 2 |
* Copyright 2003 Ned Ludd <solar@gentoo.org> |
| 3 |
* Copyright 1999-2003 Gentoo Technologies, Inc. |
| 4 |
* Distributed under the terms of the GNU General Public License v2 |
| 5 |
* $Header: /home/cvsroot/gentoo-projects/pax-utils/scanelf.c,v 1.4 2003/11/01 08:58:23 solar Exp $ |
| 6 |
* |
| 7 |
******************************************************************** |
| 8 |
* This program is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License as |
| 10 |
* published by the Free Software Foundation; either version 2 of the |
| 11 |
* License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 21 |
* MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <string.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <dirent.h> |
| 31 |
#include <getopt.h> |
| 32 |
|
| 33 |
#include "paxelf.h" |
| 34 |
|
| 35 |
static const char *rcsid = |
| 36 |
"$Id: scanelf.c,v 1.4 2003/11/01 08:58:23 solar Exp $"; |
| 37 |
|
| 38 |
#define PARSE_FLAGS "hvlp" |
| 39 |
static struct option const long_options[] = { |
| 40 |
{"help", no_argument, 0, 'h'}, |
| 41 |
{"version", no_argument, 0, 'v'}, |
| 42 |
{"path", no_argument, 0, 'p'}, |
| 43 |
{"ldpath", no_argument, 0, 'l'}, |
| 44 |
{NULL, no_argument, NULL, 0} |
| 45 |
}; |
| 46 |
|
| 47 |
/* scan a directory for ET_EXEC files and print when we find one */ |
| 48 |
void scanelf(const char *path) |
| 49 |
{ |
| 50 |
elfobj *elf = NULL; |
| 51 |
register DIR *dir; |
| 52 |
register struct dirent *dentry; |
| 53 |
|
| 54 |
if ((chdir(path) == 0) && ((dir = opendir(path)))) { |
| 55 |
while ((dentry = readdir(dir))) { |
| 56 |
/* verify this is real ELF */ |
| 57 |
if ((elf = readelf(dentry->d_name)) != NULL) { |
| 58 |
if (!check_elf_header(elf->ehdr)) |
| 59 |
if (IS_ELF(elf)) |
| 60 |
printf("%s %s %s/%s\n", |
| 61 |
pax_short_flags(PAX_FLAGS(elf)), |
| 62 |
get_elfetype(elf->ehdr->e_type), path, |
| 63 |
dentry->d_name); |
| 64 |
|
| 65 |
if (elf != NULL) { |
| 66 |
munmap(elf->data, elf->len); |
| 67 |
free(elf); |
| 68 |
elf = NULL; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
closedir(dir); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/* display usage and exit */ |
| 78 |
void usage(char **argv) |
| 79 |
{ |
| 80 |
fprintf(stderr, |
| 81 |
"Usage: %s [options] dir1 dir2 dirN...\n", |
| 82 |
(*argv != NULL) ? argv[0] : __FILE__ "\b\b"); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void showopt(int c, char *data) |
| 87 |
{ |
| 88 |
int i; |
| 89 |
for (i = 0; long_options[i].name; i++) |
| 90 |
if (long_options[i].val == c) |
| 91 |
fprintf(stderr, " -%c, --%s\t: %s\n", c, long_options[i].name, |
| 92 |
data); |
| 93 |
} |
| 94 |
|
| 95 |
/* parse command line arguments and preform needed actions */ |
| 96 |
void parseargs(int argc, char **argv) |
| 97 |
{ |
| 98 |
int flag; |
| 99 |
char *p, *path; |
| 100 |
FILE *fp; |
| 101 |
|
| 102 |
opterr = 0; |
| 103 |
while ((flag = |
| 104 |
(int) getopt_long(argc, argv, |
| 105 |
PARSE_FLAGS, long_options, NULL)) != EOF) { |
| 106 |
switch (flag) { |
| 107 |
case 'h': |
| 108 |
usage(argv); |
| 109 |
showopt('p', "Scan all directories in PATH environment."); |
| 110 |
showopt('l', "Scan all directories in /etc/ld.so.conf"); |
| 111 |
showopt('h', "Print this help and exit."); |
| 112 |
showopt('v', "Print version and exit."); |
| 113 |
exit(EXIT_SUCCESS); |
| 114 |
case 'v': |
| 115 |
fprintf(stderr, "%s compiled %s\n", __FILE__, __DATE__); |
| 116 |
fprintf(stderr, |
| 117 |
"%s written for Gentoo Linux <solar@gentoo.org>\n\t%s\n", |
| 118 |
(*argv != NULL) ? argv[0] : __FILE__ "\b\b", rcsid); |
| 119 |
exit(EXIT_SUCCESS); |
| 120 |
case 'l': |
| 121 |
/* scan ld.so.conf for ldpath */ |
| 122 |
if ((fp = fopen("/etc/ld.so.conf", "r")) != NULL) { |
| 123 |
path = malloc(_POSIX_PATH_MAX); |
| 124 |
while ((fgets(path, _POSIX_PATH_MAX, fp)) != NULL) { |
| 125 |
if (*path == '/') { |
| 126 |
if ((p = strrchr(path, '\r')) != NULL) |
| 127 |
*p = 0; |
| 128 |
if ((p = strrchr(path, '\n')) != NULL) |
| 129 |
*p = 0; |
| 130 |
scanelf(path); |
| 131 |
} |
| 132 |
} |
| 133 |
free(path); |
| 134 |
} |
| 135 |
break; |
| 136 |
case 'p': |
| 137 |
if ((path = strdup(getenv("PATH"))) == NULL) { |
| 138 |
perror("strdup"); |
| 139 |
exit(EXIT_FAILURE); |
| 140 |
} |
| 141 |
/* split string into dirs */ |
| 142 |
while ((p = strrchr(path, ':')) != NULL) { |
| 143 |
scanelf(p + 1); |
| 144 |
*p = 0; |
| 145 |
} |
| 146 |
if (path != NULL) |
| 147 |
free(path); |
| 148 |
break; |
| 149 |
case '?': |
| 150 |
default: |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
while (optind < argc) |
| 155 |
scanelf(argv[optind++]); |
| 156 |
} |
| 157 |
|
| 158 |
int main(int argc, char **argv) |
| 159 |
{ |
| 160 |
if (argc < 2) { |
| 161 |
usage(argv); |
| 162 |
exit(EXIT_FAILURE); |
| 163 |
} |
| 164 |
parseargs(argc, argv); |
| 165 |
return 0; |
| 166 |
} |