From: Steve Kenton <skenton@ou.edu>
To: <linux-kernel@vger.kernel.org>
Subject: /sys inconsistent newline requirement on write
Date: Mon, 4 Apr 2016 11:06:49 -0600 [thread overview]
Message-ID: <57029F29.2070905@ou.edu> (raw)
Feature or bug?
Using shell echo to write to /sys files normally adds an implicit
newline but
write() in a C program must add it explicitly. Some but not all /sys files
require a newline or the write fails. 'Extra' new lines from echo are
silently ignored and do not cause errors.
For example:
writing "1" to /sys/block/sd*/device/delete does not require a trailing
newline
writing "offline" to /sys/block/sd*/device/state does require a
trailing newline
$ make test
$ sudo ./test
write failed without newline: Invalid argument
===================================================
// Test program to illustrate required newline for writes to
/sys/block/sd*/device/state
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int sysfd;
if ((sysfd = open("/sys/block/sdb/device/state", O_WRONLY)) < 0)
perror("open failed");
else
{
if (write(sysfd, "offline", 7) < 0)
perror("write failed without newline");
if (write(sysfd, "offline\n", 8) < 0)
perror("write failed with newline");
if (close(sysfd))
perror("close failed");
}
return 0;
}
reply other threads:[~2016-04-04 17:40 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=57029F29.2070905@ou.edu \
--to=skenton@ou.edu \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox