From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/6892 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: getrandom syscall Date: Wed, 28 Jan 2015 09:54:10 -0500 Message-ID: <20150128145410.GH4574@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1422456873 3014 80.91.229.3 (28 Jan 2015 14:54:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 28 Jan 2015 14:54:33 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-6905-gllmg-musl=m.gmane.org@lists.openwall.com Wed Jan 28 15:54:33 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1YGU0f-0000xM-Ry for gllmg-musl@m.gmane.org; Wed, 28 Jan 2015 15:54:25 +0100 Original-Received: (qmail 9227 invoked by uid 550); 28 Jan 2015 14:54:23 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 8171 invoked from network); 28 Jan 2015 14:54:22 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:6892 Archived-At: On Tue, Jan 27, 2015 at 11:12:46PM +0100, Daniel Cegiełka wrote: > best regards, > Daniel Thanks. I've been wanting to get this added as well as a getentropy function (the other API for the same thing). > #include > #include > #include "syscall.h" > > int getrandom(void *buf, size_t len) > { > int ret, pre_errno = errno; There's no need to save/restore errno here. errno is only meaningful after a function returns an error code. On success it should not be inspected and could contain junk. > if (len > 256) { > errno = EIO; > return -1; > } Could you explain the motivation for this part? > do { > ret = syscall(SYS_getrandom, buf, len, 0); > } while (ret == -1 && errno == EINTR); This would be more efficient (and avoid your errno issue entirely) if you use __syscall which returns -errcode rather than storing errcode in errno. It allows the whole loop to be inlined with no function call. Something like: while ((ret = __syscall(SYS_getrandom, buf, len, 0)) == -EINTR); Of course there's the question of whether it should loop on EINTR anyway; I don't know. Also if this can block there's the question of whether it should be cancellable, but that can be decided later. Finally, I wonder if it would make sense to use other fallbacks in the case where the syscall is not supported -- perhaps the aux vector AT_RANDOM or even /dev/urandom? (But I'd rather avoid doing anything that depends on fds, which would make a function that appears to always-work but actually fails on resource exhaustion.) Rich