Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

[SOLVED] How to use perl IO::Socket::INET with inetd

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
mv
Watchman
Watchman
User avatar
Posts: 6795
Joined: Wed Apr 20, 2005 12:12 pm

[SOLVED] How to use perl IO::Socket::INET with inetd

  • Quote

Post by mv » Sun Oct 19, 2014 9:03 am

This is a perl programming question which came up in some systemd discussion:

I have a daemon in perl which currently uses $socket = IO::Socket::INET->new(...) to create a TCP socket.

When such a daemon should be used with inetd or systemd socket activation, it should be able to use STDIN/STDOUT (or other passed filedescriptors) as a socket.

Is it possible to "transform" STDIN/STDOUT into a corresponding perl object? The intention is to replace only the above "new"-command by some code so that afterwards all of the rest of the daemon would work unchanged with inetd (so that e.g. $socket->accept(), $socket->recv(...), $socket->send(...) work afterwards with inetd by reading/writing appropriate data to STDIN/STDOUT).
Last edited by mv on Tue Oct 21, 2014 7:17 pm, edited 1 time in total.
Top
RazielFMX
l33t
l33t
User avatar
Posts: 835
Joined: Sat Apr 23, 2005 6:35 pm
Location: NY, USA

  • Quote

Post by RazielFMX » Tue Oct 21, 2014 4:41 pm

STDOUT/STDIN are default filehandles. You can use them in an OO fashion by including "use FileHandle" at the top of your code.

Then you can do things like:

Code: Select all

#!/usr/bin/perl -w

use strict;
use FileHandle;

#Don't buffer writes
STDOUT->autoflush(1);

#Read 256 bytes
my $buffer = '';
my $read = STDIN->sysread($buffer, 256);
I don't think you can perform socket operations on them though, but I could be wrong.

Check out this: http://www.perlmonks.org/?node_id=544341

EDIT:

I think for what you are trying to do is to have a command line switch that can be invoked by inetd to turn on inetd functionality. I would then either as a standalone PM or a nested class, do something like this:

Code: Select all

my $socket = ($inetdEnabled) ? InetdWrapper->new() : IO::Socket::INET->new(%args)
You then just implement what you need:

Code: Select all

package InetdWrapper;

use strict;
use FileHandle;

sub new {
    my $pkg = shift;
    return bless({}, $pkg);
}

sub close {
    # You might want this as a no-op, not sure
}

sub syswrite {
    my $self = shift;
    return STDOUT->syswrite(@_);
}

sub sysread {
    my $self = shift;
    return STDIN->sysread(@_);
}
You get the idea.
I am not anti-systemd; I am pro-choice. If being the latter makes you feel that I am the former, then so be it.
Top
mv
Watchman
Watchman
User avatar
Posts: 6795
Joined: Wed Apr 20, 2005 12:12 pm

  • Quote

Post by mv » Tue Oct 21, 2014 7:15 pm

The point is that I want to do socket operations on them, in particular accept() (and use the sockets obtained by that).

In German USEnet, I got now a simple receipt which I have just tested and which works:

Code: Select all

$socket = new IO::Socket::INET();
$socket->fdopen(0, 'r');
It is a little bit hackish, since theoretically the missing argument list of IO::Socket::INET might mean that also some other important information is not initialized, but so far it works without any issues.
Top
RazielFMX
l33t
l33t
User avatar
Posts: 835
Joined: Sat Apr 23, 2005 6:35 pm
Location: NY, USA

  • Quote

Post by RazielFMX » Thu Oct 23, 2014 12:48 pm

That is effectively duping the file descriptor, so it should be perfectly reasonable to use. Nifty trick.
I am not anti-systemd; I am pro-choice. If being the latter makes you feel that I am the former, then so be it.
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic