Skip to content

Commit 346c08e

Browse files
committed
Consolidate change_runlevel to one
There are 3 copies that are identical. Move one to the common library and delete the others. Need to move the get_progname to common also so that the messages can be tailored to the exact program.
1 parent b656284 commit 346c08e

File tree

6 files changed

+112
-191
lines changed

6 files changed

+112
-191
lines changed

audisp/plugins/remote/audisp-remote.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ remote_conf_t config;
7676
static int warned = 0;
7777

7878
/* Constants */
79-
static const char *SINGLE = "1";
80-
static const char *HALT = "0";
81-
static const char *INIT_PGM = "/sbin/init";
8279
static const char *SPOOL_FILE = "/var/spool/audit/remote.log";
8380

8481
/* Local function declarations */
@@ -210,47 +207,6 @@ static int is_pipe(int fd)
210207
return 0;
211208
}
212209

213-
static void change_runlevel(const char *level)
214-
{
215-
char *argv[3];
216-
int pid;
217-
218-
// In case of halt, we need to log the message before we halt
219-
if (strcmp(level, HALT) == 0) {
220-
write_to_console("audit: will try to change runlevel to %s\n", level);
221-
}
222-
223-
pid = fork();
224-
if (pid < 0) {
225-
syslog(LOG_ALERT,
226-
"audisp-remote failed to fork switching runlevels");
227-
return;
228-
}
229-
if (pid) { /* Parent */
230-
int status;
231-
232-
// Wait until child exits
233-
if (waitpid(pid, &status, 0) < 0) {
234-
return;
235-
}
236-
237-
// Check if child exited normally, runlevel change was successful
238-
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
239-
write_to_console("audit: changed runlevel to %s\n", level);
240-
}
241-
242-
return;
243-
}
244-
245-
/* Child */
246-
argv[0] = (char *)INIT_PGM;
247-
argv[1] = (char *)level;
248-
argv[2] = NULL;
249-
execve(INIT_PGM, argv, NULL);
250-
syslog(LOG_ALERT, "audisp-remote failed to exec %s", INIT_PGM);
251-
exit(1);
252-
}
253-
254210
static void safe_exec(const char *exe, const char *message)
255211
{
256212
char *argv[3];

audisp/queue.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <syslog.h>
3030
#include <string.h>
3131
#include <fcntl.h>
32-
#include <sys/wait.h>
3332
#include "queue.h"
3433
#include "common.h"
3534

@@ -63,8 +62,6 @@ extern volatile ATOMIC_INT disp_hup;
6362
#endif
6463
static unsigned int q_depth, processing_suspended, overflowed;
6564
static ATOMIC_UNSIGNED currently_used, max_used;
66-
static const char *SINGLE = "1";
67-
static const char *HALT = "0";
6865
static int queue_full_warning = 0;
6966
static int persist_fd = -1;
7067
static int persist_sync = 0;
@@ -171,48 +168,6 @@ int init_queue(unsigned int size)
171168
return init_queue_extended(size, Q_IN_MEMORY, NULL);
172169
}
173170

174-
static void change_runlevel(const char *level)
175-
{
176-
char *argv[3];
177-
int pid;
178-
static const char *init_pgm = "/sbin/init";
179-
180-
// In case of halt, we need to log the message before we halt
181-
if (strcmp(level, HALT) == 0) {
182-
write_to_console("audit: will try to change runlevel to %s\n", level);
183-
}
184-
185-
pid = fork();
186-
if (pid < 0) {
187-
syslog(LOG_ALERT, "Audispd failed to fork switching runlevels");
188-
return;
189-
}
190-
191-
if (pid) { /* Parent */
192-
int status;
193-
194-
// Wait until child exits
195-
if (waitpid(pid, &status, 0) < 0) {
196-
return;
197-
}
198-
199-
// Check if child exited normally, runlevel change was successful
200-
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
201-
write_to_console("audit: changed runlevel to %s\n", level);
202-
}
203-
204-
return;
205-
}
206-
207-
/* Child */
208-
argv[0] = (char *)init_pgm;
209-
argv[1] = (char *)level;
210-
argv[2] = NULL;
211-
execve(init_pgm, argv, NULL);
212-
syslog(LOG_ALERT, "Audispd failed to exec %s", init_pgm);
213-
exit(1);
214-
}
215-
216171
static int do_overflow_action(struct disp_conf *config)
217172
{
218173
int rc = -1;

common/common.c

Lines changed: 108 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,36 @@
2020
* Steve Grubb <[email protected]>
2121
*/
2222

23-
#include "libaudit.h"
24-
#include "common.h"
23+
#include "config.h"
2524
#include <fcntl.h>
2625
#include <unistd.h>
2726
#include <stdio.h>
2827
#include <utmpx.h>
2928
#include <fcntl.h>
3029
#include <stdlib.h> // strtol
3130
#include <errno.h>
31+
#include <string.h>
32+
#include <libgen.h> // basename
33+
#include <signal.h>
34+
#include <sys/wait.h>
35+
#include "libaudit.h"
36+
#include "private.h" // audit_msg
37+
#include "common.h"
3238

3339
/*
3440
* This function returns 1 if it is the last record in an event.
3541
* It returns 0 otherwise.
3642
*
3743
* When processing an event stream we define the end of an event via
38-
* record type = AUDIT_EOE (audit end of event type record), or
39-
* record type = AUDIT_PROCTITLE (we note the AUDIT_PROCTITLE is always
44+
* record type = AUDIT_EOE (audit end of event type record), or
45+
* record type = AUDIT_PROCTITLE (we note the AUDIT_PROCTITLE is always
4046
* the last record), or
41-
* record type = AUDIT_KERNEL (kernel events are one record events), or
42-
* record type < AUDIT_FIRST_EVENT (only single record events appear
47+
* record type = AUDIT_KERNEL (kernel events are one record events), or
48+
* record type < AUDIT_FIRST_EVENT (only single record events appear
4349
* before this type), or
44-
* record type >= AUDIT_FIRST_ANOM_MSG (only single record events appear
50+
* record type >= AUDIT_FIRST_ANOM_MSG (only single record events appear
4551
* after this type), or
46-
* record type >= AUDIT_MAC_UNLBL_ALLOW && record type <= AUDIT_MAC_CALIPSO_DEL
52+
* record type >= AUDIT_MAC_UNLBL_ALLOW && record type <= AUDIT_MAC_CALIPSO_DEL
4753
* (these are also one record events)
4854
*/
4955
int audit_is_last_record(int type)
@@ -72,9 +78,9 @@ int write_to_console(const char *fmt, ...)
7278
return 0;
7379

7480
va_start(args, fmt);
75-
if (vdprintf(fd, fmt, args) < 0) {
81+
if (vdprintf(fd, fmt, args) < 0)
7682
res = 0;
77-
}
83+
7884
va_end(args);
7985
close(fd);
8086

@@ -100,7 +106,8 @@ void wall_message(const char* format, ...)
100106
// Only active users have a valid terminal
101107
if (entry->ut_type == USER_PROCESS) {
102108
char tty_path[128];
103-
snprintf(tty_path, sizeof(tty_path), "/dev/%s", entry->ut_line);
109+
snprintf(tty_path, sizeof(tty_path), "/dev/%s",
110+
entry->ut_line);
104111

105112
fd = open(tty_path, O_WRONLY | O_NOCTTY);
106113
if (fd != -1) {
@@ -125,40 +132,106 @@ long time_string_to_seconds(const char *time_string,
125132
if (errno || time_string == end) {
126133
if (subsystem)
127134
syslog(LOG_ERR,
128-
"%s: Error converting %s to a number - line %d",
129-
subsystem, time_string, line);
135+
"%s: Error converting %s to a number - line %d",
136+
subsystem, time_string, line);
130137
return -1;
131138
}
132139

133140
if (*end && end[1]) {
134141
if (subsystem)
135142
syslog(LOG_ERR,
136-
"%s: Unexpected characters in %s - line %d",
137-
subsystem, time_string, line);
143+
"%s: Unexpected characters in %s - line %d",
144+
subsystem, time_string, line);
138145
return -1;
139146
}
140147
switch (*end) {
141-
case 'm':
142-
i *= MINUTES;
143-
break;
144-
case 'h':
145-
i *= HOURS;
146-
break;
147-
case 'd':
148-
i *= DAYS;
149-
break;
150-
case 'M':
151-
i *= MONTHS;
152-
break;
153-
case '\0':
154-
break;
155-
default:
156-
if (subsystem)
157-
syslog(LOG_ERR,
158-
"%s: Unknown time unit in %s - line %d",
159-
subsystem, time_string, line);
160-
return -1;
148+
case 'm':
149+
i *= MINUTES;
150+
break;
151+
case 'h':
152+
i *= HOURS;
153+
break;
154+
case 'd':
155+
i *= DAYS;
156+
break;
157+
case 'M':
158+
i *= MONTHS;
159+
break;
160+
case '\0':
161+
break;
162+
default:
163+
if (subsystem)
164+
syslog(LOG_ERR,
165+
"%s: Unknown time unit in %s - line %d",
166+
subsystem, time_string, line);
167+
return -1;
161168
}
162169
return i;
163170
}
164171

172+
const char *get_progname(void)
173+
{
174+
static char progname[256];
175+
if (progname[0] == 0) {
176+
char tname[256];
177+
ssize_t len = readlink("/proc/self/exe",tname,sizeof(tname)-1);
178+
if (len != -1) {
179+
tname[len] = '\0';
180+
strcpy(progname, basename(progname));
181+
} else
182+
strcpy(progname, "unknown");
183+
}
184+
return progname;
185+
}
186+
187+
const char *SINGLE = "1";
188+
const char *HALT = "0";
189+
void change_runlevel(const char *level)
190+
{
191+
char *argv[3];
192+
int pid;
193+
struct sigaction sa;
194+
static const char *init_pgm = "/sbin/init";
195+
196+
// In case of halt, we need to log the message before we halt
197+
if (strcmp(level, HALT) == 0) {
198+
write_to_console("%s: will try to change runlevel to %s\n",
199+
get_progname(), level);
200+
}
201+
202+
pid = fork();
203+
if (pid < 0) {
204+
audit_msg(LOG_ALERT,
205+
"%s failed to fork switching runlevels",
206+
get_progname());
207+
return;
208+
}
209+
if (pid) { /* Parent */
210+
int status;
211+
212+
// Wait until child exits
213+
if (waitpid(pid, &status, 0) < 0) {
214+
audit_msg(LOG_ALERT,
215+
"%s failed to wait for child",get_progname());
216+
return;
217+
}
218+
// If child exited normally, runlevel change was successful
219+
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
220+
write_to_console("%s: changed runlevel to %s\n",
221+
get_progname(), level);
222+
}
223+
224+
return;
225+
}
226+
/* Child */
227+
sigfillset (&sa.sa_mask);
228+
sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
229+
230+
argv[0] = (char *)init_pgm;
231+
argv[1] = (char *)level;
232+
argv[2] = NULL;
233+
execve(init_pgm, argv, NULL);
234+
audit_msg(LOG_ALERT, "%s failed to exec %s", get_progname(), init_pgm);
235+
exit(1);
236+
}
237+

common/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ char *audit_strsplit_r(char *s, char **savedpp);
6464
char *audit_strsplit(char *s);
6565
int audit_is_last_record(int type);
6666

67+
extern const char *SINGLE;
68+
extern const char *HALT;
69+
void change_runlevel(const char *level);
70+
const char *get_progname(void);
6771

6872
#define MINUTES 60
6973
#define HOURS 60*MINUTES

lib/libaudit.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include <limits.h> /* for PATH_MAX */
4141
#include <sys/types.h>
4242
#include <sys/socket.h> /* AF_MAX */
43-
#include <libgen.h> /* For basename */
4443
#ifdef HAVE_LIBCAP_NG
4544
#include <cap-ng.h>
4645
#endif
@@ -120,21 +119,6 @@ static int audit_priority(int xerrno)
120119
return LOG_WARNING;
121120
}
122121

123-
static const char *get_progname(void)
124-
{
125-
static char progname[256];
126-
if (progname[0] == 0) {
127-
char tname[256];
128-
ssize_t len = readlink("/proc/self/exe",tname,sizeof(tname)-1);
129-
if (len != -1) {
130-
tname[len] = '\0';
131-
strcpy(progname, basename(progname));
132-
} else
133-
strcpy(progname, "unknown");
134-
}
135-
return progname;
136-
}
137-
138122
int audit_request_status(int fd)
139123
{
140124
int rc = audit_send(fd, AUDIT_GET, NULL, 0);

0 commit comments

Comments
 (0)