The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <unistd.h> |
| 5 | #include <malloc.h> |
| 6 | #include <errno.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | extern int init_module(void *, unsigned long, const char *); |
| 12 | |
| 13 | static void *read_file(const char *filename, ssize_t *_size) |
| 14 | { |
| 15 | int ret, fd; |
| 16 | struct stat sb; |
| 17 | ssize_t size; |
| 18 | void *buffer = NULL; |
| 19 | |
| 20 | /* open the file */ |
| 21 | fd = open(filename, O_RDONLY); |
| 22 | if (fd < 0) |
| 23 | return NULL; |
| 24 | |
| 25 | /* find out how big it is */ |
| 26 | if (fstat(fd, &sb) < 0) |
| 27 | goto bail; |
| 28 | size = sb.st_size; |
| 29 | |
| 30 | /* allocate memory for it to be read into */ |
| 31 | buffer = malloc(size); |
| 32 | if (!buffer) |
| 33 | goto bail; |
| 34 | |
| 35 | /* slurp it into our buffer */ |
| 36 | ret = read(fd, buffer, size); |
| 37 | if (ret != size) |
| 38 | goto bail; |
| 39 | |
| 40 | /* let the caller know how big it is */ |
| 41 | *_size = size; |
| 42 | |
| 43 | bail: |
| 44 | close(fd); |
| 45 | return buffer; |
| 46 | } |
| 47 | |
| 48 | int insmod_main(int argc, char **argv) |
| 49 | { |
| 50 | void *file; |
| 51 | ssize_t size; |
| 52 | int ret; |
| 53 | |
| 54 | /* make sure we've got an argument */ |
| 55 | if (argc < 2) { |
| 56 | fprintf(stderr, "usage: insmod <module.o>\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /* read the file into memory */ |
| 61 | file = read_file(argv[1], &size); |
| 62 | if (!file) { |
| 63 | fprintf(stderr, "insmod: can't open '%s'\n", argv[1]); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | /* pass it to the kernel */ |
| 68 | /* XXX options */ |
| 69 | ret = init_module(file, size, ""); |
| 70 | if (ret != 0) { |
| 71 | fprintf(stderr, |
| 72 | "insmod: init_module '%s' failed (%s)\n", |
| 73 | argv[1], strerror(errno)); |
| 74 | } |
| 75 | |
| 76 | /* free the file buffer */ |
| 77 | free(file); |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |