From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_PDS_PRO_TLD autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 2391 invoked from network); 23 Oct 2022 03:44:03 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 23 Oct 2022 03:44:03 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1666496643; b=JOrROQqlPEaUMKyWpmriZyBECEY7/GFMR9//r1RykgPQI5g7tISYGsSgO0poi2yj1yoQsICVNS Kf1lm/5nlDZE5vOWniRWwTBNJksXH74Ato1kK7D81R1P3B9uxBRIL3TOvgmnLwDaQgqNUUuzlo HHbVGNq+k/zy7c9w3LV+xMQ6FipeeXD95rXvo77P6ovTuQLsMASkPesNv/qAbyV8lhj1OwI5PY HVjF8+kjk2rk5B3VbzGyG834EmGRGamsBXlysp2PvQrGQOY4ZK0bE2TVfyhqlZp5Gh5a7MiKxd rvCyRk8OnzPAlF9xs2W34pGroMYJrF9lF210fAqI6EbKQg==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (iris.zentaur.org) smtp.remote-ip=198.58.127.206; dkim=pass header.d=zentaur.org header.s=dkim20200120 header.a=rsa-sha256; dmarc=pass header.from=zentaur.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1666496643; bh=vnMzn5IMiJPJ9Rr3XdSPNxMuiVObFuBvwOts/qyFzhE=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Subject:From:To: MIME-Version:Date:Message-ID:DKIM-Signature:DKIM-Signature; b=BMlq82eEag3++IDHo1xtfZQirGUgO+OFg9F/FT7KeWC6zHkpUbbc1V/PUyKq3k7FfhkUNBTdJP MyvN32N9n7Rb7DEEigAAhADtBAik//f7A784iHifRDCV96mmVDw1FBHsv3kboOYnlQw3tJ/qa1 lKBpNSiFTQUxc5DhKbutHhWtt3tz6amYJQssG1/dhx9tsyX/cqhaBgzyaBNctCtOlEQkuOHs3X jOH9TuaJEXkXYf+EvFMZg/1fsdBlV7fBgVB6DpajiwFIr8vEXmpWZGmxq50ictYX1yOQakXbHk AMUg8itvOywsOyplJxGU1QVizqF2vvAaVpqg19KolsJmHw==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:Subject:From:To:MIME-Version:Date:Message-ID:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References; bh=vnMzn5IMiJPJ9Rr3XdSPNxMuiVObFuBvwOts/qyFzhE=; b=bcPcR3o+Ds5EtvY2iNnJCBVzfM 4XWFjtIOCTWxMTzee5tjJcqxPC+mMy21sGehbcp9gJte7MXqRHjMv3taZPZMEgDmzIj56DhCVPzsh oTPi8IACDXZaI4CLVXwPnSyYUkDzdc2IU4Q7eisTV545uEB+fDMLMBdgM/Mi4QWbj+DGrwa3Ei5Za EAzNjkXHcv0k3XH7PiiB1iWb1xib34evcp6T1qMamGJivrDLRwoe7jjabJMyT8U0r5YEuXgb7HHR3 UUHRKqQIAVztq8IbQAPEaH16QNz1TEQnM8HUgbomOKpervkVXVBMIaM9qOZ0nEr51pUZHhbENh6Jh puqZri1Q==; Received: by zero.zsh.org with local id 1omRu6-0003zx-6V; Sun, 23 Oct 2022 03:44:02 +0000 Authentication-Results: zsh.org; iprev=pass (iris.zentaur.org) smtp.remote-ip=198.58.127.206; dkim=pass header.d=zentaur.org header.s=dkim20200120 header.a=rsa-sha256; dmarc=pass header.from=zentaur.org; arc=none Received: from iris.zentaur.org ([198.58.127.206]:59442) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1omRtS-0003dG-9n; Sun, 23 Oct 2022 03:43:24 +0000 Received: from iris.zentaur.org (localhost [127.0.0.1]) by iris.zentaur.org (Postfix) with ESMTP id 4Mw3vX3d67z3wZj for ; Sun, 23 Oct 2022 03:43:20 +0000 (UTC) Authentication-Results: iris.zentaur.org (amavisd-new); dkim=pass (2048-bit key) reason="pass (just generated, assumed good)" header.d=zentaur.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zentaur.org; h= content-transfer-encoding:content-type:content-type:subject :subject:from:from:to:content-language:user-agent:mime-version :date:date:message-id; s=dkim20200120; t=1666496598; x= 1666500199; bh=vnMzn5IMiJPJ9Rr3XdSPNxMuiVObFuBvwOts/qyFzhE=; b=L nBWcVlsl6M+MiMl3TrISUG+7FTdGrl8eAA2Vs7N1807q2ZuKtgg522p2djPAUHtF vuRGYZLmfYldXd/+HbRvliKTBOh406oNhAFRB2dKXSyc4+/33pAKaNMoTFpidULA +FDkAvKm28DpK5gzqI9krcgX3uJiHrD5033Z20vAL+3UY5A2Nuh7jKZoelWWlLzh g2KiSkvxaa4RQRAsAQQadt3pmDBkTlni21D9AnvII/sMkvFNNx7HYNZLj3sjcpfr rULp114gJF05iYCZvnrBvhbveJaRa3TLxA2IXqq3NrPvRV30XnadDHKO+IPXtEAc x0OTqiwyeLAUkTmymJXRw== X-Virus-Scanned: amavisd-new at iris.zentaur.org Received: from iris.zentaur.org ([127.0.0.1]) by iris.zentaur.org (iris.zentaur.org [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id rALhuNqcpsfL for ; Sun, 23 Oct 2022 03:43:18 +0000 (UTC) Received: from [192.168.72.136] (unknown [136.50.213.220]) by iris.zentaur.org (Postfix) with ESMTPSA id 4Mw3vV6JSLz3wZb for ; Sun, 23 Oct 2022 03:43:18 +0000 (UTC) Message-ID: Date: Sat, 22 Oct 2022 22:43:11 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 Content-Language: en-US To: "zsh-workers@zsh.org" From: Clinton Bunch Subject: preliminary patch for zsh/random module Content-Type: text/plain; charset=UTF-8; format=flowed X-Antivirus: Avast (VPS 221022-4, 10/22/2022), Outbound message X-Antivirus-Status: Clean Content-Transfer-Encoding: quoted-printable X-Seq: 50807 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: It still needs testing on more platforms and additional error-checking=20 as well as documentation, but I was hoping for some feedback on the=20 general ideas. diff --git a/Src/Modules/random.c b/Src/Modules/random.c new file mode 100644 index 000000000..038a56c1f --- /dev/null +++ b/Src/Modules/random.c @@ -0,0 +1,350 @@ +/* + * random.c - module to access kernel randome sources. + * + * This file is part of zsh, the Z shell. + * + * Copyright (c) 2022 Clinton Bunch + * All rights reserved. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and to distribute modified versions of this software for any + * purpose, provided that the above copyright notice and the following + * two paragraphs appear in all copies of this software. + * + * In no event shall Zolt=C3=A1n Hidv=C3=A9gi or the Zsh Development Gro= up be liable + * to any party for direct, indirect, special, incidental, or consequent= ial + * damages arising out of the use of this software and its documentation= , + * even if Zolt=C3=A1n Hidv=C3=A9gi and the Zsh Development Group have b= een=20 advised of + * the possibility of such damage. + * + * Clinton Bunch and the Zsh Development Group specifically disclaim any + * warranties, including, but not limited to, the implied warranties of + * merchantability and fitness for a particular purpose.=C2=A0 The softw= are + * provided hereunder is on an "as is" basis, and Zolt=C3=A1n Hidv=C3=A9= gi and the + * Zsh Development Group have no obligation to provide maintenance, + * support, updates, enhancements, or modifications. + * + */ + +#include "random.mdh" +#include "random.pro" + +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_RANDOM_H +#include +#endif + +/* Simplify select URANDOM specific code */ +#if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_GETRANDOM) +#define USE_URANDOM +#endif + +/* Define a buffer type for 32 bit integers */ +#ifdef UINT32_MAX +typedef uint32_t zuint32; +#else +#define UINT32_MAX=C2=A0 UINT_MAX +typedef unsigned int zuint32; +#endif + +/* buffer to pre-load integers for SRANDOM to lessen the context=20 switches */ +zuint32 rand_buff[8]; +static int buf_cnt =3D -1; + +#ifdef USE_URANDOM +/* File descriptor for /dev/urandom */ +int randfd =3D -1; +#endif /* USE_URANDOM */ + +/* + *=C2=A0 Function takes an input buffer and converts the characters to h= ex and + *=C2=A0 places them in an outbut buffer twice the size. Returns -1 if i= t can't. + *=C2=A0 Returns the size of the output otherwise. + */ + +/**/ +static int +ztr_to_hex(char *out, size_t outlen, unsigned char *input, size_t inlen) +{ +=C2=A0=C2=A0=C2=A0 int i =3D 0, j =3D 0; + +=C2=A0=C2=A0=C2=A0 if (outlen < (inlen * 2 + 1)) +=C2=A0=C2=A0=C2=A0 return -1; + +=C2=A0=C2=A0=C2=A0 for(i =3D 0, j =3D 0; i < inlen; i++, j+=3D2) { +=C2=A0=C2=A0=C2=A0 if (j < outlen) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 sprintf((char *)(out + j),"%02X",i= nput[i]); +=C2=A0=C2=A0=C2=A0 } else { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return -1; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 out[j]=3D'\0'; +=C2=A0=C2=A0=C2=A0 return j; +} + +/**/ +static int +getrandom_buffer(void *buf, size_t len) +{ +=C2=A0=C2=A0=C2=A0 int ret; + +#ifdef HAVE_ARC4RANDOM +=C2=A0=C2=A0=C2=A0 arc4random_buf(buf,len); +=C2=A0=C2=A0=C2=A0 ret =3D len; +#elif defined(HAVE_GETRANDOM) +=C2=A0=C2=A0=C2=A0 ret=3Dgetrandom(buf,len,0); +#else +=C2=A0=C2=A0=C2=A0 ret=3Dread(randfd,buf,len); +#endif +=C2=A0=C2=A0=C2=A0 return ret; +} + +/* + * Implements the getrandom builtin to return a string of random bytes o= f + * user-specified length.=C2=A0 It either prints them to stdout or retur= ns them + * in a parameter passed on the command line. + * + */ + +/**/ +static int +bin_getrandom(char *name, char **argv, Options ops, int func) +{ + +=C2=A0=C2=A0=C2=A0 size_t len=3D8; +=C2=A0=C2=A0=C2=A0 size_t byte_len=3D0, outlen; +=C2=A0=C2=A0=C2=A0 int integer_out=3D0, i; +=C2=A0=C2=A0=C2=A0 char int2str[11];=C2=A0 /*maximum string length of a = 32-bit integer +=20 null */ + +=C2=A0=C2=A0=C2=A0 unsigned char *buf; +=C2=A0=C2=A0=C2=A0 zuint32=C2=A0 *int_buf; +=C2=A0=C2=A0=C2=A0 char *scalar =3D NULL, *arrname=3D NULL, *endptr, *le= n_arg, *outbuff; +=C2=A0=C2=A0=C2=A0 char **array; + +=C2=A0=C2=A0=C2=A0 if (OPT_ISSET(ops, 's')) { +=C2=A0=C2=A0=C2=A0 scalar =3D OPT_ARG(ops, 's'); +=C2=A0=C2=A0=C2=A0 if (!isident(scalar)) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name, "argument to -s not= an identifier: %s", scalar); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (OPT_ISSET(ops,'a')) { +=C2=A0=C2=A0=C2=A0 arrname =3D OPT_ARG(ops, 'a'); +=C2=A0=C2=A0=C2=A0 if (!isident(arrname)) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name,"argument to -a not = an identifier: %s", arrname); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (OPT_ISSET(ops, 'i')) { +=C2=A0=C2=A0=C2=A0 if (!OPT_ISSET(ops,'a')) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name,"-i requires -a"); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 integer_out=3D1; +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (OPT_ISSET(ops, 'l')) { +=C2=A0=C2=A0=C2=A0 errno =3D 0; + +=C2=A0=C2=A0=C2=A0 len_arg=3DOPT_ARG(ops, 'l'); +=C2=A0=C2=A0=C2=A0 len =3D strtoul(len_arg, &endptr, 10); +=C2=A0=C2=A0=C2=A0 if (errno !=3D 0) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name, "%s: %e", len_arg, = errno); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } else if (*len_arg =3D=3D '\0' || *endptr !=3D '\0')= { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name, "%s: invalid decima= l number", len_arg); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } else if (len > 64 || (integer_out && len > 16) || l= en <=3D 0) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zwarnnam(name,"length must be betw= een 1 and %d you specified: %d", +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (integer_out?16= :64), len); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (!byte_len) +=C2=A0=C2=A0=C2=A0 byte_len =3D len; +=C2=A0=C2=A0=C2=A0 if (integer_out) +=C2=A0=C2=A0=C2=A0 byte_len =3D len*sizeof(zuint32); + +=C2=A0=C2=A0=C2=A0 buf =3D zalloc(byte_len); + +=C2=A0=C2=A0=C2=A0 if (getrandom_buffer(buf, byte_len) < 0) { +=C2=A0=C2=A0=C2=A0 zwarnnam(name,"Couldn't get random data"); +=C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (OPT_ISSET(ops, 'r')) { +=C2=A0=C2=A0=C2=A0 outbuff =3D (char *) buf; +=C2=A0=C2=A0=C2=A0 outlen=C2=A0 =3D byte_len; +=C2=A0=C2=A0=C2=A0 } else { +=C2=A0=C2=A0=C2=A0 outlen=3Dbyte_len*2+1; +=C2=A0=C2=A0=C2=A0 outbuff =3D (char*) zalloc(outlen); +=C2=A0=C2=A0=C2=A0 ztr_to_hex(outbuff, outlen, buf, byte_len); +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (scalar) { +=C2=A0=C2=A0=C2=A0 setsparam(scalar, metafy(outbuff, outlen, META_DUP)); +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if(arrname) { +=C2=A0=C2=A0=C2=A0 array =3D (char **) zalloc((len+1)*sizeof(char *)); +=C2=A0=C2=A0=C2=A0 array[len] =3D NULL; + +=C2=A0=C2=A0=C2=A0 if(integer_out) +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 int_buf=3D(zuint32 *)buf; + +=C2=A0=C2=A0=C2=A0 for(i=3D0;i < len;i++) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 if(integer_out) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 sprintf(int2str,"%u",int_buf[i]); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 } else { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 sprintf(int2str,"%d",buf[i]); +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 array[i]=3Dztrdup(int2str); +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 setaparam(arrname,array); +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (!scalar && !arrname) { +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 fwrite(outbuff,1,outlen,stdou= t); +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (outbuff !=3D=C2=A0 (void = *) buf) { +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 zfree(outbuff,outlen); +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 } + + +=C2=A0=C2=A0=C2=A0 zfree(buf, len); +=C2=A0=C2=A0=C2=A0 return 0; +} + + + +/**/ +static zlong +get_srandom(UNUSED(Param pm)) { + +=C2=A0=C2=A0=C2=A0 if(buf_cnt < 0) { +=C2=A0=C2=A0=C2=A0 getrandom_buffer((void*) rand_buff,sizeof(rand_buff))= ; +=C2=A0=C2=A0=C2=A0 buf_cnt=3D7; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 return rand_buff[buf_cnt--]; +} + +/**/ +static mnumber +math_zrandom(UNUSED(char *name), UNUSED(int argc), UNUSED(mnumber *argv)= , +=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 UNUSED(int id)) +{ +=C2=A0=C2=A0=C2=A0 mnumber ret; +=C2=A0=C2=A0=C2=A0 uint32_t r; + +=C2=A0=C2=A0=C2=A0 r=3Dget_srandom(NULL); +=C2=A0=C2=A0=C2=A0 ret.type =3D MN_FLOAT; +=C2=A0=C2=A0=C2=A0 ret.u.d =3D r/(UINT32_MAX+0.0); + +=C2=A0=C2=A0=C2=A0 return ret; +} + +static struct builtin bintab[] =3D { +=C2=A0=C2=A0=C2=A0 BUILTIN("getrandom", 0, bin_getrandom, 0, 5, 0, "ria:= s:l:%", NULL), +}; + +static const struct gsu_integer srandom_gsu =3D +{ get_srandom, nullintsetfn, stdunsetfn }; + +static struct paramdef patab[] =3D { +=C2=A0=C2=A0=C2=A0 SPECIALPMDEF("SRANDOM", PM_INTEGER|PM_READONLY,=20 &srandom_gsu,NULL,NULL), +}; + +static struct mathfunc mftab[] =3D { +=C2=A0=C2=A0=C2=A0 NUMMATHFUNC("zrandom", math_zrandom, 0, 0, 0), +}; + +static struct features module_features =3D { +=C2=A0=C2=A0=C2=A0 bintab, sizeof(bintab)/sizeof(*bintab), +=C2=A0=C2=A0=C2=A0 NULL, 0, +=C2=A0=C2=A0=C2=A0 mftab, sizeof(mftab)/sizeof(*mftab), +=C2=A0=C2=A0=C2=A0 patab, sizeof(patab)/sizeof(*patab), +=C2=A0=C2=A0=C2=A0 0 +}; + +/**/ +int +setup_(UNUSED(Module m)) +{ +#ifdef USE_URANDOM +=C2=A0=C2=A0=C2=A0 struct stat st; + +=C2=A0=C2=A0=C2=A0 if (lstat("/dev/urandom",&st) < 0) { +=C2=A0=C2=A0=C2=A0 zwarn("No kernel random pool found."); +=C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } + +=C2=A0=C2=A0=C2=A0 if (!(S_ISCHR(st.st_mode)) ) { +=C2=A0=C2=A0=C2=A0 zwarn("No kernel random pool found."); +=C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +#endif /* USE_URANDOM */ +=C2=A0=C2=A0=C2=A0 return 0; +} + +/**/ +int +features_(Module m, char ***features) +{ +=C2=A0=C2=A0=C2=A0 *features =3D featuresarray(m, &module_features); +=C2=A0=C2=A0=C2=A0 return 0; +} + +/**/ +int +enables_(Module m, int **enables) +{ +=C2=A0=C2=A0=C2=A0 return handlefeatures(m, &module_features, enables); +} + +/**/ +int +boot_(Module m) +{ +#ifdef USE_URANDOM +=C2=A0=C2=A0=C2=A0 int tmpfd=3D-1; + +=C2=A0=C2=A0=C2=A0 if ((tmpfd =3D open("/dev/urandom", O_RDONLY)) < 0) { +=C2=A0=C2=A0=C2=A0 zwarnnam("Could not access kernel random pool"); +=C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +=C2=A0=C2=A0=C2=A0 randfd =3D movefd(tmpfd); +=C2=A0=C2=A0=C2=A0 if (randfd < 0) +=C2=A0=C2=A0=C2=A0 zwarnnam("Could not access kernel random pool"); +=C2=A0=C2=A0=C2=A0 return 1; +=C2=A0=C2=A0=C2=A0 } +#endif +=C2=A0=C2=A0=C2=A0 return 0; +} + +/**/ +int +cleanup_(Module m) +{ +=C2=A0=C2=A0=C2=A0 return setfeatureenables(m, &module_features, NULL); +} + +/**/ +int +finish_(UNUSED(Module m)) +{ +#ifdef USE_URANDOM +=C2=A0=C2=A0=C2=A0 if (randfd >=3D 0) +=C2=A0=C2=A0=C2=A0 zclose(randfd); +#endif /* USE_URANDOM */ +=C2=A0=C2=A0=C2=A0 return 0; +} diff --git a/Src/Modules/random.mdd b/Src/Modules/random.mdd new file mode 100644 index 000000000..3803a4533 --- /dev/null +++ b/Src/Modules/random.mdd @@ -0,0 +1,7 @@ +name=3Dzsh/random +link=3Deither +load=3Dyes + +autofeatures=3D"b:getrandom p:SRANDOM f:zrandom" + +objects=3D"random.o" diff --git a/configure.ac b/configure.ac index 074141d38..f1fa01274 100644 --- a/configure.ac +++ b/configure.ac @@ -675,6 +675,7 @@ fi =C2=A0AC_CHECK_HEADERS(sys/time.h sys/times.h sys/select.h termcap.h ter= mio.h \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0termios.h sys/param.h = sys/filio.h string.h memory.h \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0limits.h fcntl.h libc.= h sys/utsname.h sys/resource.h \ +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 sys/random.h \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0locale.h errno.h stdio= .h stdarg.h varargs.h stdlib.h \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0unistd.h sys/capabilit= y.h \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0utmp.h utmpx.h sys/typ= es.h pwd.h grp.h poll.h sys/mman.h \ @@ -1337,6 +1338,7 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cygwin_con= v_path \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 nanosleep = \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 srand_dete= rministic \ +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 getrandom arc4random \ =C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 setutxent = getutxent endutxent getutent) =C2=A0AC_FUNC_STRCOLL