LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 11-06-2019, 08:19 PM   #1
reberhardt
LQ Newbie
 
Registered: Nov 2019
Posts: 1

Rep: Reputation: Disabled
Determine (from userspace) if two processes share the same struct file


Hi,

I'm working on building a tool that helps new systems programmers understand what file-related syscalls do. When a program hits a breakpoint, I want to generate some diagrams of the file descriptor tables, open file table, and vnode table for the running processes. I can get a lot of the info I need from /proc/*/fd and from lsof, but I can't tell when two file descriptors are referring to the *same* open file (e.g. they share the same cursor).

As a concrete example, say process A opens a file, then forks, producing process B. I can tell that both processes are referring the same file, but I can't tell whether file A and file B opened that file independently, or if they have the same underlying open file.

Reading through https://www.tldp.org/LDP/lki/lki-3.html, I understand what I need is to determine when two tasks share a particular `struct file`. However, I don't know if this data is exposed through any API to user space. Is there something I can look at to determine this? I'm guessing the answer is no, since `struct file` doesn't include anything like a unique ID, but I just want to make sure before I try doing something unnecessarily complicated.

Thanks!
Ryan
 
Old 11-07-2019, 05:38 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Heuristically, you could check if they have a parent-child (or perhaps ancestor) relationship and the same file offset. I believe the offset is in /proc/$PID/fdinfo/$FD, as "pos".

Not proof but a strong indication that they share the file table entry.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a struct within a struct? smoothdogg00 Programming 3 12-21-2006 01:38 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration