halfcountplus has asked for the wisdom of the Perl Monks concerning the following question:

I have a server that needs to begin as root to open a socket, but after that I want it to run as a non-privileged entity. But I cannot seem to set the gid.
#!/usr/bin/perl -w use strict; $>=$<=48; # uid $)=$(=48; # gid setpgrp $$,48; open(FH,">/tmp/new.txt"); close(FH);
The uid changes, but the gid does not. The point with the open() is that it must write files as "apache.apache". But because the gid does not change, they end up "apache.root". Then if I try system("chown ...") it is, predictably, denied permission.

How can I change the process group? Or failing that, how can I set the ownership of a created file in this situation?

Replies are listed 'Best First'.
Re: set gid
by bv (Friar) on Sep 08, 2009 at 21:32 UTC

    This is a common issue. The solution is to set the GID first. Otherwise you no longer have the privileges required to set the GID.

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
      LOL! (Thanks bv)