Codebase list sysvinit / e0bcfb6
New upstream version 2.94 Dmitry Bogatov 5 years ago
18 changed file(s) with 643 addition(s) and 74 deletion(s). Raw diff Collapse all Expand all
0 sysvinit (2.94) unreleased; urgency=low
1
2 [ Jesse Smith ]
3
4 * When the halt command is called with the -p flag (or as poweroff)
5 the command now passes the "-h -P" flags to shutdown. This
6 in turn sets the INIT_HALT environment variable to POWEROFF.
7 Assuming this value is checked by initscripts during the
8 shutting down procedure, it should cause the system to
9 be powered off.
10 If halt is called without -p then the value of INIT_HALT
11 is not set and the default action (often set in /etc/defaut/halt)
12 is taken.
13 * Removed unnecessary malloc.h includes. Memory allocation
14 and freeing is now handled in stdlib.h
15 * Added defines for FreeBSD to make some components compile
16 on FreeBSD 11.
17 * Increased the size of the kernel command line buffer in bootlogd
18 from 256 characters to 4096. This size is defined in KERNEL_COMMAND_LENGTH
19 for easy modification downstream.
20 * Added logsave.c and logsave.8 manual page from e2fsprogs to make
21 sure logsave is available to initscripts.
22 * Updated src/Makefile to make sure bootlogd compiles with Clang.
23 * Use defined constants for password length in sulogin. Makes
24 it easier to update/patch later.
25 * Minor code fixes across multiple source files to avoid buffer
26 overflows, or uninitialized strings.
27 * Changed the way the "when" variable is used internally in shutdown.c.
28 It starts as a NULL pointer, then might get set as a pointer to optarg,
29 then it might get set to point to an argv parameter, then it might have
30 a string value copied into it, over-writing the original data. We should
31 not risk over-writing internal variables which might get used for something
32 else (it's rude and security risk). Set up "when" as its own buffer
33 that has data from optargs and/or argv copied into it.
34 * Fixed typo in init.8 manual page.
35 * Updated text of fstab-decode to explain what the utility does.
36
37
038 sysvinit (2.93) released; urgency=low
139
240 [ Jesse Smith ]
442 * Fixed typo in pidof which would prevent the -o (omit PID)
543 flag from working.
644 Fixes Debian bug ##913394.
45 * Fixed error where pidof would not omit checking PIDs passed
46 to it when the -o flag was used.
47 Fixes Debian bug #913394.
48
749
850 sysvinit (2.92) released; urgency=low
951
2626
2727 .SH DESCRIPTION
2828 .B fstab-decode
29 decodes escapes in the specified \fIARGUMENT\fRs
29 decodes escapes (such as newline characters) in the specified \fIARGUMENT\fRs
3030 and uses them to run \fICOMMAND\fR.
3131 The argument escaping uses the same rules as path escaping in
3232 \fB/etc/fstab\fR,
3333 .B /etc/mtab
3434 and \fB/proc/mtab\fR.
35
36 In essence fstab-decode can be used anytime we want to pass multiple
37 parameters to a command as a list of command line argments. It turns output
38 like this:
39
40 .nf
41 /root
42 /mnt/remote-disk
43 /home
44
45 Into one long list of parameters, "/root /mnt/remote-disk /home". This
46 can be useful when trying to work with multiple filesystems at once. For
47 instance, we can use it to unmount multiple NFS shares.
3548
3649 .SH EXIT STATUS
3750 .B fstab-decode
4255
4356 .SH EXAMPLES
4457 .nf
58 The following example reads fstab, finds all instances of VFAT filesystems and
59 prints their mount points (argument 2 in the fstab file). fstab-decode then runs
60 the specified program, umount, and passes it the list of VFAT mountpoints.
61 This unmounts all VFAT partitions.
62
63
4564 .B fstab-decode umount $(awk \[aq]$3 == \[dq]vfat\[dq] { print $2 }\[aq] /etc/fstab)
4665 .fi
0 '\" -*- coding: UTF-8 -*-
1 .\" A man page for fstab-decode(8).
2 .\"
3 .\" Copyright (C) 2006 Red Hat, Inc. All rights reserved.
4 .\"
5 .\" This copyrighted material is made available to anyone wishing to use,
6 .\" modify, copy, or redistribute it subject to the terms and conditions of the
7 .\" GNU General Public License v.2.
8 .\"
9 .\" This program is distributed in the hope that it will be useful, but WITHOUT
10 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 .\" more details.
13 .\"
14 .\" You should have received a copy of the GNU General Public License along
15 .\" with this program; if not, write to the Free Software Foundation, Inc.,
16 .\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 .\"
18 .\" Author: Miloslav Trmac <mitr@redhat.com>
19 .TH fstab-decode 8 "May 2006"
20
21 .SH NAME
22 fstab-decode \- run a command with fstab-encoded arguments
23
24 .SH SYNOPSIS
25 \fBfstab-decode\fR \fICOMMAND\fR [\fIARGUMENT\fR]...
26
27 .SH DESCRIPTION
28 .B fstab-decode
29 decodes escapes in the specified \fIARGUMENT\fRs
30 and uses them to run \fICOMMAND\fR.
31 The argument escaping uses the same rules as path escaping in
32 \fB/etc/fstab\fR,
33 .B /etc/mtab
34 and \fB/proc/mtab\fR.
35
36 .SH EXIT STATUS
37 .B fstab-decode
38 exits with status 127 if
39 .I COMMAND
40 can't be run.
41 Otherwise it exits with the status returned by \fICOMMAND\fR.
42
43 .SH EXAMPLES
44 .nf
45 .B fstab-decode umount $(awk \[aq]$3 == \[dq]vfat\[dq] { print $2 }\[aq] /etc/fstab)
46 .fi
275275 .TP 0.5i
276276 .B SIGUSR2
277277 When init receives SIGUSR2, init closes and leaves the control fifo,
278 \fB/run/initctl\f\P, closed. This may be used to make sure init is not
278 \fB/run/initctl\fP, closed. This may be used to make sure init is not
279279 holding open any files. However, it also prevents init from switching
280280 runlevels. Which means commands like shutdown no longer work.
281281 The fifo can be re-opened by sending init the SIGUSR1 signal.
0 .\" -*- nroff -*-
1 .\" Copyright 2003 by Theodore Ts'o. All Rights Reserved.
2 .\" This file may be copied under the terms of the GNU Public License.
3 .\"
4 .TH LOGSAVE 8 "@E2FSPROGS_MONTH@ @E2FSPROGS_YEAR@" "E2fsprogs version @E2FSPROGS_VERSION@"
5 .SH NAME
6 logsave \- save the output of a command in a logfile
7 .SH SYNOPSIS
8 .B logsave
9 [
10 .B \-asv
11 ]
12 .I logfile cmd_prog [ ... ]
13 .SH DESCRIPTION
14 The
15 .B logsave
16 program will execute
17 .I cmd_prog
18 with the specified argument(s), and save a copy of its output to
19 .IR logfile .
20 If the containing directory for
21 .I logfile
22 does not exist,
23 .B logsave
24 will accumulate the output in memory until it can be written out.
25 A copy of the output will also be written to standard output.
26 .PP
27 If
28 .I cmd_prog
29 is a single hyphen ('-'), then instead of executing a program,
30 .B logsave
31 will take its input from standard input and save it in
32 .I logfile
33 .PP
34 .B logsave
35 is useful for saving the output of initial boot scripts
36 until the /var partition is mounted, so the output can be written to
37 /var/log.
38 .SH OPTIONS
39 .TP
40 .B \-a
41 This option will cause the output to be appended to
42 .IR logfile ,
43 instead of replacing its current contents.
44 .TP
45 .B \-s
46 This option will cause
47 .B logsave
48 to skip writing to the log file text which is bracketed with a control-A
49 (ASCII 001 or Start of Header) and control-B (ASCII 002 or Start of
50 Text). This allows progress bar information to be visible to the user
51 on the console, while not being written to the log file.
52 .TP
53 .B \-v
54 This option will make
55 .B logsave
56 to be more verbose in its output to the user.
57 .SH AUTHOR
58 Theodore Ts'o (tytso@mit.edu)
59 .SH SEE ALSO
60 .BR fsck (8)
2222
2323 # For some known distributions we do not build all programs, otherwise we do.
2424 BIN =
25 SBIN = init halt shutdown runlevel killall5 fstab-decode
25 SBIN = init halt shutdown runlevel killall5 fstab-decode logsave
2626 USRBIN = last mesg readbootlog
2727
2828 MAN1 = last.1 lastb.1 mesg.1 readbootlog.1
2929 MAN5 = initscript.5 inittab.5 initctl.5
3030 MAN8 = halt.8 init.8 killall5.8 pidof.8 poweroff.8 reboot.8 runlevel.8
31 MAN8 += shutdown.8 telinit.8 fstab-decode.8
31 MAN8 += shutdown.8 telinit.8 fstab-decode.8 logsave.8
3232
3333 ifeq ($(DISTRO),)
3434 SBIN += sulogin bootlogd
114114 last: LDLIBS += $(STATIC)
115115 last: last.o
116116
117 logsave: LDLIBS += $(STATIC)
118 logsave: logsave.o
119
117120 mesg: LDLIBS += $(STATIC)
118121 mesg: mesg.o
119122
136139 shutdown: dowall.o shutdown.o utmp.o
137140
138141 bootlogd: LDLIBS += -lutil $(STATIC)
139 bootlogd: bootlogd.o bootlogd.h
142 bootlogd: bootlogd.o
140143
141144 readbootlog: LDLIBS += $(STATIC)
142 readbootlog: readbootlog.o bootlogd.h
145 readbootlog: readbootlog.o
143146
144147 fstab-decode: LDLIBS += $(STATIC)
145148 fstab-decode: fstab-decode.o
150153 runlevellog.o: runlevellog.h runlevellog.c paths.h
151154
152155 init.o: CPPFLAGS += $(SELINUX_DEF)
153 init.o: init.c init.h initreq.h paths.h reboot.h runlevellog.h runlevellog.c set.h paths.h
156 init.o: init.c init.h initreq.h paths.h reboot.h runlevellog.h runlevellog.c set.h
154157
155158 utmp.o:
156159
158161 init_utmp.o: utmp.c init.h initreq.h paths.h
159162 $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
160163
164 bootlogd.o: bootlogd.c bootlogd.h
165
166 readbootlog.o: readbootlog.c
167
161168 utmpdump.o: utmpdump.c oldutmp.h
162169
163170 shutdown.o: shutdown.c paths.h reboot.h initreq.h init.h
165172 halt.o: halt.c reboot.h paths.h runlevellog.c runlevellog.h
166173
167174 last.o: last.c oldutmp.h
175
176 logsave.o: logsave.c
168177
169178 consoles.o: consoles.c consoles.h
170179
2323 * along with this program; if not, write to the Free Software
2424 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2525 *
26 * *NOTE* *NOTE* *NOTE*
27 * This is a PROOF OF CONCEPT IMPLEMENTATION
28 *
29 * I have bigger plans for Debian, but for now
30 * this has to do ;)
31 *
3226 */
3327
3428 #include <sys/types.h>
3933 #include <time.h>
4034 #include <stdio.h>
4135 #include <errno.h>
42 #include <malloc.h>
4336 #include <stdlib.h>
4437 #include <unistd.h>
4538 #include <string.h>
4740 #include <getopt.h>
4841 #include <dirent.h>
4942 #include <fcntl.h>
43 #ifdef __linux__
5044 #include <pty.h>
45 #endif
46
47 #ifdef __FreeBSD__
48 #include <termios.h>
49 #include <libutil.h>
50 #endif
51
5152 #include <ctype.h>
5253 #ifdef __linux__
5354 #include <sys/mount.h>
5556 #include "bootlogd.h"
5657
5758 #define MAX_CONSOLES 16
59 #define KERNEL_COMMAND_LENGTH 4096
5860
5961 char ringbuf[32768];
6062 char *endptr = ringbuf + sizeof(ringbuf);
249251 /* This appears to be unused. unsigned int kdev; */
250252 #endif
251253 struct stat st, st2;
252 char buf[256];
254 char buf[KERNEL_COMMAND_LENGTH];
253255 char *p;
254256 int didmount = 0;
255257 int n;
278280 perror("bootlogd: /proc/cmdline");
279281 } else {
280282 buf[0] = 0;
281 if ((n = read(fd, buf, sizeof(buf) - 1)) < 0)
283 if ((n = read(fd, buf, KERNEL_COMMAND_LENGTH - 1)) < 0)
282284 perror("bootlogd: /proc/cmdline");
283285 close(fd);
284286 }
285287 if (didmount) umount("/proc");
286288
287
288289 if (n < 0) return 0;
289
290
291290
292291 /*
293292 * OK, so find console= in /proc/cmdline.
154154 /*
155155 * Switch to another runlevel.
156156 */
157 void do_shutdown(char *fl, char *tm)
157 void do_shutdown(char *fl, int should_poweroff, char *tm)
158158 {
159 char *args[8];
159 char *args[9];
160160 int i = 0;
161161
162162 args[i++] = "shutdown";
163163 args[i++] = fl;
164 if ( (! strcmp(fl, "-h") ) && (should_poweroff) )
165 args[i++] = "-P";
164166 if (tm) {
165167 args[i++] = "-t";
166168 args[i++] = tm;
258260 */
259261 c = get_runlevel();
260262 if (c != '0' && c != '6')
261 do_shutdown(do_reboot ? "-r" : "-h", tm);
263 do_shutdown(do_reboot ? "-r" : "-h", do_poweroff, tm);
262264 }
263265
264266 /*
1212 Version information is not placed in the top-level Makefile by default
1313 */
1414 #ifndef VERSION
15 #define VERSION "2.91"
15 #define VERSION "2.94"
1616 #endif
1717 /*
1818 * This file is part of the sysvinit suite,
1919 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
20 * Copyright (C) 2017-2019 Jesse Smith
2021 *
2122 * This program is free software; you can redistribute it and/or modify
2223 * it under the terms of the GNU General Public License as published by
5152 #include <string.h>
5253 #include <signal.h>
5354 #include <termios.h>
55 #ifdef __FreeBSD__
56 #include <utmpx.h>
57 #else
5458 #include <utmp.h>
59 #endif
5560 #include <ctype.h>
5661 #include <stdarg.h>
5762 #include <sys/ttydefaults.h>
6065
6166 #ifdef WITH_SELINUX
6267 # include <selinux/selinux.h>
68 #endif
69 #ifdef __FreeBSD__
70 extern char **environ;
6371 #endif
6472
6573 #ifdef __i386__
132140 int wrote_utmp_reboot = 1; /* Set when we wrote the reboot record */
133141 int wrote_wtmp_rlevel = 1; /* Set when we wrote the runlevel record */
134142 int wrote_utmp_rlevel = 1; /* Set when we wrote the runlevel record */
135 int sltime = WAIT_BETWEEN_SIGNALS; /* Sleep time between TERM and KILL */
143 int sleep_time = WAIT_BETWEEN_SIGNALS; /* Sleep time between TERM and KILL */
136144 char *argv0; /* First arguments; show up in ps listing */
137145 int maxproclen; /* Maximal length of argv[0] with \0 */
138146 struct utmp utproto; /* Only used for sizeof(utproto.ut_id) */
143151
144152 /* Used by re-exec part */
145153 int reload = 0; /* Should we do initialization stuff? */
146 char *myname="/sbin/init"; /* What should we exec */
154 char *myname=INIT; /* What should we exec */
147155 int oops_error; /* Used by some of the re-exec code. */
148156 const char *Signature = "12567362"; /* Signature for re-exec fd */
149157
287295 fprintf(fp, "-SI%u\n", got_signals);
288296 fprintf(fp, "-WR%d\n", wrote_wtmp_reboot);
289297 fprintf(fp, "-WU%d\n", wrote_utmp_reboot);
290 fprintf(fp, "-ST%d\n", sltime);
298 fprintf(fp, "-ST%d\n", sleep_time);
291299 fprintf(fp, "-DB%d\n", did_boot);
292300
293301 for (p = family; p; p = p->next) {
415423 }
416424 break;
417425 case D_SLTIME:
418 if (fscanf(f, "%d\n", &sltime) == EOF && errno != 0) {
426 if (fscanf(f, "%d\n", &sleep_time) == EOF && errno != 0) {
419427 fprintf(stderr, "%s (%d): %s\n", __FILE__, __LINE__, strerror(errno));
420428 }
421429 break;
520528 * Set the process title.
521529 */
522530 #ifdef __GNUC__
531 #ifndef __FreeBSD__
523532 __attribute__ ((format (printf, 1, 2)))
524533 #endif
534 #endif
535 /* This function already exists on FreeBSD. No need to delcare it. */
536 #ifndef __FreeBSD__
525537 static int setproctitle(char *fmt, ...)
526538 {
527539 va_list ap;
541553
542554 return len;
543555 }
556 #endif
544557
545558 /*
546559 * Set console_dev to a working console.
17041717 /*
17051718 * Yup, but check every second if we still have children.
17061719 */
1707 for(f = 0; f < sltime; f++) {
1720 for(f = 0; f < sleep_time; f++) {
17081721 for(ch = family; ch; ch = ch->next) {
17091722 if (!(ch->flags & KILLME)) continue;
17101723 if ((ch->flags & RUNNING) && !(ch->flags & ZOMBIE))
19581971 foo = arg;
19591972 ok = 1;
19601973 }
1961 if (ok == 2) sltime = st;
1974 if (ok == 2) sleep_time = st;
19621975
19631976 #endif /* INITLVL */
19641977
21592172
21602173 /*
21612174 * Attempt to re-exec.
2162 */
2163 static
2164 void re_exec(void)
2175 * Renaming to my_re_exec since re_exec is now a common function name
2176 * which conflicts.
2177 */
2178 static
2179 void my_re_exec(void)
21652180 {
21662181 CHILD *ch;
21672182 sigset_t mask, oldset;
22782293 runlevel = read_level(level);
22792294 if (runlevel == 'U') {
22802295 runlevel = oldlevel;
2281 re_exec();
2296 my_re_exec();
22822297 } else {
22832298 if (oldlevel != 'S' && runlevel == 'S') console_stty();
22842299 if (runlevel == '6' || runlevel == '0' ||
24592474 }
24602475 switch(request.cmd) {
24612476 case INIT_CMD_RUNLVL:
2462 sltime = request.sleeptime;
2477 sleep_time = request.sleeptime;
24632478 fifo_new_level(request.runlevel);
24642479 quit = 1;
24652480 break;
24662481 case INIT_CMD_POWERFAIL:
2467 sltime = request.sleeptime;
2482 sleep_time = request.sleeptime;
24682483 do_power_fail('F');
24692484 quit = 1;
24702485 break;
24712486 case INIT_CMD_POWERFAILNOW:
2472 sltime = request.sleeptime;
2487 sleep_time = request.sleeptime;
24732488 do_power_fail('L');
24742489 quit = 1;
24752490 break;
24762491 case INIT_CMD_POWEROK:
2477 sltime = request.sleeptime;
2492 sleep_time = request.sleeptime;
24782493 do_power_fail('O');
24792494 quit = 1;
24802495 break;
26802695 #endif
26812696 if (runlevel == 'U') {
26822697 runlevel = oldlevel;
2683 re_exec();
2698 my_re_exec();
26842699 } else {
26852700 if (oldlevel != 'S' && runlevel == 'S') console_stty();
26862701 if (runlevel == '6' || runlevel == '0' ||
29012916
29022917 while ((f = getopt(argc, argv, "t:e:")) != EOF) switch(f) {
29032918 case 't':
2904 sltime = atoi(optarg);
2919 sleep_time = atoi(optarg);
29052920 break;
29062921 case 'e':
29072922 if (env == NULL)
29342949 usage(progname);
29352950 request.cmd = INIT_CMD_RUNLVL;
29362951 request.runlevel = argv[optind][0];
2937 request.sleeptime = sltime;
2952 request.sleeptime = sleep_time;
29382953 }
29392954
29402955 /* Change to the root directory. */
29762991 progname, INITLVL);
29772992 exit(1);
29782993 }
2979 fprintf(fp, "%s %d", argv[optind], sltime);
2994 fprintf(fp, "%s %d", argv[optind], sleep_time);
29802995 fclose(fp);
29812996
29822997 /* And tell init about the pending runlevel change. */
153153 #define D_WROTE_WTMP_RLEVEL -16
154154 #define D_WROTE_UTMP_RLEVEL -17
155155
156 #ifdef __FreeBSD__
157 #define UTMP_FILE "/var/run/utmp"
158 #define RUN_LVL 1
159 struct utmp
160 {
161 char ut_id[4];
162 };
163 #endif
164
5757 #include <sys/wait.h>
5858 #include <unistd.h>
5959
60 char *Version = "@(#)killall5 2.86 31-Jul-2004 miquels@cistron.nl";
61
6260 #ifndef PATH_MAX
6361 # ifdef MAXPATHLEN
6462 # define PATH_MAX MAXPATHLEN
434432
435433 } while (1);
436434
437 if (real) strcpy(real, curr);
435 if (real) /* real is defined elsewhere as being PATH_MAX + 1 */
436 {
437 memset(real, '\0', PATH_MAX + 1);
438 strncpy(real, curr, PATH_MAX);
439 }
438440
439441 if (errno == EINVAL) {
440442 const size_t nlen = strlen(curr);
3333 #include <ctype.h>
3434 #include <utmp.h>
3535 #include <errno.h>
36 #include <malloc.h>
3736 #include <stdlib.h>
3837 #include <unistd.h>
3938 #include <string.h>
4645
4746 #ifndef SHUTDOWN_TIME
4847 # define SHUTDOWN_TIME 254
48 #endif
49
50 #ifndef PATH_MAX
51 #define PATH_MAX 2048
4952 #endif
5053
5154 char *Version = "@(#) last 2.85 31-Apr-2004 miquels";
253256 #define BTMP_FILE getbtmp()
254257 char *getbtmp()
255258 {
256 static char btmp[128];
259 static char btmp[PATH_MAX + 5]; /* max path + btmp + null terminator */
257260 char *p;
258261
259 strcpy(btmp, WTMP_FILE);
262 memset(btmp, '\0', PATH_MAX + 5);
263 strncpy(btmp, WTMP_FILE, PATH_MAX);
260264 if ((p = strrchr(btmp, '/')) == NULL)
261265 p = btmp;
262266 else
841845 switch (ut.ut_type) {
842846 case SHUTDOWN_TIME:
843847 if (extended) {
844 strcpy(ut.ut_line, "system down");
848 strncpy(ut.ut_line, "system down", OLD_LINESIZE - 1);
845849 quit = list(&ut, lastboot, R_NORMAL);
846850 }
847851 lastdown = lastrch = ut.ut_time;
850854 case OLD_TIME:
851855 case NEW_TIME:
852856 if (extended) {
853 strcpy(ut.ut_line,
857 strncpy(ut.ut_line,
854858 ut.ut_type == NEW_TIME ? "new time" :
855 "old time");
859 "old time", OLD_LINESIZE - 1);
856860 quit = list(&ut, lastdown, R_TIMECHANGE);
857861 }
858862 break;
859863 case BOOT_TIME:
860 strcpy(ut.ut_line, "system boot");
864 strncpy(ut.ut_line, "system boot", OLD_LINESIZE - 1);
861865 quit = list(&ut, lastdown, R_REBOOT);
862866 lastboot = ut.ut_time;
863867 down = 1;
0 /*
1 * logsave.c --- A program which saves the output of a program until
2 * /var/log is mounted.
3 *
4 * Copyright (C) 2003 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12 #ifndef _XOPEN_SOURCE
13 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
14 #endif
15 #ifndef HAVE_SIGNAL_H
16 #define HAVE_SIGNAL_H
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <errno.h>
28 #ifdef HAVE_SIGNAL_H
29 #include <signal.h>
30 #endif
31 #ifdef HAVE_GETOPT_H
32 #include <getopt.h>
33 #else
34 extern char *optarg;
35 extern int optind;
36 #endif
37
38 static int outfd = -1;
39 static int outbufsize = 0;
40 static void *outbuf = 0;
41 static int verbose = 0;
42 static int do_skip = 0;
43 static int skip_mode = 0;
44 static pid_t child_pid = -1;
45
46 static void usage(char *progname)
47 {
48 printf("Usage: %s [-asv] logfile program\n", progname);
49 exit(1);
50 }
51
52 #define SEND_LOG 0x01
53 #define SEND_CONSOLE 0x02
54 #define SEND_BOTH 0x03
55
56 /*
57 * Helper function that does the right thing if write returns a
58 * partial write, or an EAGAIN/EINTR error.
59 */
60 static int write_all(int fd, const char *buf, size_t count)
61 {
62 ssize_t ret;
63 int c = 0;
64
65 while (count > 0) {
66 ret = write(fd, buf, count);
67 if (ret < 0) {
68 if ((errno == EAGAIN) || (errno == EINTR))
69 continue;
70 return -1;
71 }
72 count -= ret;
73 buf += ret;
74 c += ret;
75 }
76 return c;
77 }
78
79 static void send_output(const char *buffer, int c, int flag)
80 {
81 const char *cp;
82 char *n;
83 int cnt, d, del;
84
85 if (c == 0)
86 c = strlen(buffer);
87
88 if (flag & SEND_CONSOLE) {
89 cnt = c;
90 cp = buffer;
91 while (cnt) {
92 del = 0;
93 for (d=0; d < cnt; d++) {
94 if (skip_mode &&
95 (cp[d] == '\001' || cp[d] == '\002')) {
96 del = 1;
97 break;
98 }
99 }
100 write_all(1, cp, d);
101 if (del)
102 d++;
103 cnt -= d;
104 cp += d;
105 }
106 }
107 if (!(flag & SEND_LOG))
108 return;
109 if (outfd > 0)
110 write_all(outfd, buffer, c);
111 else {
112 n = realloc(outbuf, outbufsize + c);
113 if (n) {
114 outbuf = n;
115 memcpy(((char *)outbuf)+outbufsize, buffer, c);
116 outbufsize += c;
117 }
118 }
119 }
120
121 static int do_read(int fd)
122 {
123 int c;
124 char buffer[4096], *cp, *sep;
125
126 c = read(fd, buffer, sizeof(buffer)-1);
127 if (c <= 0)
128 return c;
129 if (do_skip) {
130 send_output(buffer, c, SEND_CONSOLE);
131 buffer[c] = 0;
132 cp = buffer;
133 while (*cp) {
134 if (skip_mode) {
135 cp = strchr(cp, '\002');
136 if (!cp)
137 return 0;
138 cp++;
139 skip_mode = 0;
140 continue;
141 }
142 sep = strchr(cp, '\001');
143 if (sep)
144 *sep = 0;
145 send_output(cp, 0, SEND_LOG);
146 if (sep) {
147 cp = sep + 1;
148 skip_mode = 1;
149 } else
150 break;
151 }
152 } else
153 send_output(buffer, c, SEND_BOTH);
154 return c;
155 }
156
157 static void signal_term(int sig)
158 {
159 if (child_pid > 0)
160 kill(child_pid, sig);
161 }
162
163 static int run_program(char **argv)
164 {
165 int fds[2];
166 int status, rc, pid;
167 char buffer[80];
168 #ifdef HAVE_SIGNAL_H
169 struct sigaction sa;
170 #endif
171
172 if (pipe(fds) < 0) {
173 perror("pipe");
174 exit(1);
175 }
176
177 #ifdef HAVE_SIGNAL_H
178 memset(&sa, 0, sizeof(struct sigaction));
179 sa.sa_handler = signal_term;
180 sigaction(SIGINT, &sa, 0);
181 sigaction(SIGTERM, &sa, 0);
182 #ifdef SA_RESTART
183 sa.sa_flags = SA_RESTART;
184 #endif
185 #endif
186
187 pid = fork();
188 if (pid < 0) {
189 perror("vfork");
190 exit(1);
191 }
192 if (pid == 0) {
193 dup2(fds[1],1); /* fds[1] replaces stdout */
194 dup2(fds[1],2); /* fds[1] replaces stderr */
195 close(fds[0]); /* don't need this here */
196 close(fds[1]);
197
198 execvp(argv[0], argv);
199 perror(argv[0]);
200 exit(1);
201 }
202 child_pid = pid;
203 close(fds[1]);
204
205 while (!(waitpid(pid, &status, WNOHANG ))) {
206 do_read(fds[0]);
207 }
208 child_pid = -1;
209 do_read(fds[0]);
210 close(fds[0]);
211
212 if ( WIFEXITED(status) ) {
213 rc = WEXITSTATUS(status);
214 if (rc) {
215 send_output(argv[0], 0, SEND_BOTH);
216 sprintf(buffer, " exited with status code %d\n", rc);
217 send_output(buffer, 0, SEND_BOTH);
218 }
219 } else {
220 if (WIFSIGNALED(status)) {
221 send_output(argv[0], 0, SEND_BOTH);
222 sprintf(buffer, "died with signal %d\n",
223 WTERMSIG(status));
224 send_output(buffer, 0, SEND_BOTH);
225 return 1;
226 }
227 rc = 0;
228 }
229 return rc;
230 }
231
232 static int copy_from_stdin(void)
233 {
234 int c, bad_read = 0;
235
236 while (1) {
237 c = do_read(0);
238 if ((c == 0 ) ||
239 ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
240 if (bad_read++ > 3)
241 break;
242 continue;
243 }
244 if (c < 0) {
245 perror("read");
246 exit(1);
247 }
248 bad_read = 0;
249 }
250 return 0;
251 }
252
253
254
255 int main(int argc, char **argv)
256 {
257 int c, pid, rc;
258 char *outfn, **cpp;
259 int openflags = O_CREAT|O_WRONLY|O_TRUNC;
260 int send_flag = SEND_LOG;
261 int do_stdin;
262 time_t t;
263
264 while ((c = getopt(argc, argv, "+asv")) != EOF) {
265 switch (c) {
266 case 'a':
267 openflags &= ~O_TRUNC;
268 openflags |= O_APPEND;
269 break;
270 case 's':
271 do_skip = 1;
272 break;
273 case 'v':
274 verbose++;
275 send_flag |= SEND_CONSOLE;
276 break;
277 }
278 }
279 if (optind == argc || optind+1 == argc)
280 usage(argv[0]);
281 outfn = argv[optind];
282 optind++;
283 argv += optind;
284 /* argc -= optind; - this is not used */
285
286 outfd = open(outfn, openflags, 0644);
287 do_stdin = !strcmp(argv[0], "-");
288
289 send_output("Log of ", 0, send_flag);
290 if (do_stdin)
291 send_output("stdin", 0, send_flag);
292 else {
293 for (cpp = argv; *cpp; cpp++) {
294 send_output(*cpp, 0, send_flag);
295 send_output(" ", 0, send_flag);
296 }
297 }
298 send_output("\n", 0, send_flag);
299 t = time(0);
300 send_output(ctime(&t), 0, send_flag);
301 send_output("\n", 0, send_flag);
302
303 if (do_stdin)
304 rc = copy_from_stdin();
305 else
306 rc = run_program(argv);
307
308 send_output("\n", 0, send_flag);
309 t = time(0);
310 send_output(ctime(&t), 0, send_flag);
311 send_output("----------------\n", 0, send_flag);
312
313 if (outbuf) {
314 pid = fork();
315 if (pid < 0) {
316 perror("fork");
317 exit(1);
318 }
319 if (pid) {
320 if (verbose)
321 printf("Backgrounding to save %s later\n",
322 outfn);
323 exit(rc);
324 }
325 setsid(); /* To avoid getting killed by init */
326 while (outfd < 0) {
327 outfd = open(outfn, openflags, 0644);
328 sleep(1);
329 }
330 write_all(outfd, outbuf, outbufsize);
331 free(outbuf);
332 }
333 if (outfd >= 0)
334 close(outfd);
335
336 exit(rc);
337 }
3030 #include <stdarg.h>
3131 #include <getopt.h>
3232 #include <stdio.h>
33
34 #ifndef PATH_MAX
35 #define PATH_MAX 2048
36 #endif
3337
3438 int dostat(char *path, struct stat *st, int do_lstat, int quiet)
3539 {
104108 int main(int argc, char **argv)
105109 {
106110 struct stat st, st2;
107 char buf[256];
111 char buf[PATH_MAX + 1];
108112 char *path;
109113 int quiet = 0;
110114 int showdev = 0;
162166
163167 memset(buf, 0, sizeof(buf));
164168 strncpy(buf, path, sizeof(buf) - 4);
165 strcat(buf, "/..");
169 strncat(buf, "/..", 3);
166170 if (dostat(buf, &st2, 0, quiet) < 0)
167171 return 1;
168172
5353 #include <signal.h>
5454 #include <fcntl.h>
5555 #include <stdarg.h>
56 #ifdef __FreeBSD__
57 #include <utmpx.h>
58 #else
5659 #include <utmp.h>
60 #endif
5761 #include <syslog.h>
5862 #include "paths.h"
5963 #include "reboot.h"
6064 #include "initreq.h"
6165 #include "init.h"
6266
67 #ifdef __FreeBSD__
68 extern char **environ;
69 #endif
6370
6471 #define MESSAGELEN 256
72 #define STATELEN 64
73 #define WHEN_SIZE 64
6574
6675 /* Whether we should warn system is shutting down */
6776 #define QUIET_FULL 2
7584 int forcefsck = 0; /* Force fsck on reboot */
7685 char message[MESSAGELEN]; /* Warning message */
7786 char *sltime = 0; /* Sleep time */
78 char newstate[64]; /* What are we gonna do */
87 char newstate[STATELEN]; /* What are we gonna do */
7988 int doself = 0; /* Don't use init */
8089 int got_alrm = 0;
8190
224233 */
225234 void issue_warn(int mins)
226235 {
227 char buf[MESSAGELEN + sizeof(newstate)];
236 char buf[MESSAGELEN + sizeof(newstate) + 1];
228237 int len;
229238
230239 buf[0] = 0;
231 strncat(buf, message, sizeof(buf) - 1);
240 strncpy(buf, message, MESSAGELEN);
232241 len = strlen(buf);
233242
234243 if (mins == 0)
511520 char buf[128];
512521 char term[UT_LINESIZE + 6];
513522 char *sp;
514 char *when = NULL;
523 char when[WHEN_SIZE];
515524 int c, i, wt;
516525 int hours, mins;
517526 int didnolog = 0;
539548 }
540549 strcpy(down_level, "1");
541550 halttype = NULL;
551 memset(when, '\0', WHEN_SIZE);
542552
543553 /* Process the options. */
544554 while((c = getopt(argc, argv, "HPacqQkrhnfFyt:g:i:")) != EOF) {
585595 case 'y': /* Ignored for sysV compatibility */
586596 break;
587597 case 'g': /* sysv style to specify time. */
588 when = optarg;
598 strncpy(when, optarg, WHEN_SIZE - 1);
589599 break;
590600 case 'i': /* Level to go to. */
591601 if (!strchr("0156aAbBcCsS", optarg[0])) {
671681
672682 /* Read remaining words, skip time if needed. */
673683 message[0] = 0;
674 for(c = optind + (!cancel && !when); c < argc; c++) {
684 for(c = optind + (!cancel && !when[0]); c < argc; c++) {
675685 if (strlen(message) + strlen(argv[c]) + 4 > MESSAGELEN)
676686 break;
677687 strcat(message, argv[c]);
696706 }
697707
698708 /* Check syntax. */
699 if (when == NULL) {
709 if (when[0] == '\0') {
700710 if (optind == argc) usage();
701 when = argv[optind++];
711 strncpy(when, argv[optind++], WHEN_SIZE - 1);
702712 }
703713
704714 /* See if we are already running. */
717727 /* Tell users what we're gonna do. */
718728 switch(down_level[0]) {
719729 case '0':
720 strcpy(newstate, "for system halt");
730 strncpy(newstate, "for system halt", STATELEN);
721731 break;
722732 case '6':
723 strcpy(newstate, "for reboot");
733 strncpy(newstate, "for reboot", STATELEN);
724734 break;
725735 case '1':
726 strcpy(newstate, "to maintenance mode");
736 strncpy(newstate, "to maintenance mode", STATELEN);
727737 break;
728738 default:
729 sprintf(newstate, "to runlevel %s", down_level);
739 snprintf(newstate, STATELEN, "to runlevel %s", down_level);
730740 break;
731741 }
732742
764774
765775 /* Alias now and take care of old '+mins' notation. */
766776 if (!strcmp(when, "now")) strcpy(when, "0");
767 if (when[0] == '+') when++;
768
777
778 sp = when;
779 if (when[0] == '+') sp++;
769780 /* Decode shutdown time. */
770 for (sp = when; *sp; sp++) {
781 for ( ; *sp; sp++) {
771782 if (*sp != ':' && (*sp < '0' || *sp > '9'))
772783 usage();
773784 }
7171 # include <selinux/get_context_list.h>
7272 #endif
7373
74 #define PASSWORD_LENGTH 256
75
7476 #include "consoles.h"
7577 #define CONMAX 16
7678
417419 struct passwd *pw;
418420 struct spwd *spw;
419421 FILE *fp;
420 static char line[256];
421 static char sline[256];
422 static char line[PASSWORD_LENGTH];
423 static char sline[PASSWORD_LENGTH];
422424 char *p;
423425
424426 /*
452454 /*
453455 * Find root in the password file.
454456 */
455 while((p = fgets(line, 256, fp)) != NULL) {
457 while((p = fgets(line, PASSWORD_LENGTH, fp)) != NULL) {
456458 if (strncmp(line, "root:", 5) != 0)
457459 continue;
458460 p += 5;
486488 fprintf(stderr, "sulogin: %s: root password garbled\n\r", F_PASSWD);
487489 return &pwd;
488490 }
489 while((p = fgets(sline, 256, fp)) != NULL) {
491 while((p = fgets(sline, PASSWORD_LENGTH, fp)) != NULL) {
490492 if (strncmp(sline, "root:", 5) != 0)
491493 continue;
492494 p += 5;
3131 #include <time.h>
3232 #include <fcntl.h>
3333 #include <string.h>
34 #ifdef __FreeBSD__
35 #include <utmpx.h>
36 #else
3437 #include <utmp.h>
38 #endif
3539
3640 #include "init.h"
3741 #include "initreq.h"
3333
3434
3535 #define MAXLEN 4096
36 #define LINE_SIZE 80
3637 #define MAXLINES 20
3738
3839 int main(int argc, char **argv)
3940 {
4041 char buf[MAXLEN];
41 char line[83];
42 char line[LINE_SIZE + 3]; /* leave room for \r\n\0 */
4243 int i, f, ch;
4344 int len = 0;
4445 int remote = 0;
8081 if ((argc - optind) > 0) {
8182 for(f = optind; f < argc; f++) {
8283 len += strlen(argv[f]) + 1;
83 if (len >= MAXLEN-2) break;
84 if (len >= MAXLEN-4) break;
8485 strcat(buf, argv[f]);
8586 if (f < argc-1) strcat(buf, " ");
8687 }