~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/mic/mpssd/sysfs.c

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Intel MIC Platform Software Stack (MPSS)
  4  *
  5  * Copyright(c) 2013 Intel Corporation.
  6  *
  7  * Intel MIC User Space Tools.
  8  */
  9 
 10 #include "mpssd.h"
 11 
 12 #define PAGE_SIZE 4096
 13 
 14 char *
 15 readsysfs(char *dir, char *entry)
 16 {
 17         char filename[PATH_MAX];
 18         char value[PAGE_SIZE];
 19         char *string = NULL;
 20         int fd;
 21         int len;
 22 
 23         if (dir == NULL)
 24                 snprintf(filename, PATH_MAX, "%s/%s", MICSYSFSDIR, entry);
 25         else
 26                 snprintf(filename, PATH_MAX,
 27                          "%s/%s/%s", MICSYSFSDIR, dir, entry);
 28 
 29         fd = open(filename, O_RDONLY);
 30         if (fd < 0) {
 31                 mpsslog("Failed to open sysfs entry '%s': %s\n",
 32                         filename, strerror(errno));
 33                 return NULL;
 34         }
 35 
 36         len = read(fd, value, sizeof(value));
 37         if (len < 0) {
 38                 mpsslog("Failed to read sysfs entry '%s': %s\n",
 39                         filename, strerror(errno));
 40                 goto readsys_ret;
 41         }
 42         if (len == 0)
 43                 goto readsys_ret;
 44 
 45         value[len - 1] = '\0';
 46 
 47         string = malloc(strlen(value) + 1);
 48         if (string)
 49                 strcpy(string, value);
 50 
 51 readsys_ret:
 52         close(fd);
 53         return string;
 54 }
 55 
 56 int
 57 setsysfs(char *dir, char *entry, char *value)
 58 {
 59         char filename[PATH_MAX];
 60         char *oldvalue;
 61         int fd, ret = 0;
 62 
 63         if (dir == NULL)
 64                 snprintf(filename, PATH_MAX, "%s/%s", MICSYSFSDIR, entry);
 65         else
 66                 snprintf(filename, PATH_MAX, "%s/%s/%s",
 67                          MICSYSFSDIR, dir, entry);
 68 
 69         oldvalue = readsysfs(dir, entry);
 70 
 71         fd = open(filename, O_RDWR);
 72         if (fd < 0) {
 73                 ret = errno;
 74                 mpsslog("Failed to open sysfs entry '%s': %s\n",
 75                         filename, strerror(errno));
 76                 goto done;
 77         }
 78 
 79         if (!oldvalue || strcmp(value, oldvalue)) {
 80                 if (write(fd, value, strlen(value)) < 0) {
 81                         ret = errno;
 82                         mpsslog("Failed to write new sysfs entry '%s': %s\n",
 83                                 filename, strerror(errno));
 84                 }
 85         }
 86         close(fd);
 87 done:
 88         if (oldvalue)
 89                 free(oldvalue);
 90         return ret;
 91 }
 92 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp