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 #define SYSFS_MAX_FILENAME_LEN (SYSFS_MAX_FILENAME_SIZE + 5)
29 
30 TmEcode SysFsWriteValue(const char *path, int64_t value)
31 {
32 #if defined(__linux__)
33  char fname[SYSFS_MAX_FILENAME_LEN] = "/sys/";
34  char sentence[64];
35 
36  if (!path || strlen(path) > SYSFS_MAX_FILENAME_SIZE) {
37  SCLogWarning("File path too long, max allowed: %d", SYSFS_MAX_FILENAME_SIZE);
39  }
40 
41  strlcat(fname, path, sizeof(fname));
42 
43  /* File must be present and process have correct capabilities to open */
44  int fd = open(fname, O_WRONLY);
45  if (fd < 0) {
46  SCLogError("Could not open file: %s", fname);
48  }
49 
50  snprintf(sentence, sizeof(sentence), "%" PRIu64, value);
51  ssize_t len = strlen(sentence);
52 
53  if (write(fd, sentence, len) != len) {
54  SCLogError("Could not write to file: %s", fname);
55  close(fd);
57  }
58  close(fd);
59 #endif /* __LINUX__ */
60 
62 }
len
uint8_t len
Definition: app-layer-dnp3.h:2
SYSFS_MAX_FILENAME_LEN
#define SYSFS_MAX_FILENAME_LEN
Definition: util-sysfs.c:28
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:30
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