Created
February 28, 2023 21:43
-
-
Save throwaway96/da98d52b7700e4aedbda7bebaab54a7c to your computer and use it in GitHub Desktop.
Program to test reading from /proc/self/auxv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stddef.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <inttypes.h> | |
#include <assert.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
const size_t buf_size = 1024; | |
const size_t chunk_len = sizeof(long) * 2; | |
int open_auxv(void); | |
void close_auxv(int fd); | |
off_t get_offset(int fd); | |
bool is_at_eof(int fd); | |
int main(void) { | |
int fd; | |
unsigned char buf1[buf_size], buf2[buf_size]; | |
size_t buf1_len, buf2_len; | |
ssize_t read_len; | |
assert(chunk_len <= buf_size); | |
fd = open_auxv(); | |
if ((read_len = read(fd, buf1, sizeof(buf1))) == -1) { | |
perror("read"); | |
return EXIT_FAILURE; | |
} | |
assert(get_offset(fd) == read_len); | |
buf1_len = read_len; | |
if (!is_at_eof(fd)) { | |
fprintf(stderr, "buffer of %zu bytes is not big enough\n", buf_size); | |
return EXIT_FAILURE; | |
} | |
close_auxv(fd); | |
printf("read %zu bytes at once (%zu max)\n", buf1_len, buf_size); | |
fd = open_auxv(); | |
buf2_len = 0; | |
do { | |
if ((read_len = read(fd, buf2 + buf2_len, chunk_len)) == -1) { | |
perror("read"); | |
return EXIT_FAILURE; | |
} | |
buf2_len += read_len; | |
if (buf2_len > (buf_size - chunk_len)) { | |
if (is_at_eof(fd)) { | |
break; | |
} else { | |
fprintf(stderr, "buffer of %zu bytes is not big enough while reading chunks (?)\n", buf_size); | |
return EXIT_FAILURE; | |
} | |
} | |
} while (read_len != 0); | |
assert(get_offset(fd) == (off_t) buf2_len); | |
close_auxv(fd); | |
printf("read %zu bytes in chunks (%zu bytes each)\n", buf2_len, chunk_len); | |
if (buf1_len == buf2_len) { | |
puts("lengths are equal"); | |
} else { | |
printf("different lengths (%zu vs. %zu)\n", buf1_len, buf2_len); | |
} | |
if (memcmp(buf1, buf2, buf1_len) == 0) { | |
puts("contents match"); | |
} else { | |
puts("contents do not match"); | |
} | |
return EXIT_SUCCESS; | |
} | |
int open_auxv(void) { | |
int fd; | |
if ((fd = open("/proc/self/auxv", O_RDONLY)) == -1) { | |
perror("open"); | |
exit(EXIT_FAILURE); | |
} | |
return fd; | |
} | |
void close_auxv(int fd) { | |
if (close(fd) == -1) { | |
perror("close"); | |
exit(EXIT_FAILURE); | |
} | |
} | |
off_t get_offset(int fd) { | |
off_t off; | |
if ((off = lseek(fd, 0, SEEK_CUR)) == -1) { | |
perror("lseek"); | |
exit(EXIT_FAILURE); | |
} | |
return off; | |
} | |
bool is_at_eof(int fd) { | |
ssize_t read_len; | |
char test; | |
if ((read_len = read(fd, &test, sizeof(test))) == -1) { | |
perror("read"); | |
return EXIT_FAILURE; | |
} | |
return read_len == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment