473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

passing a socket to a spawned process.

Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?

Any help is much appreciated!!

Mike
Jul 18 '05 #1
11 6775
On Mon, Oct 11, 2004 at 10:23:27AM -0700, Mike M wrote:
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?


just accept() before fork()'ing. the socket will be cloned. then close the
socket on the parent, unless needed.

--
(Not so) Random fortune:
(Really) Random fortune:
mdione@tempest:~$ cat /dev/random | uuencode random | head -n 3
begin 644 random
M=?:,8:@5MVVIM6>NAYQ6O@I&H86>.3(L"F7S>U'.9`2;%LEAR/E8M[*F=(MP
M5C>B3Y2F(X,`SW1UPAL*+OIV'EM/Z$_-15':B#<6I99_P,H%=$S6$H<&W-!;
Jul 18 '05 #2
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?

Any help is much appreciated!!


As someone has already mentioned, if you have the socket before the fork,
you will have the socket after the fork.
I had been working on a general file descriptor passing mechanism for a
few weeks in May, until I gave up.

From what I was able to work out, it is not possible in Windows.
SunOS 5.8 has no issues, and will work almost out of the box (pure
Python) with the right incantation (I have it somewhere if you really
want it).
A few constants are missing from the Linux fcntl.py, so this is not
really possible unless you are willing to patch your kernel and Python.

Googling for the terms: passing file descriptors Python
will get you the list of articles/research that I looked at.

One of those links has source code for the C struct usable with a BSD
for making this happen. Generally though, you can only pass file
descriptors (sockets) across a UNIX domain socket, or sometimes a pipe
(make it before you fork).
Good luck.
- Josiah

Jul 18 '05 #3
Marcos Dione <md****@grulic.org.ar> wrote:
On Mon, Oct 11, 2004 at 10:23:27AM -0700, Mike M wrote:
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?


just accept() before fork()'ing. the socket will be cloned. then close the
socket on the parent, unless needed.


Unfortunately I believe that's platform-dependent. Specifically: your
suggestion should work on any sensible operating system... BUT a tad
over 50% of Python programs are estimated to run on Windows, and, on
THAT platform, I know your idea can't work (no fork!) and I don't know
how to answer the OP's question (except by suggesting he move, if
feasible, to any sensible platform -- any BUT Wind0ws...!-).
Alex
Jul 18 '05 #4
On Wed, 13 Oct 2004, Alex Martelli wrote:
Marcos Dione <md****@grulic.org.ar> wrote:
On Mon, Oct 11, 2004 at 10:23:27AM -0700, Mike M wrote:
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?


just accept() before fork()'ing. the socket will be cloned. then close the
socket on the parent, unless needed.


Unfortunately I believe that's platform-dependent. Specifically: your
suggestion should work on any sensible operating system... BUT a tad
over 50% of Python programs are estimated to run on Windows, and, on
THAT platform, I know your idea can't work (no fork!) and I don't know
how to answer the OP's question (except by suggesting he move, if
feasible, to any sensible platform -- any BUT Wind0ws...!-).


I've not tried this on Windows, but on OS/2 using the native sockets
(which is close to the Windows socket model as far as I can tell) you have
to pass the socket handle (an integer) to the child (usually via the
command line), which then attaches to the socket, and signals the parent
that it can close its reference to the socket.

The EMX (OS/2, Win32) and Cygwin (Win32) runtime environments synthesize
fork() and socket inheritance over the native environment, though at a
significant cost in run time and resources.

-------------------------------------------------------------------------
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia
Jul 18 '05 #5
Josiah Carlson <jc******@uci.edu> wrote in message news:<ma**************************************@pyt hon.org>...
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?

Any help is much appreciated!!


As someone has already mentioned, if you have the socket before the fork,
you will have the socket after the fork.
I had been working on a general file descriptor passing mechanism for a
few weeks in May, until I gave up.

From what I was able to work out, it is not possible in Windows.
SunOS 5.8 has no issues, and will work almost out of the box (pure
Python) with the right incantation (I have it somewhere if you really
want it).
A few constants are missing from the Linux fcntl.py, so this is not
really possible unless you are willing to patch your kernel and Python.

Googling for the terms: passing file descriptors Python
will get you the list of articles/research that I looked at.

One of those links has source code for the C struct usable with a BSD
for making this happen. Generally though, you can only pass file
descriptors (sockets) across a UNIX domain socket, or sometimes a pipe
(make it before you fork).
Good luck.
- Josiah

Passing sockets to spawned processes is also possible in Windows, but
tricky.

While I have never passed a socket between python processes, I have
passed a socket handle from a python process to a Windows binary.
Using the win32api extensions packaged with the ActiveState
distribution, one should be able to pass the socket between python
processes just as well.

The basic pseudo-code for doing so is as follows:

Parent process:
1) Obtain socket connection in parent server process from a client
process.
2) Obtain Windows process id -> use win32api.GetCurrentProcessId()
- This process id will later be sent to the child process.
3) Spawn child process, obtaining a pipe to child_stdin -> use one of
the os.popen functions.
4) Using child_stdin pass parent process id (obtained in 2) to the
child process.
5) Next, using child_stdin pass over the socket file id, using
socket.fileno().

Now the child process should have everything it needs to listen on the
socket.

Child process:
1) Read parent process id from stdin.
2) Read socket file id from stdin.
3) Obtain a handle to the parent process using the process id -> use
win32api.OpenProcess(). This handle will be needed in the next
call...
4) Turn your socket file id into a socket -> use
win32api.DuplicateHandle().
- hSourceProcess is the handle obtained in 3
- hSource is the file id

You are now done, and should be able to read data on the socket in the
child process!

Regards,

Michael Loritsch
Jul 18 '05 #6

On 16 Oct 2004 00:32:23 -0700
lo******@gmail.com (Michael Loritsch) wrote:
Josiah Carlson <jc******@uci.edu> wrote in message news:<ma**************************************@pyt hon.org>...
Is it possible? In the parent process, I have a socket that binds,
listens and then accepts new connections (which creates new sockets in
the process). I want to be able to pass some of these new sockets to
a spawned process. Is it possible, and if so how?

Any help is much appreciated!!


As someone has already mentioned, if you have the socket before the fork,
you will have the socket after the fork.
I had been working on a general file descriptor passing mechanism for a
few weeks in May, until I gave up.

From what I was able to work out, it is not possible in Windows.
SunOS 5.8 has no issues, and will work almost out of the box (pure
Python) with the right incantation (I have it somewhere if you really
want it).
A few constants are missing from the Linux fcntl.py, so this is not
really possible unless you are willing to patch your kernel and Python.

Googling for the terms: passing file descriptors Python
will get you the list of articles/research that I looked at.

One of those links has source code for the C struct usable with a BSD
for making this happen. Generally though, you can only pass file
descriptors (sockets) across a UNIX domain socket, or sometimes a pipe
(make it before you fork).
Good luck.
- Josiah

Passing sockets to spawned processes is also possible in Windows, but
tricky.

While I have never passed a socket between python processes, I have
passed a socket handle from a python process to a Windows binary.
Using the win32api extensions packaged with the ActiveState
distribution, one should be able to pass the socket between python
processes just as well.

The basic pseudo-code for doing so is as follows:

Parent process:
1) Obtain socket connection in parent server process from a client
process.
2) Obtain Windows process id -> use win32api.GetCurrentProcessId()
- This process id will later be sent to the child process.
3) Spawn child process, obtaining a pipe to child_stdin -> use one of
the os.popen functions.
4) Using child_stdin pass parent process id (obtained in 2) to the
child process.
5) Next, using child_stdin pass over the socket file id, using
socket.fileno().

Now the child process should have everything it needs to listen on the
socket.

Child process:
1) Read parent process id from stdin.
2) Read socket file id from stdin.
3) Obtain a handle to the parent process using the process id -> use
win32api.OpenProcess(). This handle will be needed in the next
call...
4) Turn your socket file id into a socket -> use
win32api.DuplicateHandle().
- hSourceProcess is the handle obtained in 3
- hSource is the file id

You are now done, and should be able to read data on the socket in the
child process!

Goodness, I guess it is possible in Windows. That kind of thing is
really useful for a "pre-forked" server with multiple processes, or even
a super-daemon that listens on ports specified by other daemon processes
in order to handle automatic failover.

Looks like I have a reason to download pywin32 after all.

Now if only it could be done in linux out of the box.

- Josiah

Jul 18 '05 #7
I recently wrote a c module for Python, for passing file descriptors and process
credentials down unix domain sockets under Linux. (using sendmsg(2))

It was pretty easy, and I could send it to anyone interested. Contact me
off-list, since I don't normally read this group.

Perhaps there is a need for a OS-independant wrapper around versions
specifically for Linux, Windows, Solaris, etc?

Martin
Jul 18 '05 #8

ma*******@parvat.com (Martin Atkins) wrote:

I recently wrote a c module for Python, for passing file descriptors and process
credentials down unix domain sockets under Linux. (using sendmsg(2))

It was pretty easy, and I could send it to anyone interested. Contact me
off-list, since I don't normally read this group.

Perhaps there is a need for a OS-independant wrapper around versions
specifically for Linux, Windows, Solaris, etc?

Martin


I'll take the source for the C version. And in general, yes, Python
could use such a thing for all platforms, though I'm not sure all
platforms support it.

- Josiah

Jul 18 '05 #9
In article <ma**************************************@python.o rg>,
Josiah Carlson <jc******@uci.edu> wrote:
ma*******@parvat.com (Martin Atkins) wrote:

I recently wrote a c module for Python, for passing file descriptors and
process
credentials down unix domain sockets under Linux. (using sendmsg(2))

It was pretty easy, and I could send it to anyone interested. Contact me
off-list, since I don't normally read this group.

Perhaps there is a need for a OS-independant wrapper around versions
specifically for Linux, Windows, Solaris, etc?

Martin


I'll take the source for the C version. And in general, yes, Python
could use such a thing for all platforms, though I'm not sure all
platforms support it.


Rest assured, not all platforms support it.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #10
I have (finally!) put a package up on our website that provides a
Python module for sending file descriptors down Unix-domain sockets, on Linux.

See the first item under:
http://www.mca-ltd.com/index.php?PAG...urces/home.php

Please let me know of any problems, obvious omissions, etc...

BTW: I just found out about bindd (see the README), and could probably
clean the code up from the examples in its sourcecode, but the current
version of fdcred works for me!

Sorry it has taken so long!

Regards,
Martin
--
Martin C. Atkins ma*******@parvat.com
Parvat Infotech Private Limited http://www.parvat.com{/,/martin}
Jul 18 '05 #11
Martin C.Atkins wrote:
I have (finally!) put a package up on our website that provides a
Python module for sending file descriptors down Unix-domain sockets, on Linux.

See the first item under:
http://www.mca-ltd.com/index.php?PAG...urces/home.php

Please let me know of any problems, obvious omissions, etc...

BTW: I just found out about bindd (see the README), and could probably
clean the code up from the examples in its sourcecode, but the current
version of fdcred works for me!

Sorry it has taken so long!


I notice that you have used the LGPL for your code. Could you be
convinced to use a Python style license?

This would mean that some time in the (hopefully not too distant) future
the code could be added to the standard socket module.

- Dave

--
http://www.object-craft.com.au
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher J. Bottaro | last post by:
Hello, I'm trying to write a fairly simple network program. The main thread spawns a thread which creates a listener socket and then calls socket.accept on it. socket.accept blocks indefinantly....
0
by: Iker Arizmendi | last post by:
Hello all. I'm trying to get a server process to do the following on both Linux and Windows: 1) Create a socket and bind it to some port number. 2) Use spawnl to create other processes that...
2
by: mlc | last post by:
I have a server (in win32 C++, TCP) that listens and accepts socket calls, then via CreateProcess and handle inheritence over the command line starts up a process to handle the client request. Is...
0
by: Bob | last post by:
Hello All: We've developed a Windows Service that is responsible for checking for updates and downloading them. It then installs the updates by running a seperate console executable. This...
0
by: dan.jakubiec | last post by:
I'm trying to write a Python app which accepts a socket connection and then spawns another Python process to handle it. I need it to run under both Linux and Windows. I have it working under...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a C# application in which I start another process which produces output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++ compiler itself! I would like to...
0
by: loorthu | last post by:
I am noticing that pexpect kills any child that it is spawned when the parent is terminated using SIGINT (e.g Ctrl-C on the shell), but not when it is killed by SIGKILL (e.g 'kill -9' on the parent...
2
by: subrahmanya | last post by:
Hi All Does any one has idea about how to set stdout and stderr to a spawned process? I don't want to have all the handles of the parent process in the child process but I want only one of it...
2
by: DwBear75 | last post by:
I am contemplating the need for a way to handle high speed data passing between two processes. One process would act as a queue that would 'buffer' data coming from another processes. Seems that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.