467,915 Members | 1,093 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Linux, fcntl, F_SETLEASE and signals

Hey folks,

Is there anyway for a signal handler in python to get the information
from a 3 argument signal handler rather than just the signal number
and stack frame?

I've got an application where I have to check for F_SETLEASE on a file
in python on Linux 2.4. What this does is tells the kernel to notify
the current process with SIGIO that a particular file descriptor is being
modified by another process.
import fcntl
f = open(".zshrc", "r+")
fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)

0

Now, when another process opens ".zshrc", I get a SIGIO saying
something happened. The kernel provides the information on what
descriptor changed in a siginfo_t's si_fd field.

Is there anyway to get this from python?

Thanks,
Chris
--
Chris Green <cm*@dok.org>
You now have 14 minutes to reach minimum safe distance.
Jul 18 '05 #1
  • viewed: 2332
Share:
11 Replies
Chris Green <cm*@dok.org> writes:
Hey folks,

Is there anyway for a signal handler in python to get the information
from a 3 argument signal handler rather than just the signal number
and stack frame?
No.
I've got an application where I have to check for F_SETLEASE on a file
in python on Linux 2.4. What this does is tells the kernel to notify
the current process with SIGIO that a particular file descriptor is being
modified by another process.
import fcntl
f = open(".zshrc", "r+")
fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)

0

Now, when another process opens ".zshrc", I get a SIGIO saying
something happened. The kernel provides the information on what
descriptor changed in a siginfo_t's si_fd field.

Is there anyway to get this from python?


What a wonderful interface. I think you'll have to write some C for
this...

Cheers,
mwh

--
<Erwin> I recompiled XFree 4.2 with gcc 3.2-beta-from-cvs with -O42
and -march-pentium4-800Mhz and I am sure that the MOUSE
CURSOR is moving 5 % FASTER!
-- from Twisted.Quotes
Jul 18 '05 #2
I don't know of a way to do this without an extension module. It should
be possible to do it with an extension module, though. The module
would use sigaction() to install its own handler for SIGIO, and stash
the desired fields from the siginfo_t parameter somewhere. Use
Py_AddPendingCall() (undocumented? but with a public API name) to run a
callback with the stashed information in the void* arg, which calls back
into Python or does whatever else you need.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFA/p61Jd01MZaTXX0RAjKJAKCXL47MW0LwZJyKcjqObHwtq1cxYAC fZgRB
0yObNIGu/VE3Bk8cwVRE/hI=
=Ye8Y
-----END PGP SIGNATURE-----

Jul 18 '05 #3
On Wed, 21 Jul 2004 12:27:57 -0400, Chris Green <cm*@dok.org> wrote:
Hey folks,

Is there anyway for a signal handler in python to get the information
from a 3 argument signal handler rather than just the signal number
and stack frame?

I've got an application where I have to check for F_SETLEASE on a file
in python on Linux 2.4. What this does is tells the kernel to notify
the current process with SIGIO that a particular file descriptor is being
modified by another process.
import fcntl
f = open(".zshrc", "r+")
fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK) 0


would this be close enough?

Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import fcntl
import ctypes
libc = ctypes.cdll.LoadLibrary('/lib/libc.so.6')
f = open("/home/john/.bashrc")
libc.fcntl(f.fileno(), fcntl.F_SETLEASE, fcntl.F_WRLCK) 0
# now someone cats .bashrc

.... I/O possible

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #4
On Wed, 21 Jul 2004 14:03:56 -0300, John Lenton <jl*****@gmail.com> wrote:
On Wed, 21 Jul 2004 12:27:57 -0400, Chris Green <cm*@dok.org> wrote:
Hey folks,

Is there anyway for a signal handler in python to get the information
from a 3 argument signal handler rather than just the signal number
and stack frame?

I've got an application where I have to check for F_SETLEASE on a file
in python on Linux 2.4. What this does is tells the kernel to notify
the current process with SIGIO that a particular file descriptor is being
modified by another process.
>> import fcntl
>> f = open(".zshrc", "r+")
>> fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)

0


would this be close enough?

Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import fcntl
import ctypes
libc = ctypes.cdll.LoadLibrary('/lib/libc.so.6')
f = open("/home/john/.bashrc")
libc.fcntl(f.fileno(), fcntl.F_SETLEASE, fcntl.F_WRLCK) 0
# now someone cats .bashrc

... I/O possible


sorry, I got the wrong end of the stick. But ctypes lets you do what
you want, anyways. Portable as Linux :)

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #5
Hi,

I am not sure if this is the right list, but I am having a problem with
the installation of boa constructor. It all seems to have installed with
no errors, but when I try to run it, this is what I get:
python /usr/lib/python2.2/boa-constructor-0.2.3/Boa.py

Starting Boa Constructor v0.2.3
importing wxPython
reading user preferences
Segmentation fault

Here is how I installed it:
I went to boa-constructor.sourceforge.net and went to the download area.
There it says:

Before this can work, you'll need to have successfully installed
wxPython 2.4.0.7 or higher and Python 2.1 or higher. I have listed my
installed versions below.

Here is the list of my installed versions of python related things:

rpm -qa | grep -i python
python-2.2.3-5
mod_python-3.0.3-2.ent
gnome-python2-bonobo-1.99.14-5
gnome-python2-1.99.14-5
libxml2-python-2.5.10-5
bproc-python-0.9-13_Scyld
wxPythonGTK-py2.2-2.5.1.5-1
python-optik-1.4.1-2
gnome-python2-gtkhtml2-1.99.14-5
python-devel-2.2.3-5
gnome-python2-gconf-1.99.14-5
rpm-python-4.2.1-4.4
gnome-python2-canvas-1.99.14-5

Here is my list of gtk related things:

rpm -qa | grep -i gtk
gtk2-2.2.4-4.0
pygtk2-libglade-1.99.16-8
gtkhtml2-devel-2.2.0-6
pygtk2-devel-1.99.16-8
gtkhtml2-2.2.0-6
gtkam-0.1.7-6
gtkhtml3-3.0.9-4
gtk2-engines-2.2.0-2
gtk2-devel-2.2.4-4.0
gtk-doc-1.1-3.0
gtk+-1.2.10-27.1
pygtk2-1.99.16-8
gtk-engines-0.12-2.0
gtk+-devel-1.2.10-27.1
wxPythonGTK-py2.2-2.5.1.5-1
gnome-python2-gtkhtml2-1.99.14-5
gtkam-gimp-0.1.7-6
usermode-gtk-1.68-5

I downloaded boa-constructor-0.2.3.src.zip and unzipped it as root in
the /usr/lib/python2.2 directory.

Can anyone give me any suggestions on what to try next?

- Gardner

Jul 18 '05 #6
John Lenton <jl*****@gmail.com> writes:
sorry, I got the wrong end of the stick. But ctypes lets you do what
you want, anyways. Portable as Linux :)


Thanks for pointing me to that. Seems like a very interesting
module. It may help me out :)
--
Chris Green <cm*@dok.org>
Let not the sands of time get in your lunch.
Jul 18 '05 #7
Gardner Pomper wrote:
Hi,

I am not sure if this is the right list, but I am having a problem with
the installation of boa constructor. It all seems to have installed with
no errors, but when I try to run it, this is what I get:

python /usr/lib/python2.2/boa-constructor-0.2.3/Boa.py


Starting Boa Constructor v0.2.3
importing wxPython
reading user preferences
Segmentation fault

Here is how I installed it:
I went to boa-constructor.sourceforge.net and went to the download area.
There it says:

Before this can work, you'll need to have successfully installed
wxPython 2.4.0.7 or higher and Python 2.1 or higher. I have listed my
installed versions below.

Here is the list of my installed versions of python related things:

rpm -qa | grep -i python
python-2.2.3-5
mod_python-3.0.3-2.ent
gnome-python2-bonobo-1.99.14-5
gnome-python2-1.99.14-5
libxml2-python-2.5.10-5
bproc-python-0.9-13_Scyld
wxPythonGTK-py2.2-2.5.1.5-1
python-optik-1.4.1-2
gnome-python2-gtkhtml2-1.99.14-5
python-devel-2.2.3-5
gnome-python2-gconf-1.99.14-5
rpm-python-4.2.1-4.4
gnome-python2-canvas-1.99.14-5

Here is my list of gtk related things:

rpm -qa | grep -i gtk
gtk2-2.2.4-4.0
pygtk2-libglade-1.99.16-8
gtkhtml2-devel-2.2.0-6
pygtk2-devel-1.99.16-8
gtkhtml2-2.2.0-6
gtkam-0.1.7-6
gtkhtml3-3.0.9-4
gtk2-engines-2.2.0-2
gtk2-devel-2.2.4-4.0
gtk-doc-1.1-3.0
gtk+-1.2.10-27.1
pygtk2-1.99.16-8
gtk-engines-0.12-2.0
gtk+-devel-1.2.10-27.1
wxPythonGTK-py2.2-2.5.1.5-1
gnome-python2-gtkhtml2-1.99.14-5
gtkam-gimp-0.1.7-6
usermode-gtk-1.68-5

I downloaded boa-constructor-0.2.3.src.zip and unzipped it as root in
the /usr/lib/python2.2 directory.

Can anyone give me any suggestions on what to try next?

- Gardner

AFAIK, Boa doesn't work with wxPython 2.5.1. With Boa, you need to use
wxPython 2.4.2.

--
Vincent Wehren


Jul 18 '05 #8
Jeff Epler <je****@unpythonic.net> writes:
Use Py_AddPendingCall() (undocumented? but with a public API name) to
run a callback with the stashed information in the void* arg, which
calls back into Python or does whatever else you need.


What I ended up doing what creating a module that just installed a
handler that appends FD info onto a queue and then have a python call
that will check that queue suitable for use in an event loop.

It's my first python C extension so there's a few things I'm sure I'm
not doing right, most notably finding the spot to put finalization
code in.

http://cmg.dok.org/cgi-bin/pyblosxom...hon/linuxlease
--
Chris Green <cm*@dok.org>
"I'm beginning to think that my router may be confused."
Jul 18 '05 #9
What I don't see here is any handling of threads.

The SIGIO handler might be called at the same time as Python code is
running in another thread. That means the call to getPending can see a
partially modified queue, or that the queue can be modified by a signal
while getPending is running.

Perhaps the code is written in some way that avoids these problems, but
I wasn't able to see how that would happen.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBBRQNJd01MZaTXX0RAuUGAKCCe7jFNJFEXR6BpUPMw9 mJb9w5qACfdYEM
GmMXiEf+ZZ9osszXIr6M5j0=
=FgXB
-----END PGP SIGNATURE-----

Jul 18 '05 #10
Jeff Epler <je****@unpythonic.net> writes:
What I don't see here is any handling of threads.
You're right. I'm ignorant of the thread<->signal interaction in
python.

The SIGIO handler might be called at the same time as Python code is
running in another thread. That means the call to getPending can see a
partially modified queue, or that the queue can be modified by a signal
while getPending is running.


If I put a thread-safe mutex around the queue, that should be good
enough? Everything that I've seen has

Py_BEGIN_ALLOW_THREADS
code
Py_END_ALLOW_THREADS

getPending()
read from queue
interrupt while reading via signal
read from modified queue

Is this where you were recommending Py_AddPendingCall? Add the
pending increment when no other fields are taking place?

Are C extensions only accessed by a single thread unless they
Py_BEGIN_ALLOW_THREADS?

Thanks,
Chris
--
Chris Green <cm*@dok.org>
Warning: time of day goes back, taking countermeasures.

Jul 18 '05 #11
Jeff Epler <je****@unpythonic.net> writes:
What I don't see here is any handling of threads.


http://cmg.dok.org/code/linuxlease-0.3.tar.gz now has the same signal
code roughly as signalmodule.c. The biggest difference is that I
add to my internal queue the pending calls with an argument of a
pointer to the signal handler argument.

I'm not sure that is safe but it seems to work. Ideally, I'd
allocate space for the argument and then queue it.
--
Chris Green <cm*@dok.org>
A good pun is its own reword.
Jul 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Meyer, Tony | last post: by
3 posts views Thread by Math55 | last post: by
7 posts views Thread by hlubenow | last post: by
9 posts views Thread by mhearne808[insert-at-sign-here]gmail[insert-dot-he | last post: by
5 posts views Thread by GHUM | last post: by
4 posts views Thread by The Doctor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.