RocketLogger  2.0.2
sysfs.c
Go to the documentation of this file.
1 
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 
42 #include "sysfs.h"
43 
45 #define SYSFS_STRING_BUFFER_LENGTH 16
46 
48 static char string_buffer[SYSFS_STRING_BUFFER_LENGTH];
49 
50 int sysfs_export(char const *const sysfs_file, int value) {
51  return sysfs_write_int(sysfs_file, value);
52 }
53 
54 int sysfs_unexport(char const *const sysfs_file, int value) {
55  return sysfs_write_int(sysfs_file, value);
56 }
57 
58 int sysfs_is_exported(char const *const sysfs_path) {
59  // check for existence of file (not permissions)
60  int ret = access(sysfs_path, F_OK);
61  if (ret == 0) {
62  return 1;
63  }
64  if (ret < 0) {
65  // inexistent file is unexported
66  if (errno == ENOENT) {
67  return 0;
68  }
69  return -1;
70  }
71 
72  // treat insufficient permissions as failure
73  return -1;
74 }
75 
76 int sysfs_export_unexported(char const *const sysfs_path,
77  char const *const sysfs_export_file, int value) {
78  int exported = sysfs_is_exported(sysfs_path);
79  if (exported < 0) {
80  return -1;
81  }
82 
83  // if exported return immediately, otherwise export
84  if (exported == 1) {
85  return 1;
86  } else {
87  return sysfs_export(sysfs_export_file, value);
88  }
89 }
90 
91 int sysfs_unexport_exported(char const *const sysfs_path,
92  char const *const sysfs_unexport_file, int value) {
93  int exported = sysfs_is_exported(sysfs_path);
94  if (exported < 0) {
95  return -1;
96  }
97 
98  // if not exported return immediately, otherwise unexport
99  if (exported == 0) {
100  return 1;
101  } else {
102  return sysfs_unexport(sysfs_unexport_file, value);
103  }
104 }
105 
106 int sysfs_write_string(char const *const sysfs_file, char const *const value) {
107  int fd = open(sysfs_file, O_WRONLY);
108  if (fd < 0) {
109  return -1;
110  }
111 
112  int len = strlen(value);
113  int ret = write(fd, value, len);
114  close(fd);
115  if (ret != len) {
116  return -1;
117  }
118  return 0;
119 }
120 
121 int sysfs_read_string(char const *const sysfs_file, char *const value,
122  int length) {
123  int fd = open(sysfs_file, O_RDONLY);
124  if (fd < 0) {
125  return -1;
126  }
127 
128  int len = read(fd, value, length);
129  close(fd);
130  if (len < 0) {
131  return -1;
132  }
133  return len;
134 }
135 
136 int sysfs_write_int(char const *const sysfs_file, int value) {
137  string_buffer[0] = 0;
138  int ret = snprintf(string_buffer, SYSFS_STRING_BUFFER_LENGTH, "%d", value);
139  if (ret <= 0 || ret >= SYSFS_STRING_BUFFER_LENGTH) {
140  return -1;
141  }
142  return sysfs_write_string(sysfs_file, string_buffer);
143 }
144 
145 int sysfs_read_int(char const *const sysfs_file, int *const value) {
146  int ret = sysfs_read_string(sysfs_file, string_buffer,
148  if (ret <= 0) {
149  return -1;
150  }
151  if (strlen(string_buffer) <= 0) {
152  return -1;
153  }
154  *value = atoi(string_buffer);
155  return 0;
156 }
int sysfs_write_string(char const *const sysfs_file, char const *const value)
Definition: sysfs.c:106
int sysfs_read_int(char const *const sysfs_file, int *const value)
Definition: sysfs.c:145
int sysfs_read_string(char const *const sysfs_file, char *const value, int length)
Definition: sysfs.c:121
int sysfs_unexport(char const *const sysfs_file, int value)
Definition: sysfs.c:54
#define SYSFS_STRING_BUFFER_LENGTH
String buffer size for dynamic string operations.
Definition: sysfs.c:45
int sysfs_write_int(char const *const sysfs_file, int value)
Definition: sysfs.c:136
int sysfs_unexport_exported(char const *const sysfs_path, char const *const sysfs_unexport_file, int value)
Definition: sysfs.c:91
int sysfs_export_unexported(char const *const sysfs_path, char const *const sysfs_export_file, int value)
Definition: sysfs.c:76
int sysfs_export(char const *const sysfs_file, int value)
Definition: sysfs.c:50
int sysfs_is_exported(char const *const sysfs_path)
Definition: sysfs.c:58