File: run_part.c

package info (click to toggle)
shadow 1%3A4.17.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,752 kB
  • sloc: sh: 44,927; ansic: 34,406; xml: 12,252; exp: 3,691; makefile: 1,633; python: 722; yacc: 622; perl: 120; sed: 16
file content (105 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <config.h>

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <lib/prototypes.h>

#include "run_part.h"
#include "shadowlog_internal.h"


static int run_part(char *script_path, const char *name, const char *action)
{
	pid_t pid;
	int wait_status;
	pid_t pid_status;
	char *args[] = { script_path, NULL };

	pid=fork();
	if (pid==-1) {
		fprintf(shadow_logfd, "fork: %s\n", strerror(errno));
		return 1;
	}
	if (pid==0) {
		setenv("ACTION",action,1);
		setenv("SUBJECT",name,1);
		execv(script_path,args);
		fprintf(shadow_logfd, "execv: %s\n", strerror(errno));
		_exit(1);
	}

	pid_status = wait(&wait_status);
	if (pid_status == pid) {
		return (wait_status);
	}

	fprintf(shadow_logfd, "waitpid: %s\n", strerror(errno));
	return (1);
}

int run_parts(const char *directory, const char *name, const char *action)
{
	struct dirent **namelist;
	int scanlist;
	int n;
	int execute_result = 0;

	scanlist = scandir(directory, &namelist, NULL, alphasort);
	if (scanlist<=0) {
		return (0);
	}

	for (n=0; n<scanlist; n++) {
		char         *s;
		struct stat  sb;

		if (asprintf(&s, "%s/%s", directory, namelist[n]->d_name) == -1) {
			fprintf(shadow_logfd, "asprintf: %s\n", strerror(errno));
			for (; n<scanlist; n++) {
				free(namelist[n]);
			}
			free(namelist);
			return (1);
		}

		execute_result = 0;
		if (stat(s, &sb) == -1) {
			fprintf(shadow_logfd, "stat: %s\n", strerror(errno));
			free(s);
			for (; n<scanlist; n++) {
				free(namelist[n]);
			}
			free(namelist);
			return (1);
		}

		if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode)) {
			execute_result = run_part(s, name, action);
		}

		free(s);

		if (execute_result!=0) {
			fprintf(shadow_logfd,
				"%s: did not exit cleanly.\n",
			    namelist[n]->d_name);
			for (; n<scanlist; n++) {
				free(namelist[n]);
			}
			break;
		}

		free(namelist[n]);
	}
	free(namelist);

	return (execute_result);
}