#include #include #include #include #include #include #include #include #include #include #include #include #include static int tcpserver_getport(const char *service) { int ret = -1; struct servent *serv = getservbyname (service,"tcp"); if (serv == NULL){ const char *pt = service; while (isdigit(*pt)) pt++; if (isdigit(*service) && *pt == '\0'){ ret = atoi(service); }else{ tlmp_error ("No service %s defined",service); } }else{ ret = ntohs(serv->s_port); } return ret; } int main (int argc, char *argv[]) { glocal int ret = -1; glocal const char *port = NULL; glocal const char *bind = "0.0.0.0"; glocal bool background = false; glocal.ret = (argc,argv); setarg ('b',"bind","Bind on one IP",glocal.bind,false); setarg ('g',"background","Run in background",glocal.background,false); setarg ('p',"port","Listen on TCP port",glocal.port,true); fprintf (stderr,"%s\n",msg); syslog (LOG_ERR,"%s",msg); int ret = -1; int fd = socket (AF_INET,SOCK_STREAM,0); if (fd == -1){ tlmp_error ("Can't create socket (%s)",strerror(errno)); }else{ int port; struct hostent *ent = gethostbyname (glocal.bind); if (ent == NULL){ tlmp_error ("Can resolve the address or host %s",glocal.bind); }else if ((port = tcpserver_getport(glocal.port))==-1){ tlmp_error ("Can resolve the port %s",glocal.port); }else{ struct sockaddr_in sin; memcpy (&sin.sin_addr,ent->h_addr,ent->h_length); sin.sin_port = htons(port); sin.sin_family = AF_INET; if (bind(fd,(const sockaddr*)&sin,sizeof(sin))==-1){ tlmp_error ("Can't bind to socket (%s)",strerror(errno)); }else if (listen(fd,10)==-1){ tlmp_error ("Can't listen to socket (%s)",strerror(errno)); }else{ if (glocal.background){ pid_t pid = fork(); if (pid == (pid_t)-1){ tlmp_error ("Can't go background (%s),exiting" ,strerror(errno)); exit (-1); }else if (pid != 0){ exit (0); }else{ close (0); close (1); close (2); setsid(); } } ret = 0; while (1){ fd_set fdin; FD_ZERO (&fdin); FD_SET (fd,&fdin); int ok = select (fd+1,&fdin,NULL,NULL,NULL); if (ok == -1){ tlmp_error ("Select error %s",strerror(errno)); ret = -1; break; }else if (ok > 0){ // We call linuxconf and let it handle the request pid_t pid = fork(); if (pid == 0){ dup2(fd,0); close (fd); execl ("/bin/linuxconf","linuxconf","--http",NULL); tlmp_error ("Can't exec linuxconf (%s)",strerror(errno)); _exit (-1); }else if (pid == (pid_t)-1){ tlmp_error ("Can't fork (%s)",strerror(errno)); }else{ // Wait until linuxconf ends int status; while (wait(&status)!=-1); } } } } } } return ret; return glocal.ret; }