840c0b18e9990fe15416283f869bc8532c1bbf3a
Stefan Schuermans add copyright headers and l...

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c   1) /**
libuptev/src/symlink.c   2)  * UProcTrace: User-space Process Tracing
libuptev/src/symlink.c   3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
libuptev/src/symlink.c   4)  * Copyleft: GNU LESSER GENERAL PUBLIC LICENSE version 3 (see LICENSE)
libuptev/src/symlink.c   5)  */
libuptev/src/symlink.c   6) 
Stefan Schuermans add exe and cwd to proc_begin

Stefan Schuermans authored 4 years ago

liblwpttr/src/symlink.c  7) #include "symlink.h"
liblwpttr/src/symlink.c  8) #include "cleaner.h"
liblwpttr/src/symlink.c  9) 
liblwpttr/src/symlink.c 10) #include <stdlib.h>
liblwpttr/src/symlink.c 11) #include <unistd.h>
liblwpttr/src/symlink.c 12) 
liblwpttr/src/symlink.c 13) /**
liblwpttr/src/symlink.c 14)  * @brief read target of symlink
liblwpttr/src/symlink.c 15)  * @param[in] pathname path to symbolic link
liblwpttr/src/symlink.c 16)  * @return malloc-ed string containting link target or NULL
liblwpttr/src/symlink.c 17)  */
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  18) static char *uptev_symlink_read_target(char const *pathname) {
Stefan Schuermans add exe and cwd to proc_begin

Stefan Schuermans authored 4 years ago

liblwpttr/src/symlink.c 19)   size_t sz = 256;
liblwpttr/src/symlink.c 20)   char *target = NULL;
liblwpttr/src/symlink.c 21)   while (1) {
liblwpttr/src/symlink.c 22)     /* get buffer */
liblwpttr/src/symlink.c 23)     target = malloc(sz);
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  24)     if (!target) {
Stefan Schuermans add exe and cwd to proc_begin

Stefan Schuermans authored 4 years ago

liblwpttr/src/symlink.c 25)       return NULL;
liblwpttr/src/symlink.c 26)     }
liblwpttr/src/symlink.c 27)     /* get link target */
liblwpttr/src/symlink.c 28)     ssize_t len = readlink(pathname, target, sz);
liblwpttr/src/symlink.c 29)     if (len < 0) {
liblwpttr/src/symlink.c 30)       free(target);
liblwpttr/src/symlink.c 31)       return NULL;
liblwpttr/src/symlink.c 32)     }
liblwpttr/src/symlink.c 33)     /* link target fit into buffer -> terminate string and return */
liblwpttr/src/symlink.c 34)     if ((size_t)len + 1 < sz) {
liblwpttr/src/symlink.c 35)       target[len] = 0;
liblwpttr/src/symlink.c 36)       return target;
liblwpttr/src/symlink.c 37)     }
liblwpttr/src/symlink.c 38)     /* free buffer and try again with larger buffer */
liblwpttr/src/symlink.c 39)     free(target);
liblwpttr/src/symlink.c 40)     sz *= 2;
liblwpttr/src/symlink.c 41)   }
liblwpttr/src/symlink.c 42) }
liblwpttr/src/symlink.c 43) 
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  44) char *uptev_symlink_read(char const *pathname, uptev_cleaner_t *cleaner) {
Stefan Schuermans rename: lwproctrace -> upro...

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  45)   char *target = uptev_symlink_read_target(pathname);
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  46)   if (!target) {
Stefan Schuermans rename: lwproctrace -> upro...

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  47)     return NULL;
Stefan Schuermans add exe and cwd to proc_begin

Stefan Schuermans authored 4 years ago

liblwpttr/src/symlink.c 48)   }
Stefan Schuermans handle cleaner out of memory

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  49)   /* add malloc-ed object to cleaner */
libuptev/src/symlink.c  50)   if (uptev_cleaner_add_ptr(cleaner, target) != 0) {
libuptev/src/symlink.c  51)     return NULL;
libuptev/src/symlink.c  52)   }
libuptev/src/symlink.c  53)   /* success, return target string */
Stefan Schuermans rename: lwproctrace -> upro...

Stefan Schuermans authored 4 years ago

libuptev/src/symlink.c  54)   return target;