Connecting Tech Pros Worldwide Forums | Help | Site Map

Linux, fcntl, F_SETLEASE and signals

Chris Green
Guest
 
Posts: n/a
#1: Jul 18 '05
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.
[color=blue][color=green][color=darkred]
>>> import fcntl
>>> f = open(".zshrc", "r+")
>>> fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)[/color][/color][/color]
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 <cmg@dok.org>
You now have 14 minutes to reach minimum safe distance.

Michael Hudson
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


Chris Green <cmg@dok.org> writes:
[color=blue]
> 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?[/color]

No.
[color=blue]
> 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.
>[color=green][color=darkred]
> >>> import fcntl
> >>> f = open(".zshrc", "r+")
> >>> fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)[/color][/color]
> 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?[/color]

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
Jeff Epler
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


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-----

John Lenton
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


On Wed, 21 Jul 2004 12:27:57 -0400, Chris Green <cmg@dok.org> wrote:[color=blue]
> 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.
>[color=green][color=darkred]
> >>> import fcntl
> >>> f = open(".zshrc", "r+")
> >>> fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)[/color][/color]
> 0[/color]

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.[color=blue][color=green][color=darkred]
>>> 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)[/color][/color][/color]
0[color=blue][color=green][color=darkred]
>>>
>>> # now someone cats .bashrc[/color][/color][/color]
.... I/O possible

--
John Lenton (jlenton@gmail.com) -- Random fortune:
bash: fortune: command not found
John Lenton
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


On Wed, 21 Jul 2004 14:03:56 -0300, John Lenton <jlenton@gmail.com> wrote:[color=blue]
> On Wed, 21 Jul 2004 12:27:57 -0400, Chris Green <cmg@dok.org> wrote:[color=green]
> > 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.
> >[color=darkred]
> > >>> import fcntl
> > >>> f = open(".zshrc", "r+")
> > >>> fcntl.fcntl(f, fcntl.F_SETLEASE, fcntl.F_WRLCK)[/color]
> > 0[/color]
>
> 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.[color=green][color=darkred]
> >>> 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)[/color][/color]
> 0[color=green][color=darkred]
> >>>
> >>> # now someone cats .bashrc[/color][/color]
> ... I/O possible[/color]

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

--
John Lenton (jlenton@gmail.com) -- Random fortune:
bash: fortune: command not found
Gardner Pomper
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


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:
[color=blue]
> python /usr/lib/python2.2/boa-constructor-0.2.3/Boa.py[/color]
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



Chris Green
Guest
 
Posts: n/a
#7: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


John Lenton <jlenton@gmail.com> writes:
[color=blue]
> sorry, I got the wrong end of the stick. But ctypes lets you do what
> you want, anyways. Portable as Linux :)[/color]

Thanks for pointing me to that. Seems like a very interesting
module. It may help me out :)
--
Chris Green <cmg@dok.org>
Let not the sands of time get in your lunch.
vincent wehren
Guest
 
Posts: n/a
#8: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


Gardner Pomper wrote:[color=blue]
> 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:
>
>[color=green]
>>python /usr/lib/python2.2/boa-constructor-0.2.3/Boa.py[/color]
>
> 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[/color]


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

--
Vincent Wehren




Chris Green
Guest
 
Posts: n/a
#9: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


Jeff Epler <jepler@unpythonic.net> writes:
[color=blue]
>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.[/color]

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 <cmg@dok.org>
"I'm beginning to think that my router may be confused."
Jeff Epler
Guest
 
Posts: n/a
#10: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


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-----

Chris Green
Guest
 
Posts: n/a
#11: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


Jeff Epler <jepler@unpythonic.net> writes:
[color=blue]
> What I don't see here is any handling of threads.[/color]

You're right. I'm ignorant of the thread<->signal interaction in
python.
[color=blue]
>
> 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.[/color]

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 <cmg@dok.org>
Warning: time of day goes back, taking countermeasures.

Chris Green
Guest
 
Posts: n/a
#12: Jul 18 '05

re: Linux, fcntl, F_SETLEASE and signals


Jeff Epler <jepler@unpythonic.net> writes:
[color=blue]
> What I don't see here is any handling of threads.[/color]

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 <cmg@dok.org>
A good pun is its own reword.
Closed Thread