<commit_before>#include "linuxService.h"

#include <limits>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <iostream>
#include <signal.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>

#include "../abstractStreamFactory.h"

#include "../exceptions.h"
#include "tcpStreamFactory.h"
#include "../http_headers.h"
#include "../logOutput.h"

namespace simpleServer {

using ondra_shared::AbstractLogProviderFactory;

int ServiceControl::create(int argc, char **argv, StrViewA name, ServiceHandler handler) {
	bool handleExcept = false;
	try {
		if (argc < 3) {
			throw ServiceInvalidParametersException();
		}

		StrViewA *tmp_arglist = (StrViewA *)alloca(sizeof(StrViewA)*argc);


		for (int i = 0; i < argc; i++) {
			tmp_arglist[i] = StrViewA(argv[i]);
		}
		ArgList arglist(tmp_arglist, argc);


		StrViewA pidfile = arglist[1];
		StrViewA command = arglist[2];
		ArgList remainArgs = arglist.substr(3);

		RefCntPtr<LinuxService> svc = new LinuxService(pidfile);


		if (command == "start") {
			if (!svc->enterDaemon()) {
				return svc->waitForExitCode();
			}
			return svc->startService(name, handler, remainArgs);
		} else if (command == "run") {
			return svc->startService(name, handler, remainArgs);
		} else if (command == "stop") {
			handleExcept = true;
			svc->stopOtherService();return 0;
		} else if (command == "wait") {
			handleExcept = true;
			return svc->postCommand("wait",ArgList(),std::cerr,-1,true);
		} else if (command == "restart") {
			handleExcept = true;
			svc->stopOtherService();
			handleExcept = false;
			//svc->postCommand( "stop", ArgList(), std::cerr);
			if (!svc->enterDaemon()) {
					return svc->waitForExitCode();
				}
			return svc->startService(name, handler, remainArgs);
		} else if (command == "status") {
			if (svc->checkPidFile()) return 0;
			else std::cerr << "Service not running" << std::endl;
			return 1;
		} else {
				handleExcept = true;
				svc->postCommand(command, remainArgs, std::cerr);
		}
		return 0;
	} catch (SystemException &e) {
		if (handleExcept && (e.getErrNo() == ECONNREFUSED || e.getErrNo() == ENOENT)) {
			std::cerr << "Service not running" << std::endl;
		} else {
			std::cerr << "ERROR: " << e.what() << std::endl;
		}
		return e.getErrNo();
	} catch (std::exception &e) {
		std::cerr << "ERROR: " << e.what() << std::endl;
		return ENOENT;
	}
}





void LinuxService::dispatch() {


	if (getuid() == 0) {
		logWarning("The service is starting under the root priviledge. This is not recomended for the security reasons.");
	}



	Stream s;
	setsid();
	if (umbilicalCord) {
		sendExitCode(0);
		close(umbilicalCord);
		umbilicalCord = 0;
	}


	 s = mother();

	 logProgress("The service started, pid= $1",getpid);

	while (s != nullptr) {
		processRequest(s);
		s = nullptr;
		 s = mother();
		 cleanWaitings();
	}

	logProgress("The service is exiting");
}

typedef StringPool<char> Pool;


Pool::String readLine(Pool &p, Stream s) {
	std::size_t m = p.begin_add();
	int c = s();
	while (c != -1 && c != '\n') {
		p.push_back(c);
		c = s();
	}
	return p.end_add(m);
}

void LinuxService::processRequest(Stream s) {

	try {
		Pool p;
		std::vector<Pool::String> args;
		Pool::String item = readLine(p,s);
		while (!item.getView().empty()) {
			args.push_back(item);
			item = readLine(p,s);
		}

		auto argbuf = (StrViewA *)alloca(sizeof(StrViewA)*args.size());
		for (std::size_t i =0, cnt = args.size(); i < cnt; ++i) {
			argbuf[i] = args[i].getView();
		}

		ArgList argList(argbuf, args.size());
		if (argList.empty()) {
			return;
		} else {
			auto it =cmdMap.find(argList[0]);
			if (it == cmdMap.end()) {
				s << "ERROR: command '" << argList[0] << "' is not supported~-1";

			} else {
				int ret =  it->second(argList, s);
				s << "~" << ret;
			}
			s.flush();
		}
	} catch (std::exception &e) {<commit_msg>use VLA object on several places<commit_after>#include "linuxService.h"

#include <limits>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <iostream>
#include <signal.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>

#include "../abstractStreamFactory.h"

#include "../exceptions.h"
#include "tcpStreamFactory.h"
#include "../http_headers.h"
#include "../logOutput.h"
#include "shared/vla.h"

namespace simpleServer {

using ondra_shared::AbstractLogProviderFactory;
using ondra_shared::VLA;

int ServiceControl::create(int argc, char **argv, StrViewA name, ServiceHandler handler) {
	bool handleExcept = false;
	try {
		if (argc < 3) {
			throw ServiceInvalidParametersException();
		}

		VLA<StrViewA, 10> tmp_arglist(argc);


		for (int i = 0; i < argc; i++) {
			tmp_arglist[i] = StrViewA(argv[i]);
		}
		ArgList arglist(tmp_arglist);


		StrViewA pidfile = arglist[1];
		StrViewA command = arglist[2];
		ArgList remainArgs = arglist.substr(3);

		RefCntPtr<LinuxService> svc = new LinuxService(pidfile);


		if (command == "start") {
			if (!svc->enterDaemon()) {
				return svc->waitForExitCode();
			}
			return svc->startService(name, handler, remainArgs);
		} else if (command == "run") {
			return svc->startService(name, handler, remainArgs);
		} else if (command == "stop") {
			handleExcept = true;
			svc->stopOtherService();return 0;
		} else if (command == "wait") {
			handleExcept = true;
			return svc->postCommand("wait",ArgList(),std::cerr,-1,true);
		} else if (command == "restart") {
			handleExcept = true;
			svc->stopOtherService();
			handleExcept = false;
			//svc->postCommand( "stop", ArgList(), std::cerr);
			if (!svc->enterDaemon()) {
					return svc->waitForExitCode();
				}
			return svc->startService(name, handler, remainArgs);
		} else if (command == "status") {
			if (svc->checkPidFile()) return 0;
			else std::cerr << "Service not running" << std::endl;
			return 1;
		} else {
				handleExcept = true;
				svc->postCommand(command, remainArgs, std::cerr);
		}
		return 0;
	} catch (SystemException &e) {
		if (handleExcept && (e.getErrNo() == ECONNREFUSED || e.getErrNo() == ENOENT)) {
			std::cerr << "Service not running" << std::endl;
		} else {
			std::cerr << "ERROR: " << e.what() << std::endl;
		}
		return e.getErrNo();
	} catch (std::exception &e) {
		std::cerr << "ERROR: " << e.what() << std::endl;
		return ENOENT;
	}
}





void LinuxService::dispatch() {


	if (getuid() == 0) {
		logWarning("The service is starting under the root priviledge. This is not recomended for the security reasons.");
	}



	Stream s;
	setsid();
	if (umbilicalCord) {
		sendExitCode(0);
		close(umbilicalCord);
		umbilicalCord = 0;
	}


	 s = mother();

	 logProgress("The service started, pid= $1",getpid);

	while (s != nullptr) {
		processRequest(s);
		s = nullptr;
		 s = mother();
		 cleanWaitings();
	}

	logProgress("The service is exiting");
}

typedef StringPool<char> Pool;


Pool::String readLine(Pool &p, Stream s) {
	std::size_t m = p.begin_add();
	int c = s();
	while (c != -1 && c != '\n') {
		p.push_back(c);
		c = s();
	}
	return p.end_add(m);
}

void LinuxService::processRequest(Stream s) {

	try {
		Pool p;
		std::vector<Pool::String> args;
		Pool::String item = readLine(p,s);
		while (!item.getView().empty()) {
			args.push_back(item);
			item = readLine(p,s);
		}

		VLA<StrViewA, 10> argList(args.size());
		for (std::size_t i =0, cnt = args.size(); i < cnt; ++i) {
			argList[i] = args[i].getView();
		}

		if (argList.empty()) {
			return;
		} else {
			auto it =cmdMap.find(argList[0]);
			if (it == cmdMap.end()) {
				s << "ERROR: command '" << argList[0] << "' is not supported~-1";

			} else {
				int ret =  it->second(argList, s);
				s << "~" << ret;
			}
			s.flush();
		}
	} catch (std::exception &e) {
{'fraction_non_alphanumeric': 0.18149928543588412, 'fraction_numerical': 0.005456671430427439, 'mean_word_length': 3.7489204194941395, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '117728', 'n_tokens_mistral': 3183, 'n_tokens_neox': 2824, 'n_words': 661}