From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.sysutils.supervision.general/2707 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "Casper Ti. Vector" Newsgroups: gmane.comp.sysutils.supervision.general Subject: Re: The "Unix Philosophy 2020" document Date: Mon, 25 Nov 2019 10:52:02 +0800 Message-ID: <20191125025202.oqu4ennu3lexnxsa@CasperVector> References: <20190831130730.ki6ma7i5curucowe@CasperVector> <20190901091157.bjtfhqq6d2rg75yo@CasperVector> <20190927083816.tectynx7dzlfcvb7@CasperVector> <20191012173743.drzlgnrw4hib6hh4@CasperVector> <20191117062644.lt6wfmqwijqqhc5w@CasperVector> <20191117072850.bokuozrpokvfq662@CasperVector> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="kwgmkcz7vcquvrkn" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="66319"; mail-complaints-to="usenet@blaine.gmane.org" Cc: j.deboynepollard-newsgroups@ntlworld.com To: supervision@list.skarnet.org Original-X-From: supervision-return-2296-gcsg-supervision=m.gmane.org@list.skarnet.org Mon Nov 25 03:52:14 2019 Return-path: Envelope-to: gcsg-supervision@m.gmane.org Original-Received: from alyss.skarnet.org ([95.142.172.232]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1iZ4Ty-000H1P-Jo for gcsg-supervision@m.gmane.org; Mon, 25 Nov 2019 03:52:10 +0100 Original-Received: (qmail 2851 invoked by uid 89); 25 Nov 2019 02:52:34 -0000 Mailing-List: contact supervision-help@list.skarnet.org; run by ezmlm Original-Sender: Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Original-Received: (qmail 2844 invoked from network); 25 Nov 2019 02:52:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:date:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=px+vLCA6HDWub284Yd6RIwlp5eEOLFA1DQv23AeuIO8=; b=Qjo5+heFVPfOl1llsuW1pFEA/Vwqu9ch1AJz4WQSaEPzjYDJb41za29Wkl+V1vyTgw fMaK6CIAwEYNnkS78lXKsZF9G3k6/AlUdlNWjAoTfkJAvQncWcznzypV2sLzT05Z9/SA DcFaZQSvyzyfVIrbxik1ImmVCkEtffaXSM1KZGVJ/ZHGLd/7hAasTOIpov97u6QB4lH/ De7/JTwA0FxpeCHr9P4UVcFz4uGkbICQxpbrEka8TDJ2cCobdNz6qHznHLmCfE7jpOTH v/1HcoUDEhcn6NBSfM58e8Cx9B58YhtRCfyIztSBTaip5ccKH+F0OIsaLbk8a1RY162p BEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=px+vLCA6HDWub284Yd6RIwlp5eEOLFA1DQv23AeuIO8=; b=taC4SUcmLAGE4wAE0f2jMC3CACvTnIzxTVdO5bSKqrY1nkokJOBAQ4t2MnmH8dO9hV aSw8MouQFjAg/fpKlhPdWoX79NOZpo0TAHOmp9ilzMvJcFxssMyuKwfPN70q3V5f0Uh0 apRKDDTMG/p2iP2TZLQgwzMiCoNsRd8IngooDpMw2TzQzKRtKnEdTf7zb4to+axCG+Af CpD5D2xPmBYffL15kIHeYwU6M0wPltbGiwkcwtl6Shco6Z4E2XKCmmPgawR9sM+zSKjk bVHNmu9GyVLthI1o6/g334rIWOsSjk0Ydq9oxjQaKR5EAhT/C2IXESHDk18dMe4i0hhr dKag== X-Gm-Message-State: APjAAAV2BD4XrX3AOriu5zzrA0QHJBYRpQ59Cdf+kP3sTO2+PX69rEXM EMdpkbGdupc7zINPLc4mmQaopcDAIdo= X-Google-Smtp-Source: APXvYqy79WOKg5dTW5zFBzsHpb6pafwoTA2kUbqdZnVXy9+8+c02VYhzuhX6tFoAdOORrX8GbOLEuw== X-Received: by 2002:a17:90a:77c8:: with SMTP id e8mr34711082pjs.83.1574650326104; Sun, 24 Nov 2019 18:52:06 -0800 (PST) X-Google-Original-From: "Casper Ti. Vector" Mail-Followup-To: supervision@list.skarnet.org, j.deboynepollard-newsgroups@ntlworld.com Content-Disposition: inline In-Reply-To: Xref: news.gmane.org gmane.comp.sysutils.supervision.general:2707 Archived-At: --kwgmkcz7vcquvrkn Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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] . -- My current OpenPGP key: RSA4096/0x227E8CAAB7AA186C (expires: 2020.10.19) 7077 7781 B859 5166 AE07 0286 227E 8CAA B7AA 186C --kwgmkcz7vcquvrkn Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="move-to-control-group.patch" --- 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; } --kwgmkcz7vcquvrkn--