<div dir="ltr">Ah! Should it? Like, should it do all the daemon things that the container itself does?</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 27, 2014 at 12:30 PM, Serge Hallyn <span dir="ltr"><<a href="mailto:serge.hallyn@ubuntu.com" target="_blank">serge.hallyn@ubuntu.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">Quoting John Keiser (<a href="mailto:jkeiser@getchef.com">jkeiser@getchef.com</a>):<br>
> Hi all,<br>
><br>
> I am trying to use lxc_container->start() from within a process that<br>
> happens to have started a TCP server, and even when my program exits, the<br>
> server socket stays alive. This can be tested by running this program<br>
> (which starts a server and then starts a container) twice--the second time<br>
> resulting in a bind() error:<br>
><br>
> #include <sys/socket.h><br>
> #include <netinet/in.h><br>
> #include <stdio.h><br>
> #include <errno.h><br>
> #include <strings.h><br>
> #include <stdlib.h><br>
> #include <lxc/lxccontainer.h><br>
><br>
> int main(int argc, char**argv)<br>
> {<br>
> struct lxc_container *c;<br>
> int listenfd,connfd,n;<br>
> struct sockaddr_in servaddr;<br>
> int port;<br>
><br>
> // Start server<br>
> port = atoi(argv[2]);<br>
> printf("Listening on port %d ...\n", port);<br>
> listenfd=socket(AF_INET,SOCK_STREAM,0);<br>
> bzero(&servaddr,sizeof(servaddr));<br>
> servaddr.sin_family = AF_INET;<br>
> servaddr.sin_addr.s_addr=htonl(INADDR_ANY);<br>
> servaddr.sin_port=htons(port);<br>
> if (bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))<br>
> {<br>
> perror("Error calling bind()");<br>
> exit(1);<br>
> }<br>
> listen(listenfd,1024);<br>
><br>
> // Start<br>
> printf("Starting container ...\n");<br>
> c = lxc_container_new(argv[1], NULL);<br>
> c->want_daemonize(c, true);<br>
> c->want_close_all_fds(c, true);<br>
> c->start(c, 0, NULL);<br>
><br>
> return 0;<br>
> }<br>
><br>
><br>
</div></div>> *> gcc test.c -llxc -o test*<br>
> *> lxc-create -n blarghle -t ubuntu*<br>
><br>
> *> ./test blarghle 9050*<br>
<div class="">> Listening on port 9050 ...<br>
> Starting container ...<br>
</div>> *> ./test blarghle 9050*<br>
<div class="">> Listening on port 9050 ...<br>
> Error calling bind(): Address already in use<br>
><br>
> As you can see, even though ./test finished the first time, the second<br>
> ./test cannot bind to the port because it is still in use.<br>
><br>
> This appears to be because the lxc init process has a ghost "./test" as its<br>
> parent:<br>
><br>
</div>> *> ps -ef | grep 30276*<br>
<div class="">> root 30276 1 0 18:37 ? 00:00:00 ./test blarghle 9050<br>
> root 30285 30276 0 18:37 ? 00:00:00 /sbin/init<br>
</div>> *> lsof | grep 9050*<br>
<div class="">> test 30276 root 3u IPv4 366649<br>
> 0t0 TCP *:9050 (LISTEN)<br>
<br>
</div>Hm. That's the container monitor. It doesn't close all fds like<br>
container init does.<br>
<div class=""><br>
> I am stymied on how to investigate further. My best guess is it has to do<br>
> with the double forking in<br>
</div>> lxccontainer.h<<a href="https://github.com/lxc/lxc/blob/master/src/lxc/lxccontainer.c#L608-L621" target="_blank">https://github.com/lxc/lxc/blob/master/src/lxc/lxccontainer.c#L608-L621</a>>,<br>
<div class="">> though I don't really understand what that is for yet :) Any thoughts?<br>
><br>
> Thanks!<br>
><br>
> --John<br>
<br>
</div>> _______________________________________________<br>
> lxc-devel mailing list<br>
> <a href="mailto:lxc-devel@lists.linuxcontainers.org">lxc-devel@lists.linuxcontainers.org</a><br>
> <a href="http://lists.linuxcontainers.org/listinfo/lxc-devel" target="_blank">http://lists.linuxcontainers.org/listinfo/lxc-devel</a><br>
<br>
_______________________________________________<br>
lxc-devel mailing list<br>
<a href="mailto:lxc-devel@lists.linuxcontainers.org">lxc-devel@lists.linuxcontainers.org</a><br>
<a href="http://lists.linuxcontainers.org/listinfo/lxc-devel" target="_blank">http://lists.linuxcontainers.org/listinfo/lxc-devel</a><br>
</blockquote></div><br></div>