Skip to content

Commit

Permalink
Reduce code size a bit
Browse files Browse the repository at this point in the history
Do some changes that reduce the code size a bit.
  • Loading branch information
chris-se committed Jan 19, 2016
1 parent ebb5caa commit 47f4b2e
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 264 deletions.
44 changes: 16 additions & 28 deletions devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,22 @@ void wait_for_device(char *real_device_name, int *timeout, const char *device, i
* do a very simple and stupid polling loop to wait until the
* requested device is present. This could be improved a bit,
* but for now it's good enough. */
time_t start;
struct timeval tv;
int r, have_device;
time_t start, current;
int have_device;
static int have_shown_message_timeout;
int type;
unsigned int major, minor;
char uuid[16];

/* Parse device information */
if (!is_valid_device_name(device, &type, &major, &minor, uuid))
panic(0, LOG_PREFIX, "Unsupported device specified: ", device, NULL);
panic(0, "Unsupported device specified: ", device, NULL);

if (delay) {
struct timespec req = { delay, 0 };
struct timespec rem;
(void)nanosleep(&req, &rem);
}
if (delay)
(void)sleep(delay);

/* Our timeout starts *after* the rootdelay. */
r = gettimeofday(&tv, NULL);
if (r < 0)
panic(errno, LOG_PREFIX, "Couldn't determine current time for timeout", NULL);
start = tv.tv_sec;
start = time(NULL);

if (type == WANT_NAME) {
set_buf(real_device_name, MAX_PATH_LEN, device, NULL);
Expand All @@ -126,17 +119,15 @@ void wait_for_device(char *real_device_name, int *timeout, const char *device, i
}

while (have_device) {
r = gettimeofday(&tv, NULL);
if (r < 0)
panic(errno, LOG_PREFIX, "Couldn't determine current time for timeout", NULL);
if (*timeout > 0 && tv.tv_sec - start > *timeout)
panic(0, LOG_PREFIX, "Timeout while waiting for devices for / (and possibly /usr) filesystems to appear "
"(did you specify the correct ones?)", NULL);
current = time(NULL);
if (*timeout > 0 && current - start > *timeout)
panic(0, "Timeout while waiting for devices for / (and possibly /usr) filesystems to appear "
"(did you specify the correct ones?)", NULL);
/* In case this takes longer, show a nice message so the user has SOME
* idea of what's going on here. */
if (tv.tv_sec - start > DEVICE_MESSAGE_TIMEOUT && !have_shown_message_timeout) {
if (current - start > DEVICE_MESSAGE_TIMEOUT && !have_shown_message_timeout) {
have_shown_message_timeout = 1;
warn(LOG_PREFIX, "Waiting for ", device, " to appear...", NULL);
warn("Waiting for ", device, " to appear...", NULL);
}
/* Sleep for DEVICE_POLL_MSEC milliseconds, then poll again. */
struct timespec req = { 0, DEVICE_POLL_MSEC * 1000 * 1000 };
Expand All @@ -152,10 +143,8 @@ void wait_for_device(char *real_device_name, int *timeout, const char *device, i
/* Make sure we record how many seconds on the timeout are left,
* because this function may be called again for the /usr filesystem. */
if (*timeout > 0) {
r = gettimeofday(&tv, NULL);
if (r < 0)
panic(errno, LOG_PREFIX, "Couldn't determine current time for timeout", NULL);
*timeout = tv.tv_sec - start;
current = time(NULL);
*timeout = current - start;
if (*timeout <= 0)
*timeout = 1;
}
Expand Down Expand Up @@ -265,12 +254,11 @@ int is_valid_device_name(const char *device_name, int *type, unsigned int* major
if (device_name[0] == '0' && device_name[1] == 'x') {
x = strtoul(device_name + 2, &endptr, 16);
if (endptr && !*endptr) {
if (type)
if (type) {
*type = WANT_MAJMIN;
if (major)
*major = (int)(x >> 8);
if (minor)
*minor = (int)(x & 0xff);
}
return 1;
}
return 0;
Expand Down
52 changes: 24 additions & 28 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void panic(int err, ...)
* because a kernel panic obscures the message. But we need to cause
* a kernel panic (by PID 1 exiting), because if the user tells the
* kernel to reboot on panic, we want to make sure this happens. */
warn(LOG_PREFIX, "Will cause kernel panic in 10s...", NULL);
warn("Will cause kernel panic in 10s...", NULL);
sleep(10);
_exit(1);
}
Expand All @@ -61,40 +61,25 @@ void showmsgv(va_list ap, const char *before1, const char *after1, const char *a
/* Don't use stdio functions, because we link statically
* and they bloat the binary. */

va_list ap_count;
int argc = 0;
int i;
int argc = 1;
int fd;
struct iovec iov[32];
unsigned extra_arg_count = !!before1 + !!after1 + !!after2;
unsigned first_arg = 0;

va_copy(ap_count, ap);
while (va_arg(ap_count, const char *)) {
argc++;
}
va_end(ap_count);

/* Try to open /dev/kmsg, log to stderr if not possible */
fd = open(KMSG_FILENAME, O_WRONLY | O_NOCTTY | O_CLOEXEC);
if (fd < 0)
fd = 2;

/* We only support a fixed number of arguments arguments. */
if (argc + 1 + extra_arg_count > 32)
argc = 31 - extra_arg_count;
unsigned extra_arg_count = !!after1 + !!after2;
const char *arg;

if (before1) {
iov[0].iov_base = (char *)before1;
iov[0].iov_len = strlen(before1);
iov[1].iov_base = (char *)before1;
iov[1].iov_len = strlen(before1);
argc++;
first_arg = 1;
}

for (i = first_arg; i < argc; i++) {
const char *arg = va_arg(ap, const char *);
iov[i].iov_base = (char *)arg;
iov[i].iov_len = strlen(arg);
while ((arg = va_arg(ap, const char *))) {
iov[argc].iov_base = (char *)arg;
iov[argc].iov_len = strlen(arg);
argc++;
/* We only support a fixed number of arguments arguments. */
if (argc + 1 + extra_arg_count > 32)
break;
}

if (after1) {
Expand All @@ -109,9 +94,20 @@ void showmsgv(va_list ap, const char *before1, const char *after1, const char *a
argc++;
}

if (argc == 1)
return;

iov[argc].iov_base = (char *)"\n";
iov[argc].iov_len = 1;

iov[0].iov_base = (char *)LOG_PREFIX;
iov[0].iov_len = sizeof(LOG_PREFIX) - 1;

/* Try to open /dev/kmsg, log to stderr if not possible */
fd = open(KMSG_FILENAME, O_WRONLY | O_NOCTTY | O_CLOEXEC);
if (fd < 0)
fd = 2;

writev(fd, iov, argc + 1);

if (fd >= 3)
Expand Down
Loading

0 comments on commit 47f4b2e

Please sign in to comment.