Difference between revisions of "How to access information in sysfs"

[quality revision] [quality revision]
m
 
m

Template:ArticleMainWriter

Template:ArticleApprovedVersion

1 Article purpose[edit]

This article provides some information about the sysfs pseudo filesystem usage from the user space.

2 Sysfs (/sys) pseudo filesystem[edit]

Sysfs provides a mean to export kernel data structures, their attributes, and the linkages between them to the user space.

Please refer to sysfs part of pseudo filesystem page.

3 Sysfs usage[edit]

Linux kernel provides a documentation[1] about the rules for sysfs usage.

Some examples are also described below with two different approaches for using sysfs entries from the user space:

  • Linux application in C language
  • bash script.

3.1 Example from Linux application[edit]

The below example is a typical sequence for using sysfs entry (here a PWM component):

  • open a file descriptor of the sysfs entry file
10  len=snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwm_channel);
11  fd = open(buf, O_RDWR);

  • if fd is correctly opened, write/read value in the file: pay attention to the "text" format
12  if (fd < 0)
13  {
14      perror("pwm/duty_cycle");
15      return fd;
16  }

  • read: store data to buffer
18  read(fd, buf, sizeof(buf));

  • write: write data from buffer
20  len = snprintf(buf, sizeof(buf), "%d", 900000);
21  write(fd, buf, len);

  • close file descriptor
30  close(fd);

3.2 Example for shell command / bash script[edit]

Operations on sysfs entries can be done by using command lines (i.e. echo for writing, cat for reading).

In this way, it is possible to use a bash script to execute a configuration sequence, similarly to what a user would do by typing multiple shell commands.

An example is provided in How_to_use_PWM_with_sysfs_interface.

4 References[edit]


<noinclude>

{{ArticleMainWriter | Jean-PhilippeR}}

{{ ArticleApprovedVersion | Jean-PhilippeR | Jean-ChristopheT,BenjaminG | No previous approved version  | BrunoB - 16Jan'19 - 10265 | 17Jan'19 }}

[[Category:How to trace and debug]]
[[Category:OpenSTLinux filesystem]]</noinclude>

== Article purpose ==
This article provides some information about the sysfs pseudo filesystem usage from the user space.

== Sysfs (/sys) pseudo filesystem ==
Sysfs provides a mean to export kernel data structures, their attributes, and the linkages between them to the user space.

Please refer to [[Pseudo filesystem#sysfs (/sys) - System filesystem|sysfs part]] of [[Pseudo filesystem|pseudo filesystem]] page.

== Sysfs usage ==
Linux kernel provides a documentation<ref>{{CodeSource | Linux kernel | Documentation/admin-guide/sysfs-rules.rst}}</ref> about the rules for sysfs usage.

Some examples are also described below with two different approaches for using sysfs entries from the user space:
* Linux application in C language
* bash script.

=== Example from Linux application ===
The below example is a typical sequence for using sysfs entry (here a PWM component):

* open a file descriptor of the sysfs entry file <pre class="brush:c;gutter:true;first-line: 10;">
<syntaxhighlight lang="c" line start="10">
len=snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwm_channel);
 fd = open(buf, O_RDWR);</pre></syntaxhighlight>

*if fd is correctly opened, write/read value in the file: pay attention to the "text" format<pre class="brush:c;gutter:true;first-line: 12;"><syntaxhighlight lang="c" line start="12">

 if (fd < 0)
 {
     perror("pwm/duty_cycle");
     return fd;
 }</pre></syntaxhighlight>

:*read: store data to buffer<pre class="brush:c;gutter:true;first-line: 18;"><syntaxhighlight lang="c" line start="18">

 read(fd, buf, sizeof(buf));</pre></syntaxhighlight>

:*write: write data from buffer<pre class="brush:c;gutter:true;first-line: 20;"><syntaxhighlight lang="c" line start="20">

 len = snprintf(buf, sizeof(buf), "%d", 900000);
 write(fd, buf, len);</pre></syntaxhighlight>

* close file descriptor<pre class="brush:c;gutter:true;first-line: 30;"><syntaxhighlight lang="c" line start="30">

 close(fd);</pre></syntaxhighlight>


=== Example for shell command / bash script ===
Operations on sysfs entries can be done by using command lines (i.e. ''echo'' for writing, ''cat'' for reading).

In this way, it is possible to use a bash script to execute a configuration sequence, similarly to what a user would do by typing multiple shell commands.

An example is provided in [[PWM_overview#How_to_use_PWM_with_sysfs_interface|How_to_use_PWM_with_sysfs_interface]].

== References ==<references />

<noinclude>

[[Category:How to trace and debug]]
[[Category:OpenSTLinux filesystem]]
{{PublicationRequestId | 10265 | 2019-01-16 |BrunoB}}</noinclude>
Line 1: Line 1:
<noinclude>
 
{{ArticleMainWriter | Jean-PhilippeR}}
 
 
{{ ArticleApprovedVersion | Jean-PhilippeR | Jean-ChristopheT,BenjaminG | No previous approved version  | BrunoB - 16Jan'19 - 10265 | 17Jan'19 }}
 
 
[[Category:How to trace and debug]]
 
[[Category:OpenSTLinux filesystem]]
 
</noinclude>
 
 
 
== Article purpose ==
 
== Article purpose ==
 
This article provides some information about the sysfs pseudo filesystem usage from the user space.
 
This article provides some information about the sysfs pseudo filesystem usage from the user space.
Line 27: Line 18:
   
 
* open a file descriptor of the sysfs entry file
 
* open a file descriptor of the sysfs entry file
<pre class="brush:c;gutter:true;first-line: 10;">
+
<syntaxhighlight lang="c" line start="10">
 
  len=snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwm_channel);
 
  len=snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwm_channel);
 
  fd = open(buf, O_RDWR);
 
  fd = open(buf, O_RDWR);
</pre>
+
</syntaxhighlight>
 
*if fd is correctly opened, write/read value in the file: pay attention to the "text" format
 
*if fd is correctly opened, write/read value in the file: pay attention to the "text" format
<pre class="brush:c;gutter:true;first-line: 12;">
+
<syntaxhighlight lang="c" line start="12">
 
  if (fd < 0)
 
  if (fd < 0)
 
  {
 
  {
Line 38: Line 29:
 
     return fd;
 
     return fd;
 
  }
 
  }
</pre>
+
</syntaxhighlight>
 
:*read: store data to buffer
 
:*read: store data to buffer
<pre class="brush:c;gutter:true;first-line: 18;">
+
<syntaxhighlight lang="c" line start="18">
 
  read(fd, buf, sizeof(buf));
 
  read(fd, buf, sizeof(buf));
</pre>
+
</syntaxhighlight>
 
:*write: write data from buffer
 
:*write: write data from buffer
<pre class="brush:c;gutter:true;first-line: 20;">
+
<syntaxhighlight lang="c" line start="20">
 
  len = snprintf(buf, sizeof(buf), "%d", 900000);
 
  len = snprintf(buf, sizeof(buf), "%d", 900000);
 
  write(fd, buf, len);
 
  write(fd, buf, len);
</pre>
+
</syntaxhighlight>
 
* close file descriptor
 
* close file descriptor
<pre class="brush:c;gutter:true;first-line: 30;">
+
<syntaxhighlight lang="c" line start="30">
 
  close(fd);
 
  close(fd);
</pre>
+
</syntaxhighlight>
   
 
=== Example for shell command / bash script ===
 
=== Example for shell command / bash script ===
Line 62: Line 53:
 
== References ==
 
== References ==
 
<references />
 
<references />
  +
  +
<noinclude>
  +
[[Category:How to trace and debug]]
  +
[[Category:OpenSTLinux filesystem]]
  +
{{PublicationRequestId | 10265 | 2019-01-16 |BrunoB}}
  +
</noinclude>