1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-11-28 08:02:54 +00:00

sbin/init: Reduce code duplication by using %m for error message

instead of passing %s with return value from strerror().

MFC after:	2 weeks
This commit is contained in:
Xin LI 2020-12-25 17:49:33 -08:00
parent 7e75718295
commit 804b4afb9c

View File

@ -532,8 +532,7 @@ getsecuritylevel(void)
name[1] = KERN_SECURELVL;
len = sizeof curlevel;
if (sysctl(name, 2, &curlevel, &len, NULL, 0) == -1) {
emergency("cannot get kernel security level: %s",
strerror(errno));
emergency("cannot get kernel security level: %m");
return (-1);
}
return (curlevel);
@ -558,8 +557,8 @@ setsecuritylevel(int newlevel)
name[1] = KERN_SECURELVL;
if (sysctl(name, 2, NULL, NULL, &newlevel, sizeof newlevel) == -1) {
emergency(
"cannot change kernel security level from %d to %d: %s",
curlevel, newlevel, strerror(errno));
"cannot change kernel security level from %d to %d: %m",
curlevel, newlevel);
return;
}
#ifdef SECURE
@ -651,13 +650,13 @@ read_file(const char *path, void **bufp, size_t *bufsizep)
fd = open(path, O_RDONLY);
if (fd < 0) {
emergency("%s: %s", path, strerror(errno));
emergency("%s: %m", path);
return (-1);
}
error = fstat(fd, &sb);
if (error != 0) {
emergency("fstat: %s", strerror(errno));
emergency("fstat: %m");
close(fd);
return (error);
}
@ -665,14 +664,14 @@ read_file(const char *path, void **bufp, size_t *bufsizep)
bufsize = sb.st_size;
buf = malloc(bufsize);
if (buf == NULL) {
emergency("malloc: %s", strerror(errno));
emergency("malloc: %m");
close(fd);
return (error);
}
nbytes = read(fd, buf, bufsize);
if (nbytes != (ssize_t)bufsize) {
emergency("read: %s", strerror(errno));
emergency("read: %m");
close(fd);
free(buf);
return (error);
@ -680,7 +679,7 @@ read_file(const char *path, void **bufp, size_t *bufsizep)
error = close(fd);
if (error != 0) {
emergency("close: %s", strerror(errno));
emergency("close: %m");
free(buf);
return (error);
}
@ -699,20 +698,20 @@ create_file(const char *path, const void *buf, size_t bufsize)
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0700);
if (fd < 0) {
emergency("%s: %s", path, strerror(errno));
emergency("%s: %m", path);
return (-1);
}
nbytes = write(fd, buf, bufsize);
if (nbytes != (ssize_t)bufsize) {
emergency("write: %s", strerror(errno));
emergency("write: %m");
close(fd);
return (-1);
}
error = close(fd);
if (error != 0) {
emergency("close: %s", strerror(errno));
emergency("close: %m");
return (-1);
}
@ -739,11 +738,11 @@ mount_tmpfs(const char *fspath)
error = nmount(iov, iovlen, 0);
if (error != 0) {
if (*errmsg != '\0') {
emergency("cannot mount tmpfs on %s: %s: %s",
fspath, errmsg, strerror(errno));
emergency("cannot mount tmpfs on %s: %s: %m",
fspath, errmsg);
} else {
emergency("cannot mount tmpfs on %s: %s",
fspath, strerror(errno));
emergency("cannot mount tmpfs on %s: %m",
fspath);
}
return (error);
}
@ -770,7 +769,7 @@ reroot(void)
*/
error = kill(-1, SIGKILL);
if (error != 0 && errno != ESRCH) {
emergency("kill(2) failed: %s", strerror(errno));
emergency("kill(2) failed: %m");
goto out;
}
@ -792,7 +791,7 @@ reroot(void)
* Execute the temporary init.
*/
execl(_PATH_REROOT_INIT, _PATH_REROOT_INIT, "-r", NULL);
emergency("cannot exec %s: %s", _PATH_REROOT_INIT, strerror(errno));
emergency("cannot exec %s: %m", _PATH_REROOT_INIT);
out:
emergency("reroot failed; going to single user mode");
@ -812,7 +811,7 @@ reroot_phase_two(void)
*/
error = reboot(RB_REROOT);
if (error != 0) {
emergency("RB_REBOOT failed: %s", strerror(errno));
emergency("RB_REBOOT failed: %m");
goto out;
}
@ -829,8 +828,7 @@ reroot_phase_two(void)
error = sysctlbyname("kern.init_path",
init_path, &init_path_len, NULL, 0);
if (error != 0) {
emergency("failed to retrieve kern.init_path: %s",
strerror(errno));
emergency("failed to retrieve kern.init_path: %m");
goto out;
}
}
@ -845,7 +843,7 @@ reroot_phase_two(void)
*/
execl(path, path, NULL);
}
emergency("cannot exec init from %s: %s", init_path, strerror(errno));
emergency("cannot exec init from %s: %m", init_path);
out:
emergency("reroot failed; going to single user mode");
@ -879,8 +877,7 @@ single_user(void)
/* Instead of going single user, let's reboot the machine */
sync();
if (reboot(howto) == -1) {
emergency("reboot(%#x) failed, %s", howto,
strerror(errno));
emergency("reboot(%#x) failed, %m", howto);
_exit(1); /* panic and reboot */
}
warning("reboot(%#x) returned", howto);
@ -1187,9 +1184,9 @@ static int
start_session_db(void)
{
if (session_db && (*session_db->close)(session_db))
emergency("session database close: %s", strerror(errno));
emergency("session database close: %m");
if ((session_db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL) {
emergency("session database open: %s", strerror(errno));
emergency("session database open: %m");
return (1);
}
return (0);
@ -1211,7 +1208,7 @@ add_session(session_t *sp)
data.size = sizeof sp;
if ((*session_db->put)(session_db, &key, &data, 0))
emergency("insert %d: %s", sp->se_process, strerror(errno));
emergency("insert %d: %m", sp->se_process);
}
/*
@ -1226,7 +1223,7 @@ del_session(session_t *sp)
key.size = sizeof sp->se_process;
if ((*session_db->del)(session_db, &key, 0))
emergency("delete %d: %s", sp->se_process, strerror(errno));
emergency("delete %d: %m", sp->se_process);
}
/*