shadow1ng/fscan · error

service_dir_create_failed

Error message

service_dir_create_failed

What it means

copyToServicePath tries to create/locate the systemd service directory (e.g. /etc/systemd/system or the user unit dirs) from a candidate list; none could be created or found, so it returns service_dir_create_failed.

Source

Thrown at plugins/local/systemdservice.go:163

// copyToServicePath 复制文件到服务目录
func (p *SystemdServicePlugin) copyToServicePath(targetFile string) (string, error) {
	// 选择服务目录
	serviceDirs := []string{
		"/usr/local/bin",
		"/opt/local",
		"/usr/bin",
	}

	var targetDir string
	for _, dir := range serviceDirs {
		if err := os.MkdirAll(dir, 0755); err == nil {
			targetDir = dir
			break
		}
	}

	if targetDir == "" {
		return "", fmt.Errorf("%s", i18n.GetText("service_dir_create_failed"))
	}

	// 生成服务可执行文件名
	basename := filepath.Base(targetFile)
	serviceName := strings.TrimSuffix(basename, filepath.Ext(basename))
	if serviceName == "" {
		serviceName = "system-service"
	}

	targetPath := filepath.Join(targetDir, serviceName)

	// 复制文件
	err := p.copyFile(targetFile, targetPath)
	if err != nil {
		return "", err
	}

	// 设置执行权限

View on GitHub (pinned to 95cc12e753)

Solutions

  1. Run the process with sufficient privileges (sudo/root) or ensure the user unit dir (~/.config/systemd/user) is writable
  2. Check that at least one candidate directory exists or can be created: ls -ld /etc/systemd/system
  3. Verify the filesystem is writable (not mounted ro); remount rw or use a writable unit directory
  4. Check SELinux/AppArmor denials in audit logs

Example fix

// run with elevated privileges or ensure user unit dir
sudo ./tool scan
// or verify:
mkdir -p ~/.config/systemd/user
Defensive patterns

Strategy: fallback

Validate before calling

for _, d := range []string{"/etc/systemd/system", "/usr/lib/systemd/system", os.ExpandEnv("$HOME/.config/systemd/user")} {
    if fi, err := os.Stat(d); err == nil && fi.IsDir() { break }
}
// verify writability with a test create

Try / catch

if _, err := copyToServicePath(...); err != nil {
    log.Printf("no writable systemd unit dir: %v — run privileged or use user units", err)
}

Prevention

When it happens

Trigger: All candidate service directories failed os.MkdirAll (permission denied, read-only filesystem, or the candidates list yielded no usable path).

Common situations: Running unprivileged without sudo on a system where unit dirs are not writable; immutable or read-only /etc (containers, live ISOs); SELinux/AppArmor blocking directory creation.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


AI-assisted analysis of shadow1ng/fscan@95cc12e753 (2026-09-06). Data as JSON: /api/errors/fa5907d8e9c9ee7c. Report an issue: GitHub.