sysctl

SYSCTL(2)                  Linux Programmer's Manual                 SYSCTL(2)



åå
       sysctl - ã·ã¹ãã ã»ãã©ã¡ã¼ã¿ã¼ãèªã¿æ¸ããã

æ¸å¼
       #include <unistd.h>
       #include <linux/sysctl.h>

       int _sysctl(struct __sysctl_args *args);

       注: ãã®ã·ã¹ãã ã³ã¼ã«ã«ã¯ glibc ã®ã©ããã¼é¢æ°ã¯åå¨ããªããã注æãã®ç¯ãåç§ã

説æ
       ãã®ã·ã¹ãã ã³ã¼ã«ã使ç¨ããªããã¨! ã注æãã®ç¯ãåç§ã

       _sysctl()  ã³ã¼ã«ã¯ã«ã¼ãã«ãã©ã¡ã¼ã¿ã¼ãèªã¿æ¸ããããä¾ãã°ã
       ãã¹ããã¼ã ãåæã«ãªã¼ãã³ã§ãããã¡ã¤ã«ã®æ大æ°ãªã©ã å¼ãæ°ã¯ä»¥ä¸ã®å½¢å¼ã§ããã

           struct __sysctl_args {
               int    *name;    /* integer vector describing variable */
               int     nlen;    /* length of this vector */
               void   *oldval;  /* 0 or address where to store old value */
               size_t *oldlenp; /* available room for old value,
                                   overwritten by actual size of old value */
               void   *newval;  /* 0 or address of new value */
               size_t  newlen;  /* size of new value */
           };

       ãã®ã³ã¼ã«ã¯ /proc/sys ã®ä¸ã®ãã£ã¬ã¯ããªã»ããªã¼ã«ä¼¼ãæ¨æ§é (tree
       structure)ãæ¤ç´¢ããã
       ããã¦ãè¦æ±ãããé ç®ãè¦ã¤ãã£ãå ´åã¯é©åãªã«ã¼ãã³ãå¼ã³åºã㦠å¤ãèªãã ãä¿®æ‐
       £ãããããã

è¿ãå¤
       æåããå ´å㯠_sysctl()  㯠0 ãè¿ãã失æããå ´åã-1 ãè¿ããã errno
       ããã®ã¨ã©ã¼ã示ãå¤ã«è¨å®ãããã

ã¨ã©ã¼
       EFAULT oldval ã« NULL ã§ãªãå¤ãè¨å®ãã¦ã以åã®å¤ãè¦æ±ãã¦ããã®ã«ã oldlenp
              ã«ç©ºãããªãã

       ENOTDIR
              name ãè¦ã¤ãããªãã£ãã

       EACCES ã EPERM
              ããã£ã¬ã¯ããªãã®ã©ããã«æ¤ç´¢è¨±å¯ããªãã£ããã oldval ã 0 ã§ãªãã®ã«èª‐
              ã¿è¾¼ã¿è¨±å¯ããªãã£ããã newval ã 0 ã§ãªãã®ã«æ¸ãè¾¼ã¿è¨±å¯ããªãã£ãã

æºæ
       ãã®ã³ã¼ã«ã¯ Linux ç¹æã§ããã移æ¤ãæå³ããããã°ã©ã ã§ä½¿ç¨ãã¦ã¯ ãããªãã
       sysctl()  ã³ã¼ã«ã¯ Linux ã®ãã¼ã¸ã§ã³ 1.3.57 ããåå¨ãã¦ããã ãã㯠4.4BSD
       ã«ç±æ¥ãã¦ãããLinux 㯠/proc/sys ã«åã(mirror)ããã£ã¦ãããé ç®ã®ååã®ä»ãæ¹ã
       Linux 㨠4.4BSD ã§ã¯ ç°ã£ã¦ãããããã sysctl() é¢æ°ã®å®£è¨ã¯ä¸¡æ¹ã§åãã§ããã

注æ
       glibc ã¯ãã®ã·ã¹ãã ã³ã¼ã«ã«å¯¾ããã©ããã¼é¢æ°ãæä¾ãã¦ããªãã syscall(2)
       ã使ã£ã¦å¼ã³åºããã¨ãã¨ããããã¯ã»ã»ã»ãã®ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºããªããã¨ã
       é·ãéãã®ã·ã¹ãã ã³ã¼ã«ã®ä½¿ç¨ã¯éæ¨å¥¨ã¨ããã¦ããã
       ãå°æ¥ã®ãã¼ã¸ã§ã³ã®ã«ã¼ãã«ã§åé¤ãããããã ãã¨è¨ãããã»ã©ã§ããã ããªãã®ãã‐
       ã°ã©ã ã«ãã®ã·ã¹ãã ã³ã¼ã«ãããã°ãããã«ã§ãåé¤ãããã¨ã 代ããã« /proc/sys
       ã¤ã³ã¿ã¼ãã§ã¼ã¹ã使ç¨ãããã¨ã

       ãã®ã·ã¹ãã ã³ã¼ã«ã¯ãã«ã¼ãã«ã® CONFIG_SYSCTL_SYSCALL
       ãªãã·ã§ã³ãæå¹ã«ãªã£ã¦ããå ´åã®ã¿å©ç¨ã§ããã

ãã°
       ãªãã¸ã§ã¯ãã®ååã¯ãã«ã¼ãã«ã®ãã¼ã¸ã§ã³ãã¨ã«ç°ãªã£ã¦ããã
       ãã®ããããã®ã·ã¹ãã ã»ã³ã¼ã«ã¯ã¢ããªã±ã¼ã·ã§ã³ã«ã¨ã£ã¦
       ç¡ä¾¡å¤ãªãã®ã¨ãªã£ã¦ããã

       å¨ã¦ã®å¯è½ãªé ç®ãæ£ç¢ºã«è¨è¿°ããã¦ããããã§ã¯ãªãã

       ä»ã®ã¨ãã /proc/sys/kernel/ostype
       ã«æ¸ãè¾¼ããã¨ã§ãªãã¼ã¬ã¼ãã£ã³ã°ã»ã·ã¹ãã ãå¤ãããã¨ã¯ã§ããªãã

ä¾
       #define _GNU_SOURCE
       #include <unistd.h>
       #include <sys/syscall.h>
       #include <string.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <linux/sysctl.h>

       int _sysctl(struct __sysctl_args *args );

       #define OSNAMESZ 100

       int
       main(void)
       {
           struct __sysctl_args args;
           char osname[OSNAMESZ];
           size_t osnamelth;
           int name[] = { CTL_KERN, KERN_OSTYPE };

           memset(&args, 0, sizeof(struct __sysctl_args));
           args.name = name;
           args.nlen = sizeof(name)/sizeof(name[0]);
           args.oldval = osname;
           args.oldlenp = &osnamelth;

           osnamelth = sizeof(osname);

           if (syscall(SYS__sysctl, &args) == -1) {
               perror("_sysctl");
               exit(EXIT_FAILURE);
           }
           printf("This machine is running %*s\n", osnamelth, osname);
           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       proc(5)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2012-12-22                         SYSCTL(2)