Note that there are some explanatory texts on larger screens.

plurals
  1. POTrace/wrap open system call
    primarykey
    data
    text
    <p>I have the following test code:</p> <pre><code>#include &lt;stdio.h&gt; int main(void) { fprintf(stderr, "This is a test.\n"); int ret = open("somefile.log", 1); fprintf(stderr, "Return value is %d.\n", ret); return 0; } </code></pre> <p>Compiled with <code>gcc -m64 test.c -o test</code></p> <p>If I run <code>truss ./test</code>, I eventaully see the following output:</p> <pre><code>getrlimit(RLIMIT_STACK, 0xFFFFFFFF7FFFE280) = 0 getpid() = 1984 [1983] setustack(0xFFFFFFFF7EE002C0) fstat(2, 0xFFFFFFFF7FFFDAA0) = 0 This is a test. write(2, " T h i s i s a t e".., 16) = 16 open("somefile.log", O_WRONLY) = 3 Return value is write(2, " R e t u r n v a l u e".., 16) = 16 3. write(2, " 3 .\n", 3) = 3 _exit(0) </code></pre> <p>I would like to hook the open system call and execute some code before the call to open finishes. I have read about using ptrace to do this, however I do not have sys/ptrace.h on this system (solaris). I see documentation stating that /proc debugging interfaces should be used instead of ptrace(), but I haven't been able to figure out how to do what I want using procfs. </p> <p>Does anyone know if this is possible? If so, how?</p> <p>As a side note, I have also tried using the LD_PRELOAD trick to implement the open system call in my own shared library and have it call dlsym to find the address of the regular open system call. I wasn't able to figure out 100% why this doesn't work, but it seemed related to the calls being inlined and not using an address table to lookup those functions. However somehow <code>truss</code> is able to detect calls to open().</p> <p>This was my code for that attempt:</p> <p><code>cat wrap_open.c</code></p> <pre><code>#define _GNU_SOURCE #include &lt;dlfcn.h&gt; #include &lt;stdio.h&gt; static int (*next_open) (const char *path, int oflag, /* mode_t mode */) = NULL; int open(const char *path, int oflag) { char *msg; if(next_open == NULL) { fprintf(stderr, "wrapping open\n"); next_open = dlsym(RTLD_NEXT, "open"); if((msg = dlerror()) != NULL) { fprintf(stderr, "open: dlopen failed: %s\n", msg); } else { fprintf(stderr, "open: wrapping done\n"); } } fprintf(stderr, "open: opening %s\n", msg); fflush(stderr); return next_open(path, oflag); } </code></pre> <p>Compiled with <code>gcc -fPIC -shared -Wl,-soname,libwrap_open.so.1 -ldl -o libwrap_open.so.1.0</code> Executed with <code>LD_PRELOAD_32=./libwrap_open.so.1.0 ./test</code></p> <p>I don't get any output from the shared library here. Only normal program output.</p> <p>Any help or pointers is appreciated. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload