From: "Casper Ti. Vector" <caspervector@gmail.com>
To: supervision@list.skarnet.org
Cc: j.deboynepollard-newsgroups@ntlworld.com
Subject: Re: The "Unix Philosophy 2020" document
Date: Mon, 25 Nov 2019 10:52:02 +0800 [thread overview]
Message-ID: <20191125025202.oqu4ennu3lexnxsa@CasperVector> (raw)
In-Reply-To: <CADQ2Nw_kwvyfTbuMm4Qzjfm0prNZPXhOp_DQzGLK29NifiNYyg@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1612 bytes --]
On Sun, Nov 24, 2019 at 05:13:15PM -0300, Guillermo wrote:
> Those are just two if statements 'sharing' a body for brevity. That
> deals with errors in the openat() and subsequent write() calls, for
> the file that controls cgroup membership. By displaying a message
> constructed in the same way in both cases, and then throwing an
> exception. *Shrug*
That particular piece of code
> if (0 > cgroup_procs_fd.get()) {
> procs_file_error: ...
> }
> if (0 > write(cgroup_procs_fd.get(), "0\n", 2)) goto procs_file_error;
seems equivalent to
> if (0 > cgroup_procs_fd.get() ||
> 0 > write(cgroup_procs_fd.get(), "0\n", 2)) {
> ...
> }
If the error handling branches that need reuse become more complex, the
following way can also be considered (cf. [1]):
> ...
> if (...) goto err;
> ...
> if (...) goto err;
> ...
> return;
> err:
> ...
> return;
Macros and/or helper functions (again cf. [1]; they can be factored into
a mini-library in nosh) can also be used to reduce boilerplate like
> const int error(errno);
> std::fprintf(stderr, ..., std::strerror(error));
> throw EXIT_FAILURE;
which can be easily observed after the attached patch is applied.
BTW, it seems that the value of errno is passed to std::strerror()
before anything can change the errno, which implies that `const int
error(errno);' can be left out and `errno' can be directly used as the
argument of std::strerror().
[1] <https://gitea.com/CasperVector/decryst/src/branch/master/src/decr_lsa.c>.
--
My current OpenPGP key:
RSA4096/0x227E8CAAB7AA186C (expires: 2020.10.19)
7077 7781 B859 5166 AE07 0286 227E 8CAA B7AA 186C
[-- Attachment #2: move-to-control-group.patch --]
[-- Type: text/x-diff, Size: 1562 bytes --]
--- move-to-control-group.cpp 2018-09-14 21:48:55.000000000 +0800
+++ move-to-control-group.new.cpp 2019-11-25 10:50:20.518459398 +0800
@@ -50,9 +50,8 @@
next_prog = arg0_of(args);
FileStar self_cgroup(open_my_control_group_info("/proc/self/cgroup"));
- if (!self_cgroup) {
+ if (!self_cgroup && ENOENT != errno) { // `ENOENT == error' is what we'll see on a BSD.
const int error(errno);
- if (ENOENT == error) return; // This is what we'll see on a BSD.
std::fprintf(stderr, "%s: FATAL: %s: %s\n", prog, "/proc/self/cgroup", std::strerror(error));
throw EXIT_FAILURE;
}
@@ -73,20 +72,16 @@
current = prefix + current;
- if (0 > mkdirat(AT_FDCWD, current.c_str(), 0755)) {
+ if (0 > mkdirat(AT_FDCWD, current.c_str(), 0755) && EEXIST != error) {
const int error(errno);
- if (EEXIST != error) {
- std::fprintf(stderr, "%s: FATAL: %s: %s\n", prog, current.c_str(), std::strerror(error));
- throw EXIT_FAILURE;
- }
+ std::fprintf(stderr, "%s: FATAL: %s: %s\n", prog, current.c_str(), std::strerror(error));
+ throw EXIT_FAILURE;
}
const FileDescriptorOwner cgroup_procs_fd(open_appendexisting_at(AT_FDCWD, (current + "/cgroup.procs").c_str()));
- if (0 > cgroup_procs_fd.get()) {
-procs_file_error:
+ if (0 > cgroup_procs_fd.get() || 0 > write(cgroup_procs_fd.get(), "0\n", 2)) {
const int error(errno);
std::fprintf(stderr, "%s: FATAL: %s%s: %s\n", prog, current.c_str(), "/cgroup.procs", std::strerror(error));
throw EXIT_FAILURE;
}
- if (0 > write(cgroup_procs_fd.get(), "0\n", 2)) goto procs_file_error;
}
next prev parent reply other threads:[~2019-11-25 2:52 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20190831130730.ki6ma7i5curucowe@caspervector>
[not found] ` <em5af31b6f-4dd3-4a31-a6cf-beccb100238d@elzian>
[not found] ` <20190901091157.bjtfhqq6d2rg75yo@CasperVector>
2019-09-27 8:38 ` Casper Ti. Vector
2019-10-12 17:37 ` Casper Ti. Vector
2019-10-12 18:58 ` Steve Litt
2019-10-12 19:14 ` Casper Ti. Vector
2019-10-13 3:31 ` Casper Ti. Vector
2019-10-13 8:21 ` Oliver Schad
2019-10-13 13:57 ` Casper Ti. Vector
2019-12-08 17:04 ` Casper Ti. Vector
2019-10-14 6:35 ` Jens Rehsack
2019-10-22 15:33 ` Casper Ti. Vector
2019-10-22 16:54 ` Steve Litt
2019-10-22 17:07 ` Casper Ti. Vector
2019-11-17 6:26 ` Casper Ti. Vector
2019-11-17 7:28 ` Casper Ti. Vector
2019-11-24 20:13 ` Guillermo
[not found] ` <20191125025202.oqu4ennu3lexnxsa@caspervector>
2019-11-25 2:52 ` Casper Ti. Vector [this message]
2019-11-25 14:20 ` Casper Ti. Vector
2019-11-30 12:13 ` Jeff
2019-11-30 12:20 ` Jeff
2019-11-30 12:45 ` Jeff
2019-11-30 13:29 ` Laurent Bercot
2019-11-30 14:43 ` Casper Ti. Vector
2019-11-30 15:01 ` Jeff
2019-11-30 15:48 ` Casper Ti. Vector
2019-11-30 16:58 ` Jeff
2019-12-26 17:52 ` Casper Ti. Vector
2019-12-27 1:09 ` Oliver Schad
2019-12-27 11:11 ` Casper Ti. Vector
2019-12-27 2:57 ` Steve Litt
2019-12-27 13:54 ` Casper Ti. Vector
2019-12-27 15:37 ` Casper Ti. Vector
2020-01-04 9:10 ` Casper Ti. Vector
2020-01-04 18:25 ` fungal-net
2020-01-05 0:36 ` Casper Ti. Vector
2020-01-05 6:31 ` Casper Ti. Vector
2020-01-05 6:54 ` Casper Ti. Vector
2019-12-27 23:05 ` Steve Litt
[not found] ` <20191227135411.jbtxorloetcvv5bx@caspervector>
[not found] ` <20191227153719.2tt4bbidp3v7t23u@caspervector>
[not found] ` <20200104091013.wesvxvcqxquc5n2h@caspervector>
2020-01-04 12:32 ` Laurent Bercot
2019-12-27 21:29 ` Steve Litt
2019-12-27 23:34 ` Alex Suykov
2019-12-28 10:35 ` Casper Ti. Vector
2019-10-28 15:34 ` Avery Payne
2019-10-28 15:51 ` Casper Ti. Vector
[not found] ` <20190901091157.bjtfhqq6d2rg75yo@caspervector>
[not found] ` <20190927083816.tectynx7dzlfcvb7@caspervector>
[not found] ` <20191012173743.drzlgnrw4hib6hh4@caspervector>
[not found] ` <20191117062644.lt6wfmqwijqqhc5w@caspervector>
[not found] ` <20191226175258.o2nsregew6tlqlbu@caspervector>
2019-12-26 19:17 ` Laurent Bercot
2019-12-27 11:23 ` Casper Ti. Vector
2019-12-28 2:24 ` Alex Suykov
2019-12-28 2:57 ` eric vidal
2019-12-28 14:04 ` Alex Suykov
2019-12-28 18:05 ` Guillermo
2019-12-28 23:19 ` Oliver Schad
2019-12-28 18:12 ` Oliver Schad
2019-12-28 21:32 ` eric vidal
2019-12-28 6:46 ` Steve Litt
2019-12-28 13:37 ` Alex Suykov
2019-12-28 13:54 ` Casper Ti. Vector
2019-12-28 17:41 ` Oliver Schad
2019-12-28 18:29 ` Serge E. Hallyn
2019-12-28 23:16 ` Oliver Schad
2019-12-28 21:31 ` eric vidal
2019-12-29 16:07 ` Alex Suykov
2019-12-29 20:32 ` Oliver Schad
2019-12-28 10:26 ` Casper Ti. Vector
[not found] ` <20191227112309.3fow6vynss2ifw4t@caspervector>
2019-12-27 12:32 ` Laurent Bercot
2019-12-27 13:48 ` Casper Ti. Vector
2019-12-27 23:54 ` Oliver Schad
2019-12-27 23:56 ` Oliver Schad
2019-12-28 15:19 ` Guillermo
2019-12-28 18:16 ` Oliver Schad
2019-12-28 20:44 ` Laurent Bercot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191125025202.oqu4ennu3lexnxsa@CasperVector \
--to=caspervector@gmail.com \
--cc=j.deboynepollard-newsgroups@ntlworld.com \
--cc=supervision@list.skarnet.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).