From owner-dev-commits-src-all@freebsd.org Sat Dec 26 01:52:57 2020 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 3F9E14D0626; Sat, 26 Dec 2020 01:52:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4D2mzY1JYKz4dp6; Sat, 26 Dec 2020 01:52:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1F9364F27; Sat, 26 Dec 2020 01:52:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 0BQ1qvN5077156; Sat, 26 Dec 2020 01:52:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 0BQ1qvxH077155; Sat, 26 Dec 2020 01:52:57 GMT (envelope-from git) Date: Sat, 26 Dec 2020 01:52:57 GMT Message-Id: <202012260152.0BQ1qvxH077155@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Xin LI Subject: git: 804b4afb9cf8 - sbin/init: Reduce code duplication by using %m for error message instead of passing %s with return value from strerror(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: delphij X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 804b4afb9cf897dc353d9ba379dbedbba1541016 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "Commit messages for all branches of the src repository." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Dec 2020 01:52:57 -0000 The branch main has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=804b4afb9cf897dc353d9ba379dbedbba1541016 commit 804b4afb9cf897dc353d9ba379dbedbba1541016 Author: Xin LI AuthorDate: 2020-12-26 01:49:33 +0000 Commit: Xin LI CommitDate: 2020-12-26 01:49:33 +0000 sbin/init: Reduce code duplication by using %m for error message instead of passing %s with return value from strerror(). MFC after: 2 weeks --- sbin/init/init.c | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/sbin/init/init.c b/sbin/init/init.c index f7c4144eb6a5..c5f3c121f7f9 100644 --- a/sbin/init/init.c +++ b/sbin/init/init.c @@ -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); } /*