Exemple #1
0
void killAllSystems()
{
	my_printf("killing sms\n");
	killPid(messagesPid);
	my_printf("killing controller\n");
	killPid(controllerPid);
	my_printf("killing systems\n");
	killSystems(pid);
	my_printf("killing server\n");
	killPid(serverPid);
}
Exemple #2
0
void termination_handler (int signum)
/*--------------------------------------------
 * pulizia prima di uscire
 * libera la memoria condivisa
 * -----------------------------------------*/
{
	int i;
	int status;
	char writeBuffer[MSGLENGTH];
	int temp;
	

	if(getpid()==messagesPid)
	{
		formatMessage(writeBuffer,0,0,0,0,"SYSTEM STOPPING");
		write(logFileFd,writeBuffer,strlen(writeBuffer));
		close(logFileFd);
	}
	if(getpid()==mypid)		//sono il padre
	{
		my_printf("signal %d to %d\n",signum,getpid());
//		my_printf("ho ricevuto %d\n",signum);
		if(signum==SIGSEGV)
			my_printf("non bene, ho ricevuto sigsegv\n");

		killAllSystems();

		my_printf("freeing ai system nodes\n");
		free_system_nodes();

		msgctl(msg_alarm_id, IPC_RMID, 0);
		
		temp=NUMPANELS;
		NUMPANELS=0;
		for(i=0;i<temp;i++)
			free(panels[i]);
		if(panels)
			free(panels);
		panels=NULL;

		freeshm();

		my_printf("Fine\n");
		exit(0);
	}
	else
	{
		if(getpid()==serverPid)
		{
			for(i=0;i<MAXCONNECTIONS;i++)
				if(serverPids[i]>0)
					killPid(serverPids[i]);
//			exit(1);
		}
	}
}
/*!
 * \brief TerminalFolderApp::closeTerminal() tries to close the terminal app
 * \param index index of opened terminals, if more than one.
 * \return true could close the terminal
 */
bool TerminalFolderApp::closeTerminal(int index)
{
    Q_PID pid   = 0;
    bool closed = false;
    int counter = m_openPids.count();
    int before = counter;
    while (counter--)
    {
        // check if any terminal that was closed by the user
        pid = m_openPids.at(counter);
        int test = ::kill(pid, 0);
#if DEBUG_MESSAGES
        qDebug() << Q_FUNC_INFO << "pid" << pid
                 << "ret from kill" << test;
#endif
        if ( test != 0 )
        {
            m_openPids.takeAt(counter);
        }
    }
#if DEBUG_MESSAGES
    qDebug() << Q_FUNC_INFO << "active terminals" << m_openPids.count();
#endif
    if (index >= 0 && m_openPids.count() > 0)
    {
        if (index >= m_openPids.count())
        {
            index = m_openPids.count() -1;
        }
        pid    = m_openPids.takeAt(index);
        if (!(closed = killPid(pid)))
        {
            m_openPids.append(pid);
        }
    }
    if (before != m_openPids.count())
    {
       emit openCounterChanged(m_openPids.count());
    }
    return closed;
}
Exemple #4
0
bool HttpServer::startServer(bool pageServer) {
  int port = pageServer ?
    RuntimeOption::ServerPort : RuntimeOption::AdminServerPort;

  // 1. try something nice
  for (unsigned int i = 0; i < 60; i++) {
    try {
      if (pageServer) {
        m_pageServer->start();
      } else {
        m_adminServer->start();
      }
      return true;
    } catch (FailedToListenException &e) {
      if (i == 0) {
        Logger::Info("shutting down old HPHP server by /stop command");
      }

      if (errno == EACCES) {
        Logger::Error("Permission denied listening on port %d", port);
        return false;
      }

      HttpClient http;
      string url = "http://";
      url += RuntimeOption::ServerIP;
      url += ":";
      url += lexical_cast<string>(RuntimeOption::AdminServerPort);
      url += "/stop";
      StringBuffer response;
      http.get(url.c_str(), response);

      sleep(1);
    }
  }

  // 2. try something harsh
  if (RuntimeOption::ServerHarshShutdown) {
    for (unsigned int i = 0; i < 5; i++) {
      try {
        if (pageServer) {
          m_pageServer->start();
        } else {
          m_adminServer->start();
        }
        return true;
      } catch (FailedToListenException &e) {
        if (i == 0) {
          Logger::Info("shutting down old HPHP server by pid file");
        }
        killPid();
        sleep(1);
      }
    }
  }

  // 3. try something evil
  if (RuntimeOption::ServerEvilShutdown) {
    for (unsigned int i = 0; i < 60; i++) {
      try {
        if (pageServer) {
          m_pageServer->start();
        } else {
          m_adminServer->start();
        }
        return true;
      } catch (FailedToListenException &e) {
        if (i == 0) {
          Logger::Info("killing anything listening on port %d", port);
        }

        string cmd = "lsof -t -i :";
        cmd += lexical_cast<string>(port);
        cmd += " | xargs kill -9";
        Util::ssystem(cmd.c_str());

        sleep(1);
      }
    }
  }

  return false;
}
Exemple #5
0
static bool process(cchar *operation, bool quiet)
{
    cchar   *name, *off, *path;
    int     rc, launch, update, service, upstart;

    /*
        No systemd support yet
     */
    rc = 1;
    name = app->serviceName;
    launch = upstart = update = service = 0;

    if (exists("/bin/launchctl") && exists("/Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name)) {
        launch++;

    } else if (exists("/sbin/start") && exists("/etc/init/rc.conf") &&
            (exists("/etc/init/%s.conf", name) || exists("/etc/init/%s.off", name))) {
        upstart++;

    } else if (exists("/usr/sbin/update-rc.d") && exists("/etc/init.d/%s", name)) {
        update++;

    } else if (exists("/sbin/service") && exists("/etc/init.d/%s", name)) {
        service++;

    } else {
        mprError("Can't locate system tool to manage service");
        return 0;
    }

    /*
        Operations
     */
    if (smatch(operation, "install")) {
        if (launch) {
            ;

        } else if (service) {
            if (!run("/sbin/chkconfig --del %s", name)) {
                rc = 0;
            } else if (!run("/sbin/chkconfig --add %s", name)) {
                rc = 0;
            } else if (!run("/sbin/chkconfig --level 5 %s", name)) {
                rc = 0;
            }

        } else if (update) {
            ;

        } else if (upstart) {
            ;
        }

    } else if (smatch(operation, "uninstall")) {
        process("disable", 1);

        if (launch) {
            ;

        } else if (service) {
            rc = run("/sbin/chkconfig --del %s", name);

        } else if (update) {
            ;

        } else if (upstart) {
            ;
        }

    } else if (smatch(operation, "enable")) {
        /* 
            Enable service (will start on reboot)
         */
        if (launch) {
            path = sfmt("/Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name);
            /* 
                Unfortunately, there is no launchctl command to do an enable without starting. So must do a stop below.
             */
            if (!run("/bin/launchctl load -w %s", path)) {
                rc = 0;
            } else {
                rc = process("stop", 1);
            }

        } else if (update) {
            rc = run("/usr/sbin/update-rc.d %s defaults 90 10", name);
            /*
                Not supported on older versions
                rc = run("/usr/sbin/update-rc.d %s enable", name);
             */

        } else if (service) {
            rc = run("/sbin/chkconfig %s on", name);

        } else if (upstart) {
            off = sfmt("/etc/init/%s.off", name);
            if (exists(off) && !run("mv %s /etc/init/%s.conf", off, name)) {
                rc = 0;
            }
        }

    } else if (smatch(operation, "disable")) {
        process("stop", 1);
        if (launch) {
            rc = run("/bin/launchctl unload -w /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name);

        } else if (update) {
            /*  
                Not supported on older versions
                rc = run("/usr/sbin/update-rc.d %s disable", name);
             */
            rc = run("/usr/sbin/update-rc.d -f %s remove", name);

        } else if (service) {
            rc = run("/sbin/chkconfig %s off", name);

        } else if (upstart) {
            if (exists("/etc/init/%s.conf", name)) {
                rc = run("mv /etc/init/%s.conf /etc/init/%s.off", name, name);
            }
        }

    } else if (smatch(operation, "start")) {
        if (launch) {
            rc = run("/bin/launchctl load /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name);

        } else if (service) {
            rc = run("/sbin/service %s start", name);

        } else if (update) {
            rc = run("/usr/sbin/invoke-rc.d --quiet %s start", name);

        } else if (upstart) {
            rc = run("/sbin/start %s", name);
            if (!rc) {
                if (scontains(app->error, "start: Job is already running", -1)) {
                    rc = 0;
                }
            }
        }

    } else if (smatch(operation, "stop")) {
        if (launch) {
            rc = run("/bin/launchctl unload /Library/LaunchDaemons/com.%s.%s.plist", slower(BLD_COMPANY), name);

        } else if (service) {
            if (!run("/sbin/service %s stop", name)) {
                rc = killPid();
            }

        } else if (update) {
            if (!run("/usr/sbin/invoke-rc.d --quiet %s stop", name)) {
                rc = killPid();
            }

        } else if (upstart) {
            if (exists("/etc/init/%s.conf", name)) {
                rc = run("/sbin/stop %s", name);
            }
        }

    } else if (smatch(operation, "reload")) {
        rc = process("restart", 0);

    } else if (smatch(operation, "restart")) {
        process("stop", 1);
        rc = process("start", 0);

    } else if (smatch(operation, "run")) {
        runService();
    }

    if (!quiet) {
        if (!rc && app->error && *app->error) {
            mprError("Can't run command: %s\nCommand output: %s\n", app->command, app->error);
        }
        /* Logging at level one will be visible if appman -v is used */
        if (app->error && *app->error) {
            mprLog(1, "Error: %s", app->error); 
        }
        if (app->output && *app->output) {
            mprLog(1, "Output: %s", app->output); 
        }
    }
    return rc;
}
Exemple #6
0
void killSystems(int *pidArray)
/*--------------------------------------------
 * uccide i figli all'uscita per non lasciare zombie
 * int *pidArray - array con i pid dei figli
 * -----------------------------------------*/
{
	int k,l;
	int status;
	
	for(k=0;k<TOTALSYSTEMS;k++)
	{
		killPid(pidArray[k]);

		if(k<NUMSYSTEMS)	// tabella system
		{
			if(systems[k].sockfd!=-1)
				close(systems[k].sockfd);
		}
		else 
		{
			if(k<NUMSYSTEMS+NUMMULTIMETERS)//multimetro
			{
				l=k-NUMSYSTEMS;
				if(multimeters[l].sockfd!=-1)
					close(multimeters[l].sockfd);
			}
			else 	
			{
				if(k<NUMSYSTEMS+NUMMULTIMETERS+NUMKNXGATEWAYS)//knx
				{
					l=k-NUMSYSTEMS-NUMMULTIMETERS;
					if(knxGateways[l].sockfd!=-1)
						close(knxGateways[l].sockfd);
				}
				else
				{
					if(k<NUMSYSTEMS+NUMMULTIMETERS+NUMKNXGATEWAYS+NUMCEIABIGATEWAYS)//bacnet
					{
						l=k-NUMSYSTEMS-NUMMULTIMETERS-NUMKNXGATEWAYS;
						if(bacnetDevices[l].sockfd!=-1)
							close(bacnetDevices[l].sockfd);
					}
					else //ceiabi
					{
						l=k-NUMSYSTEMS-NUMMULTIMETERS-NUMKNXGATEWAYS+NUMBACNETDEVICES;
						if(CEIABIGateways[l].sockfd!=-1)
							close(CEIABIGateways[l].sockfd);
					}
				}
			}
		}



		pidArray[k]=-1;
/*		if(pidArray[k]!=-1)
		{
			
			kill(pidArray[k],SIGKILL);
			if(waitpid(pidArray[k],&status,0)==pidArray[k])
				pidArray[k]=-1;
		}*/
	}
}