Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Questions about launchd
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Questions about launchd



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


References: 
 >Questions about launchd (From: Michal Taurich <email@hidden>)
 >Re: Questions about launchd (From: Dave Zarzycki <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.