From nobody Sun Apr 07 10:50:00 2024 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VC8BL4KqDz5GXvb for ; Sun, 7 Apr 2024 10:50:02 +0000 (UTC) (envelope-from des@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (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 "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VC8BL3qlgz4yRq; Sun, 7 Apr 2024 10:50:02 +0000 (UTC) (envelope-from des@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712487002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x/sRRRrpgfj/flzYtjRGDaxa0pcmZJWpWZscL22QJRI=; b=JsYf7RyQ2jH0UvqjdWyCq4kstEFvMzAo5L+87dVHXdlLNle/v2jxSldfQ4mik0QcSKwB+i pucvMkJyu4NzJRbpoKarziAuXEDem8WFE5WcwF7pRZ3C0VmvTjx8VL5YbiUDBLUWttF7DP Vn2yKChwO5xwAGPSQKfjgyV5kPUkj/wJKDi6zQLji7aJr/fdUSOFMcSAGzIYFbviPZ+R0R p6NFOrUbLKb87mN/ZYYvQv+ZCu6r3rccBkJM7yVzBFlNaOQpmIoIoz0Y48bZbrkYXDu068 8kN4wAbMSKzIaRbm7FDRot9FaA6GEG2dZxMkQJkhboOBbKLBfBIYgz8I4R+zew== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712487002; a=rsa-sha256; cv=none; b=OUdqxJupJtWZ8ro/svzpKFMspPochjFAhMNP1G1z5E053MAySyXOMMbSyjzO8+74QpGZ2c fHb95iIR7zNwnvdakqIXIDPudeqL0eNIxG3Z+R/mhnHsodAZ2UFXZANeaYw5NgVDzW1zFa LUtuqqQzZI749Z8F5eDjkLPHh4DFXRlUOwevIMWeC16ep49Cwubmf0bNZiyHw2ywV+f7Dh xVNTmzWUc4lrLqGm9WbSH8d3PMml7CrIzTo2r7tRG7pgR194Wv0PtAICVGFGt9xbAJ/Fw3 vw+vRQQ9DM0xHpojDYkcaVEIMqWX+CAFOH11+tPhIADQv8OFpB0mxXNA2NyU4w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712487002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x/sRRRrpgfj/flzYtjRGDaxa0pcmZJWpWZscL22QJRI=; b=MALh9ulri8mAacC2vQvJO5SvWqBhK9atORob5OBL3sQEy64/VsWfsEBHsfx7i9dIhPzeoj aPfSA+TQZD22mXX38cT9QRjx79iEX62N9xjYF6BsLD1BMsqj89Q04QmUpmHiqY9ETGCbFd FYLIUMK5ZNLpveRGcC7JkpcE6l9vkN8qz/RnkTk7xg/UfEZRgnhHzl0f1sIsq1KjyIHy87 ZIFowRtuNuGf484JnSNxPKzl9wDbkZ/xy99Z/W5qn3P9BP9Q9h6nCW7jAXhnOdHJ2vTTj1 8lC6QWE73z6rXt4DE/0phu878m01r4/BMnxCVzRAl6Mv2G8VjKWMJtmaGfHM5w== Received: from ltc.des.dev (163.23.65.37.rev.sfr.net [37.65.23.163]) (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) (Authenticated sender: des) by smtp.freebsd.org (Postfix) with ESMTPSA id 4VC8BL2gtbz15q2; Sun, 7 Apr 2024 10:50:02 +0000 (UTC) (envelope-from des@freebsd.org) Received: by ltc.des.dev (Postfix, from userid 1001) id 0DC071E585; Sun, 07 Apr 2024 12:50:00 +0200 (CEST) From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= To: Rocky Hotas Cc: freebsd-hackers@freebsd.org Subject: Re: Kernel module: return a number from a device In-Reply-To: (Rocky Hotas's message of "Sat, 6 Apr 2024 22:27:12 +0200") References: User-Agent: Gnus/5.13 (Gnus v5.13) Date: Sun, 07 Apr 2024 12:50:00 +0200 Message-ID: <86r0fh5twn.fsf@ltc.des.dev> List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Rocky Hotas writes: > static int > rolld_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unuse= d) > { > uint32_t random_out; > uint32_t random_item; > int error; > > random_item =3D arc4random(); > random_out =3D random_item % d_size; > > if ((error =3D uiomove(&random_out, 1, uio)) !=3D 0) > uprintf("uiomove failed!\n"); > > return (error); > } Using a uint32_t will work on little-endian systems (such as amd64) because the least-significant byte, which is the only non-zero byte, comes first. On big-endian systems, it would simply always return 0. Furthermore, this won't only return one byte; rather, it will return one byte _at a time_, very inefficiently. This is why cat appears to hang. To truly only return one byte, you need to look at uio->uio_offset and return 0 without calling uiomove(), signaling EOF, if it is non-zero. In summary, you should write rolld_read() as: uint8_t roll =3D arc4random() % d_size; if (uio->uio_offset > 0) return (0); return (uiomove(&roll, 1, uio)); You can also use uiomove_frombuf(), which will take care of that check for you. It's a bit overkill when you're only writing a single byte, but if you wanted to output text instead of binary, you could use this: char roll[2]; roll[0] =3D '0' + arc4random() % d_size; roll[1] =3D '\n'; return (uiomove_frombuf(roll, sizeof(roll), uio)); Obviously, this will only work for d_size <=3D 9. For larger values, you will want to use snprintf(): char roll[16]; int len =3D snprintf(roll, sizeof(roll), "%d\n", arc4random() % d_s= ize); return (uiomove_frombuf(roll, len, uio)); Have fun, DES --=20 Dag-Erling Sm=C3=B8rgrav - des@FreeBSD.org