suricata
util-sysfs.c
Go to the documentation of this file.
1 /* Copyright (C) 2011-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Richard McConnell <richard_mcconnell@rapid7.com>
22  *
23  * Sysfs utility file
24  */
25 
26 #include "util-sysfs.h"
27 
28 TmEcode SysFsWriteValue(const char *path, int64_t value)
29 {
30 #if defined(__linux__)
31 #define SYSFS_MAX_FILENAME_LEN (SYSFS_MAX_FILENAME_SIZE + 5)
32  char fname[SYSFS_MAX_FILENAME_LEN] = "/sys/";
33  char sentence[64];
34 
35  if (!path || strlen(path) > SYSFS_MAX_FILENAME_SIZE) {
36  SCLogWarning("File path too long, max allowed: %d", SYSFS_MAX_FILENAME_SIZE);
38  }
39 
40  strlcat(fname, path, sizeof(fname));
41 
42  /* File must be present and process have correct capabilities to open */
43  int fd = open(fname, O_WRONLY);
44  if (fd < 0) {
45  SCLogError("Could not open file: %s", fname);
47  }
48 
49  snprintf(sentence, sizeof(sentence), "%" PRIu64, value);
50  ssize_t len = strlen(sentence);
51 
52  if (write(fd, sentence, len) != len) {
53  SCLogError("Could not write to file: %s", fname);
54  close(fd);
56  }
57  close(fd);
58 #endif /* __LINUX__ */
59 
61 }
len
uint8_t len
Definition: app-layer-dnp3.h:2
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
util-sysfs.h
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
SysFsWriteValue
TmEcode SysFsWriteValue(const char *path, int64_t value)
Definition: util-sysfs.c:28
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
TmEcode
TmEcode
Definition: tm-threads-common.h:83
SYSFS_MAX_FILENAME_SIZE
#define SYSFS_MAX_FILENAME_SIZE
Definition: util-sysfs.h:32
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275