<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"syscall"
)

func main() {

	initFlags()

	// check permissions
	if os.Getuid() != 0 && os.Geteuid() != 0 {
		fmt.Printf("npserver-daemon should be run as root, have uid=%d and euid=%d\n", os.Getuid(), os.Geteuid())
		os.Exit(1)
	}

	// check start OR stop
	if (len(flags.Start) > 0 && len(flags.Stop) > 0) || (len(flags.Start) == 0 && len(flags.Stop) == 0) {
		fmt.Println("require `--start` OR `--stop` flag")
	}

	if len(flags.Stop) > 0 {
		stopDaemon(flags.Stop)
	}
	if len(flags.Start) > 0 {
		startDaemon(flags.Start)
	}
}

func startDaemon(np string) {
	// setup args for daemon call
	args := []string{
		fmt.Sprintf("--name=%s", np),
		"--noconfig",
		fmt.Sprintf("--errlog=/var/log/npdaemon-%s.log", np),
		fmt.Sprintf("--output=/var/log/%s.log", np),
		fmt.Sprintf("--pidfile=/var/run/%s.pid", np),
		"--unsafe",
		"--",
		fmt.Sprintf("/usr/local/bin/%s", np),
	}

	// append extra args to args
	args = append(args, extraArgs...)

	// start process
	proc, err := os.StartProcess("/usr/bin/daemon", args, &os.ProcAttr{
		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
		Sys: &syscall.SysProcAttr{
			Credential: &syscall.Credential{
				Uid: uint32(os.Geteuid()),
				Gid: uint32(os.Getegid()),
			},
		},
	})
	if err != nil {
		fmt.Printf("os/exec returned an error: '%s'\n", err)
		os.Exit(1)
	}

	// wait for daemon to be ready
	_, err = proc.Wait()
	if err != nil {
		fmt.Printf("proc.Wait() failed. %s\n", err)
		os.Exit(1)
	}
}

func stopDaemon(np string) {
	pidFileName := fmt.Sprintf("/var/run/%s.pid", np)
	pidFile, err := os.Open(pidFileName)
	if err != nil {
		fmt.Printf("error opening pidFile(%s): %s\n", pidFileName, err)
		killDaemon(np)
		return
	}
	defer pidFile.Close()
	pidBytes, err := ioutil.ReadAll(pidFile)
	if err != nil {
		fmt.Printf("error reading pidFile(%s): %s\n", pidFileName, err)
		killDaemon(np)
		return
	}
	pid, err := strconv.Atoi(string(pidBytes))<commit_msg>Fix pid file read (remove newline)<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
)

func main() {

	initFlags()

	// check permissions
	if os.Getuid() != 0 && os.Geteuid() != 0 {
		fmt.Printf("npserver-daemon should be run as root, have uid=%d and euid=%d\n", os.Getuid(), os.Geteuid())
		os.Exit(1)
	}

	// check start OR stop
	if (len(flags.Start) > 0 && len(flags.Stop) > 0) || (len(flags.Start) == 0 && len(flags.Stop) == 0) {
		fmt.Println("require `--start` OR `--stop` flag")
	}

	if len(flags.Stop) > 0 {
		stopDaemon(flags.Stop)
	}
	if len(flags.Start) > 0 {
		startDaemon(flags.Start)
	}
}

func startDaemon(np string) {
	// setup args for daemon call
	args := []string{
		fmt.Sprintf("--name=%s", np),
		"--noconfig",
		fmt.Sprintf("--errlog=/var/log/npdaemon-%s.log", np),
		fmt.Sprintf("--output=/var/log/%s.log", np),
		fmt.Sprintf("--pidfile=/var/run/%s.pid", np),
		"--unsafe",
		"--",
		fmt.Sprintf("/usr/local/bin/%s", np),
	}

	// append extra args to args
	args = append(args, extraArgs...)

	// start process
	proc, err := os.StartProcess("/usr/bin/daemon", args, &os.ProcAttr{
		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
		Sys: &syscall.SysProcAttr{
			Credential: &syscall.Credential{
				Uid: uint32(os.Geteuid()),
				Gid: uint32(os.Getegid()),
			},
		},
	})
	if err != nil {
		fmt.Printf("os/exec returned an error: '%s'\n", err)
		os.Exit(1)
	}

	// wait for daemon to be ready
	_, err = proc.Wait()
	if err != nil {
		fmt.Printf("proc.Wait() failed. %s\n", err)
		os.Exit(1)
	}
}

func stopDaemon(np string) {
	pidFileName := fmt.Sprintf("/var/run/%s.pid", np)
	pidFile, err := os.Open(pidFileName)
	if err != nil {
		fmt.Printf("error opening pidFile(%s): %s\n", pidFileName, err)
		killDaemon(np)
		return
	}
	defer pidFile.Close()
	pidBytes, err := ioutil.ReadAll(pidFile)
	if err != nil {
		fmt.Printf("error reading pidFile(%s): %s\n", pidFileName, err)
		killDaemon(np)
		return
	}
	pidString := string(pidBytes)
	pidString = strings.Replace(pidString, "\n", "", 0)
	pid, err := strconv.Atoi(pidString)
{'fraction_non_alphanumeric': 0.20548943405392275, 'fraction_numerical': 0.0075297546757347586, 'mean_word_length': 3.385516506922258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '642396', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1674, 'n_words': 357}