From nobody Sat Apr 06 20:27:12 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 4VBn304q82z5HHff for ; Sat, 6 Apr 2024 20:27:24 +0000 (UTC) (envelope-from rockyhotas@tilde.team) Received: from tilde.team (tilde.team [IPv6:2607:5300:60:4f58::248]) (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) by mx1.freebsd.org (Postfix) with ESMTPS id 4VBn2z5Rkzz4W77 for ; Sat, 6 Apr 2024 20:27:23 +0000 (UTC) (envelope-from rockyhotas@tilde.team) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=tilde.team header.s=mail header.b=Fb16vaNK; dmarc=pass (policy=reject) header.from=tilde.team; spf=pass (mx1.freebsd.org: domain of rockyhotas@tilde.team designates 2607:5300:60:4f58::248 as permitted sender) smtp.mailfrom=rockyhotas@tilde.team DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tilde.team; s=mail; t=1712435235; bh=HHjarQ3wPRlmnsowe2El1zG1k2y+ihf97tlRMr7bEtU=; h=Date:From:To:Subject:Reply-To:From; b=Fb16vaNKvPzLxyzScyBjsDYLvZEusen5P+4LSQtVu/1zlOjpmfaewaVV1aqd7JRJW bEwrlk7waTDnRsKxC4zKOaeTIRKhV4DMn4jrRZ/f75nDtgYsbiHGjFAmqhtZq4YLKR g/9sOL6R8V1EitIk9ijH2CqN1CnQfFhUaQAePYDQ= Received: from localhost (mob-5-91-202-243.net.vodafone.it [5.91.202.243]) by tilde.team (Postfix) with ESMTPSA id E6C0D4C1D88 for ; Sat, 6 Apr 2024 20:27:14 +0000 (UTC) Date: Sat, 6 Apr 2024 22:27:12 +0200 From: Rocky Hotas To: freebsd-hackers@freebsd.org Subject: Kernel module: return a number from a device Message-ID: Reply-To: Rocky Hotas 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=us-ascii Content-Disposition: inline X-Spamd-Bar: --- X-Spamd-Result: default: False [-3.40 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-0.997]; MID_RHS_NOT_FQDN(0.50)[]; DMARC_POLICY_ALLOW(-0.50)[tilde.team,reject]; R_SPF_ALLOW(-0.20)[+mx]; R_DKIM_ALLOW(-0.20)[tilde.team:s=mail]; ONCE_RECEIVED(0.10)[]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_TLS_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; DKIM_TRACE(0.00)[tilde.team:+]; RCVD_COUNT_ONE(0.00)[1]; HAS_REPLYTO(0.00)[rockyhotas@tilde.team]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; MISSING_XM_UA(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; RCVD_VIA_SMTP_AUTH(0.00)[]; MLMMJ_DEST(0.00)[freebsd-hackers@freebsd.org]; ASN(0.00)[asn:16276, ipnet:2607:5300::/32, country:FR]; REPLYTO_EQ_FROM(0.00)[] X-Rspamd-Queue-Id: 4VBn2z5Rkzz4W77 Hello! I'm trying to write a simple kernel module, using as a model the example in I am a newbie. My module should be simpler than the one in the link: it should just create a read-only /dev/rolld file; each time it is read by the user (for example through `cat'), the file should provide a random number mod d_size. So, the "output" should always be 1 character. I modified the echo kernel module presented in the link. My module can successfully be loaded into the kernel and the device is created, but if I run as a user `cat /dev/rolld': $ cat /dev/rolld Opened device "rolld" successfully. and it hangs, giving no more output and without generating an error. May be this due to the fact that uiomove receives a pointer &random_out, which is a pointer to a uint32_t instead of for example a char? (And if this is the issue, how to convert a uint32_t to char inside the kernel?) Or is there some other error that I made? I paste my code below. Bye! Rocky #include #include #include #include #include #include #include #include #include static d_open_t rolld_open; static d_close_t rolld_close; static d_read_t rolld_read; static struct cdevsw rolld_cdevsw = { .d_version = D_VERSION, .d_open = rolld_open, .d_close = rolld_close, .d_read = rolld_read, .d_name = "rolld", }; /* vars */ static struct cdev *rolld_dev; static uint32_t d_size = 6; static int rolld_loader(struct module *m __unused, int what, void *arg __unused) { int error = 0; switch (what) { case MOD_LOAD: /* kldload */ error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &rolld_dev, &rolld_cdevsw, 0, UID_ROOT, GID_WHEEL, 0444, "rolld"); if (error != 0) break; printf("Roll device loaded.\n"); break; case MOD_UNLOAD: destroy_dev(rolld_dev); printf("Roll device unloaded.\n"); break; default: error = EOPNOTSUPP; break; } return (error); } static int rolld_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused, struct thread *td __unused) { int error = 0; uprintf("Opened device \"rolld\" successfully.\n"); return (error); } static int rolld_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused, struct thread *td __unused) { uprintf("Closing device \"rolld\".\n"); return (0); } static int rolld_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused) { uint32_t random_out; uint32_t random_item; int error; random_item = arc4random(); random_out = random_item % d_size; if ((error = uiomove(&random_out, 1, uio)) != 0) uprintf("uiomove failed!\n"); return (error); } DEV_MODULE(rolld, rolld_loader, NULL);