Re: Questions about launchd
Re: Questions about launchd
- Subject: Re: Questions about launchd
- From: Terry Lambert <email@hidden>
- Date: Fri, 24 Aug 2007 23:17:00 -0700
On Aug 24, 2007, at 7:47 AM, Dave Zarzycki wrote:
On Aug 22, 2007, at 6:38 PM, Michal Taurich wrote:
I did it in my script with
checking "lsof" if the file is used or not. Isn't there any other
way directly
in launchd or Lingon ?
Unfortunately, there are no facilities in the OS for knowing whether
a file is open or not.
popen of lsof will work, assuming you use the output format specifiers
to prevent it from changing output format on you, or simply look at
the found/not fond return value.
The point of forcing you to write wrappers around standard tools
instead of doing what the tool does is to ensure you that when the
system is updated, you are guaranteed that the tools are updated at
the same time that the kernel and other system components are updated,
so your code will continue working, even if we change the system
interface used by the tool.
For example, here's a function that takes a pid and returns the
resident set size of a process (limited to 32 bits because I
implemented it quickly) or < 0 on error:
/* NB: returns signed int; not suitable for processes with > 2TB RSS */
int
getrss(pid_t pid)
{
FILE *fp;
char buf[128];
int rss;
snprintf(buf, sizeof(buf), "/bin/ps -p %d -o rsz=", pid);
if ((fp = popen(buf, "r")) == NULL) {
return (-1);
}
if (!fscanf(fp, "%d", &rss)) {
return(-2);
}
fclose(fp);
return(rss);
}
This function will remain operational so long as ps remains
operational, and the standards do not change, despite software updates.
Of course, you could also write a kauth kext and keep track of this
information yourself, too.
-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden