In cases such as these (when a daemon absolutely has to perform certain tasks as root) it is usually best to use privilege separation. Fork your process into two daemons and drop privileges on one of them. The privileged process (i.e. the one running as root) is used to only perform those tasks that require root access. The other process is used for all other tasks (especially for potentially dangerous IO tasks such as sniffing the network or accepting user input). Communication between the two can be done via any of the various IPC means available.

The way you describe your goal it might even be sufficient for the unprivileged process to gather information only when the user tells it to, so it doesn't even have to be a daemon. This would mean you'd run the daemon as root all the time but have a user-run script sitting in the front, which accepts user input, gathers the necessary information and then sends this to the daemon for taking action (i.e. activate and configure the network interface). This, AFAIK, is how network-manager works, which does a lot of what you're describing (though not everything, so don't let its existence discourage you from this).

One simple way of communicating with the privileged daemon is for it to open a named pipe, which a user process/daemon can write to. By making this pipe group- and not world-writable you can limit the users who can communicate with the daemon to a given group. Or, if you only want one user to be able to communicate with the daemon, you can just make it writable for that user.


All dogma is stupid.

In reply to Re: Perl as a daemon... as root? by tirwhan
in thread Perl as a daemon... as root? by korpenkraxar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.