Solved bind/listen Address already in use

This is one of those things I had working months ago but had to put it down ... and now I can't get it to work.

It's related to trying to start www/spawn-fcgi for a fastcgi implementation listening on a unix socket, not an IP address, and using C. When I start it

spawn-fcgi -n -s /var/run/fcgi.sock -u www -- /www/a.out

I get the error in the title of this thread. From Googling around, I see many ways to try and find the PID which don't work for me or I'm doing it wrong. I also read that these open sockets should close themselves after 60 seconds or so but that never happens. I've even shutdown my workstation but the error returns.

I'm lost and looking for guidance on how to solve the bind/listen problem.
 
So something may have changed since I last worked with this. I need to investigate more but starting www/spawn-fcgi as I show may not be the correct way to do this on FreeBSD as I have found that there is a config file in /etc/rc.d and I can start and stop it using service. So the issue may not be with what I said.
 
Thank you! I was using sockstat but possibly not with the right flags as I saw what I wanted to see. I'm still confused by this but I see my program was running as root. That doesn't explain why I get the error in the title but maybe it explains something else.
 
I did sockstat -lu and, sometimes, it lists my running socket and other times it doesn't. Haven't a clue why but, right now, I still get the error shown in the title but sockstat doesn't show the socket in the command list. So I still don't get it.
 
That spawn_fcgi ... looks like the right way start it up. Don't know exactly what your a.out is meant to do, but I'm thinking that the bind/listen error is coming from that, and not from spawn_fcgi. I see this when I try to run a second second spawn_fcgi when a first one is already running and listening on the socket:
Code:
spawn-fcgi: socket is already in use, can't spawn
I also read that these open sockets should close themselves after 60 seconds or so but that never happens.
spawn_fcgi should keep the given socket open for as long as the FCGI app ( /www/a.out) runs, so that 60 second timeout would have to be part of the FCGI app, not spawn_fcgi.
 
a.out is a little test program I wrote that just spits out a "Hello World".

I'm getting closer to the solution. I edited /usr/local/etc/rc.d/spawn-fcgi. If I shouldn't do that, or there's a better way, please tell me. Here is the significant part of the original file:

Code:
: ${spawn_fcgi_enable="NO"}
: ${spawn_fcgi_app="%%LOCALBASE%%/bin/php-cgi"}
: ${spawn_fcgi_pidfile="/var/run/spawn-fcgi.pid"}
: ${spawn_fcgi_username="www"}
: ${spawn_fcgi_groupname="www"}
: ${spawn_fcgi_bindaddr="127.0.0.1"}
: ${spawn_fcgi_bindport="9000"}
: ${spawn_fcgi_bindsocket_mode="0777"}
: ${spawn_fcgi_children="5"}
: ${spawn_fcgi_max_requests="1000"}
: ${spawn_fcgi_path_env="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin"}

Above that there are some "optional settings" listed. I added
Code:
: ${spawn_fcgi_bindsocket="/var/run/fcgi-sock.fcgi"

I also removed the lines for bindaddr and bindport (I think. Now I'm forgetting) and changed spawn_fcgi_app to point to my a.out test code.

This worked but it also spit out a bunch of garbage. I had presumed that it was because it also started up about five php-cgi applications, which I don't have and don't want. I removed the line for php-cgi but spawn-fcgi fails to start.

I'm hoping the garbage printed out is an error in my code (though that worked at one time) and I need to look into that but, as I write this, I realize I'm starting to forget what I did and need to backtrack a bit.

I wish I knew what spawn_fcgi_enable="NO" was about.

EDIT: Now I'm having trouble getting back to my working version.

Code:
spawn-fcgi: child exited with: 48
/usr/local/etc/rc.d/spawn-fcgi: WARNING: failed to start spawn_fcgi
 
I have a little "Hello World" FCGI app running, using the spawn_fcgi service, with little more than this added to /etc/rc.conf (no changes to /usr/local/etc/rc.d/spawn_fcgi)
Code:
spawn_fcgi_enable="YES"
spawn_fcgi_app="/usr/home/jimmy/fcgiapp"
spawn_fcgi_bindsocket="/var/run/fcgi.sock"

spawn_fcgi_enable is just the hook that allows the system to know whether the service should be started automatically on system boot or via service spawn_fcgi start.
 
I didn't see it start by itself so I tried starting it on my own.

Code:
service spawn-fcgi onestart
Starting spawn_fcgi.
spawn-fcgi: child exited with: 13
/usr/local/etc/rc.d/spawn-fcgi: WARNING: failed to start spawn_fcgi
 
Sooo... your FCGI app is crashing and burning.
Here's the source to my Hello World FCGI app:
Code:
#include <iostream>
#include "fcgio.h"

using namespace std;

int main(void) {
  // Backup the stdio streambufs
  streambuf * cin_streambuf  = cin.rdbuf();
  streambuf * cout_streambuf = cout.rdbuf();
  streambuf * cerr_streambuf = cerr.rdbuf();

  FCGX_Request request;

  FCGX_Init();
  FCGX_InitRequest(&request, 0, 0);

  while (FCGX_Accept_r(&request) == 0) {
  fcgi_streambuf cin_fcgi_streambuf(request.in);
  fcgi_streambuf cout_fcgi_streambuf(request.out);
  fcgi_streambuf cerr_fcgi_streambuf(request.err);

  cin.rdbuf(&cin_fcgi_streambuf);
  cout.rdbuf(&cout_fcgi_streambuf);
  cerr.rdbuf(&cerr_fcgi_streambuf);

  cout << "Content-type: text/html\r\n"
  << "\r\n"
  << "<html>\n"
  << "  <head>\n"
  << "  <title>Hello, World!</title>\n"
  << "  </head>\n"
  << "  <body>\n"
  << "  <h1>Hello, World!</h1>\n"
  << "  </body>\n"
  << "</html>\n";

  // Note: the fcgi_streambuf destructor will auto flush
  }

  // restore stdio streambufs
  cin.rdbuf(cin_streambuf);
  cout.rdbuf(cout_streambuf);
  cerr.rdbuf(cerr_streambuf);

  return 0;
}
Installed fcgi-devkit to build it.
c++ -o fcgiapp -I/usr/local/include -L/usr/local/lib -lfcgi++ -lfcgi fcgiapp.cpp

Without anything in /etc/rc.conf to configure it, the spawn_fcgi service defaults to starting up PHP in fastcgi mode; overriding spawn_fcgi_app keeps that from happening (the _max_requests and _children settings are PHP specific). Setting spawn_fcgi_enable="YES" lets you start it via service spawn-fcgi start rather than having to use onestart
 
Well that's the problem. Apparently what I thought was a working app isn't. I am positive it worked at one time but I guess something got screwed up somewhere over time and I put too much faith in it. I need to investigate what I'm doing wrong. I can't be too far off.

Thank you so very much for your time.
 
Got it to work but I haven't had a chance to look into the why of it.

I'm using straight C and /usr/local/include/fcgiapp.h. Not C++. I thought I needed to open the socket myself doing this:

Code:
int sockfd = FCGX_OpenSocket("/var/run/fcgi-sock.fcgi", 1024);

and then supply that to the request:

Code:
FCGX_Init();
FCGX_InitRequest(&request, sockfd, 0);

From the example provided ljboiler I noticed he didn't open the socket like that and only supplied a zero for sockfd. Once I changed that, everything worked. I haven't a clue how I got this code to work months ago but it works now and I can now resume that major project.

Code:
#include "/usr/local/include/fcgiapp.h"

int main()
{
//    int sockfd = FCGX_OpenSocket("/var/run/fcgi-sock.fcgi", 1024);
    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);
    while (FCGX_Accept_r(&request) == 0)
    {
        FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n <h1>Hello World!</h1>");
        FCGX_Finish_r(&request);
    }
}
 
Ah - there's the rub! spawn-fcgi is already opening the socket and relaying data between your app and the socket; therefore, your app doesn't need to worry about the socket at all.

You would open the socket in your app and use it in the FCGX_InitRequest if you wanted your app to run without the help of spawn-fcgi.
 
I see! But wouldn't it run only once? spawn-fcgi is there to keep the thing rolling between calls. If I'm wrong, then I want to play with that.

OT - I see you are in St. Peters. I'm in Des Peres.
 
The while loop in your FCGI app keeps it running forever; spawn-fcgi doesn't provide any help with that, and will exit whenever your apps stops. What spawn-fcgi does do is provide a nice way to configure whatever socket you want to be listening on (and permissions for a Unix socket), so you don't have to worry about that in your FCGI app; it also remembers the PID of your FCGI app to make it easy to find and kill.

Yes, I'm out here in O'Fallon; getting ready to be pummeled with a nasty thunderstorm...
 
Back
Top