source: src/linux/universal/linux-4.4/samples/mic/mpssd/sysfs.c @ 31633

Last change on this file since 31633 was 31633, checked in by brainslayer, 2 months ago

kernel update

File size: 2.2 KB
Line 
1/*
2 * Intel MIC Platform Software Stack (MPSS)
3 *
4 * Copyright(c) 2013 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
17 *
18 * Intel MIC User Space Tools.
19 */
20
21#include "mpssd.h"
22
23#define PAGE_SIZE 4096
24
25char *
26readsysfs(char *dir, char *entry)
27{
28        char filename[PATH_MAX];
29        char value[PAGE_SIZE];
30        char *string = NULL;
31        int fd;
32        int len;
33
34        if (dir == NULL)
35                snprintf(filename, PATH_MAX, "%s/%s", MICSYSFSDIR, entry);
36        else
37                snprintf(filename, PATH_MAX,
38                         "%s/%s/%s", MICSYSFSDIR, dir, entry);
39
40        fd = open(filename, O_RDONLY);
41        if (fd < 0) {
42                mpsslog("Failed to open sysfs entry '%s': %s\n",
43                        filename, strerror(errno));
44                return NULL;
45        }
46
47        len = read(fd, value, sizeof(value));
48        if (len < 0) {
49                mpsslog("Failed to read sysfs entry '%s': %s\n",
50                        filename, strerror(errno));
51                goto readsys_ret;
52        }
53        if (len == 0)
54                goto readsys_ret;
55
56        value[len - 1] = '\0';
57
58        string = malloc(strlen(value) + 1);
59        if (string)
60                strcpy(string, value);
61
62readsys_ret:
63        close(fd);
64        return string;
65}
66
67int
68setsysfs(char *dir, char *entry, char *value)
69{
70        char filename[PATH_MAX];
71        char *oldvalue;
72        int fd, ret = 0;
73
74        if (dir == NULL)
75                snprintf(filename, PATH_MAX, "%s/%s", MICSYSFSDIR, entry);
76        else
77                snprintf(filename, PATH_MAX, "%s/%s/%s",
78                         MICSYSFSDIR, dir, entry);
79
80        oldvalue = readsysfs(dir, entry);
81
82        fd = open(filename, O_RDWR);
83        if (fd < 0) {
84                ret = errno;
85                mpsslog("Failed to open sysfs entry '%s': %s\n",
86                        filename, strerror(errno));
87                goto done;
88        }
89
90        if (!oldvalue || strcmp(value, oldvalue)) {
91                if (write(fd, value, strlen(value)) < 0) {
92                        ret = errno;
93                        mpsslog("Failed to write new sysfs entry '%s': %s\n",
94                                filename, strerror(errno));
95                }
96        }
97        close(fd);
98done:
99        if (oldvalue)
100                free(oldvalue);
101        return ret;
102}
Note: See TracBrowser for help on using the repository browser.