On Sat, Jul 29, 2017 at 02:03:22PM +0200, Mark Kettenis wrote:
> 
> I don't think we want to add string parsing like this in the kernel.
> Maybe the sysctl(8) frontend should do the mapping from strings to
> numbers?

Ok, I'll try to come up with an alternative diff that does the parsing
in sysctl(8). Let me fetch my rubber gloves...

When the necessary mechanism is there we can also take advantage of it
for setting hw.perfpolicy. That's where I copied the approach from.

> 
> > Index: etc/etc.amd64/sysctl.conf
> > ===================================================================
> > RCS file: /cvs/src/etc/etc.amd64/sysctl.conf,v
> > retrieving revision 1.7
> > diff -u -p -r1.7 sysctl.conf
> > --- etc/etc.amd64/sysctl.conf       2 Mar 2017 10:38:09 -0000       1.7
> > +++ etc/etc.amd64/sysctl.conf       25 Jul 2017 18:40:31 -0000
> > @@ -1,3 +1,3 @@
> >  #machdep.allowaperture=2   # See xf86(4)
> >  #machdep.kbdreset=1                # permit console CTRL-ALT-DEL to do a 
> > nice halt
> > -#machdep.lidaction=0               # 1=suspend, 2=hibernate laptop upon 
> > lid closing
> > +#machdep.lidaction=none            # action upon lid closing: none, 
> > suspend or hibernate
> > Index: etc/etc.i386/sysctl.conf
> > ===================================================================
> > RCS file: /cvs/src/etc/etc.i386/sysctl.conf,v
> > retrieving revision 1.21
> > diff -u -p -r1.21 sysctl.conf
> > --- etc/etc.i386/sysctl.conf        2 Mar 2017 10:38:09 -0000       1.21
> > +++ etc/etc.i386/sysctl.conf        25 Jul 2017 18:40:35 -0000
> > @@ -1,4 +1,4 @@
> >  #machdep.allowaperture=2   # See xf86(4)
> >  #machdep.apmhalt=1         # 1=powerdown hack, try if halt -p doesn't work
> >  #machdep.kbdreset=1                # permit console CTRL-ALT-DEL to do a 
> > nice halt
> > -#machdep.lidaction=0               # 1=suspend, 2=hibernate laptop upon 
> > lid closing
> > +#machdep.lidaction=none            # action upon lid closing: none, 
> > suspend or hibernate
> > Index: etc/etc.loongson/sysctl.conf
> > ===================================================================
> > RCS file: /cvs/src/etc/etc.loongson/sysctl.conf,v
> > retrieving revision 1.4
> > diff -u -p -r1.4 sysctl.conf
> > --- etc/etc.loongson/sysctl.conf    2 Mar 2017 10:38:09 -0000       1.4
> > +++ etc/etc.loongson/sysctl.conf    25 Jul 2017 18:40:40 -0000
> > @@ -1 +1 @@
> > -#machdep.lidaction=0               # 1=suspend, 2=hibernate laptop upon 
> > lid closing
> > +#machdep.lidaction=none            # action upon lid closing: none, 
> > suspend or hibernate
> > Index: sys/arch/amd64/amd64/machdep.c
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/amd64/amd64/machdep.c,v
> > retrieving revision 1.231
> > diff -u -p -r1.231 machdep.c
> > --- sys/arch/amd64/amd64/machdep.c  12 Jul 2017 06:26:32 -0000      1.231
> > +++ sys/arch/amd64/amd64/machdep.c  25 Jul 2017 19:37:22 -0000
> > @@ -264,6 +264,8 @@ void    map_tramps(void);
> >  void       init_x86_64(paddr_t);
> >  void       (*cpuresetfn)(void);
> >  
> > +int        sysctl_cpulidaction(void *, size_t *, void *, size_t);
> > +
> >  #ifdef APERTURE
> >  int allowaperture = 0;
> >  #endif
> > @@ -428,7 +430,7 @@ cpu_sysctl(int *name, u_int namelen, voi
> >     extern int amd64_has_xcrypt;
> >     dev_t consdev;
> >     dev_t dev;
> > -   int val, error;
> > +   int error;
> >  
> >     switch (name[0]) {
> >     case CPU_CONSDEV:
> > @@ -477,15 +479,7 @@ cpu_sysctl(int *name, u_int namelen, voi
> >     case CPU_XCRYPT:
> >             return (sysctl_rdint(oldp, oldlenp, newp, amd64_has_xcrypt));
> >     case CPU_LIDACTION:
> > -           val = lid_action;
> > -           error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
> > -           if (!error) {
> > -                   if (val < 0 || val > 2)
> > -                           error = EINVAL;
> > -                   else
> > -                           lid_action = val;
> > -           }
> > -           return (error);
> > +           return (sysctl_cpulidaction(oldp, oldlenp, newp, newlen));
> >  #if NPCKBC > 0 && NUKBD > 0
> >     case CPU_FORCEUKBD:
> >             if (forceukbd)
> > @@ -500,6 +494,47 @@ cpu_sysctl(int *name, u_int namelen, voi
> >             return (EOPNOTSUPP);
> >     }
> >     /* NOTREACHED */
> > +}
> > +
> > +int
> > +sysctl_cpulidaction(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
> > +{
> > +   char action[10];
> > +   int error;
> > +
> > +   switch (lid_action) {
> > +   case 0:
> > +   default:
> > +           strlcpy(action, "none", sizeof(action));
> > +           break;
> > +   case 1:
> > +           strlcpy(action, "suspend", sizeof(action));
> > +           break;
> > +#ifdef HIBERNATE
> > +   case 2:
> > +           strlcpy(action, "hibernate", sizeof(action));
> > +           break;
> > +#endif
> > +   }
> > +
> > +   error = sysctl_string(oldp, oldlenp, newp, newlen, action, 
> > sizeof(action));
> > +   if (error)
> > +           return error;
> > +
> > +   if (newp != NULL) {
> > +           if (strcmp(action, "none") == 0)
> > +                   lid_action = 0;
> > +           else if (strcmp(action, "suspend") == 0)
> > +                   lid_action = 1;
> > +#ifdef HIBERNATE
> > +           else if (strcmp(action, "hibernate") == 0)
> > +                   lid_action = 2;
> > +#endif
> > +           else
> > +                   return EINVAL;
> > +   }
> > +
> > +   return 0;
> >  }
> >  
> >  /*
> > Index: sys/arch/amd64/include/cpu.h
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/amd64/include/cpu.h,v
> > retrieving revision 1.113
> > diff -u -p -r1.113 cpu.h
> > --- sys/arch/amd64/include/cpu.h    12 Jul 2017 06:26:32 -0000      1.113
> > +++ sys/arch/amd64/include/cpu.h    23 Jul 2017 16:22:37 -0000
> > @@ -446,7 +446,7 @@ void mp_setperf_init(void);
> >     { "apmhalt", CTLTYPE_INT }, \
> >     { "xcrypt", CTLTYPE_INT }, \
> >     { 0, 0 }, \
> > -   { "lidaction", CTLTYPE_INT }, \
> > +   { "lidaction", CTLTYPE_STRING }, \
> >     { "forceukbd", CTLTYPE_INT }, \
> >  }
> >  
> > Index: sys/arch/arm/include/cpu.h
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/arm/include/cpu.h,v
> > retrieving revision 1.47
> > diff -u -p -r1.47 cpu.h
> > --- sys/arch/arm/include/cpu.h      12 Jul 2017 06:26:32 -0000      1.47
> > +++ sys/arch/arm/include/cpu.h      23 Jul 2017 16:22:46 -0000
> > @@ -83,7 +83,7 @@
> >     { 0, 0 }, \
> >     { "maxspeed", CTLTYPE_INT }, \
> >     { 0, 0 }, \
> > -   { "lidaction", CTLTYPE_INT }, \
> > +   { "lidaction", CTLTYPE_STRING }, \
> >  }
> >  
> >  #ifdef _KERNEL
> > Index: sys/arch/i386/i386/machdep.c
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/i386/i386/machdep.c,v
> > retrieving revision 1.604
> > diff -u -p -r1.604 machdep.c
> > --- sys/arch/i386/i386/machdep.c    12 Jul 2017 06:26:32 -0000      1.604
> > +++ sys/arch/i386/i386/machdep.c    25 Jul 2017 19:34:27 -0000
> > @@ -325,6 +325,8 @@ int     pentium_cpuspeed(int *);
> >  void       cpu_check_vmm_cap(struct cpu_info *);
> >  #endif /* NVMM > 0 */
> >  
> > +int        sysctl_cpulidaction(void *, size_t *, void *, size_t);
> > +
> >  static __inline u_char
> >  cyrix_read_reg(u_char reg)
> >  {
> > @@ -3485,7 +3487,7 @@ cpu_sysctl(int *name, u_int namelen, voi
> >      size_t newlen, struct proc *p)
> >  {
> >     dev_t dev;
> > -   int val, error;
> > +   int error;
> >  
> >     switch (name[0]) {
> >     case CPU_CONSDEV:
> > @@ -3551,15 +3553,7 @@ cpu_sysctl(int *name, u_int namelen, voi
> >     case CPU_XCRYPT:
> >             return (sysctl_rdint(oldp, oldlenp, newp, i386_has_xcrypt));
> >     case CPU_LIDACTION:
> > -           val = lid_action;
> > -           error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
> > -           if (!error) {
> > -                   if (val < 0 || val > 2)
> > -                           error = EINVAL;
> > -                   else
> > -                           lid_action = val;
> > -           }
> > -           return (error);
> > +           return (sysctl_cpulidaction(oldp, oldlenp, newp, newlen));
> >  #if NPCKBC > 0 && NUKBD > 0
> >     case CPU_FORCEUKBD:
> >             if (forceukbd)
> > @@ -3574,6 +3568,47 @@ cpu_sysctl(int *name, u_int namelen, voi
> >             return (EOPNOTSUPP);
> >     }
> >     /* NOTREACHED */
> > +}
> > +
> > +int
> > +sysctl_cpulidaction(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
> > +{
> > +   char action[10];
> > +   int error;
> > +
> > +   switch (lid_action) {
> > +   case 0:
> > +   default:
> > +           strlcpy(action, "none", sizeof(action));
> > +           break;
> > +   case 1:
> > +           strlcpy(action, "suspend", sizeof(action));
> > +           break;
> > +#ifdef HIBERNATE
> > +   case 2:
> > +           strlcpy(action, "hibernate", sizeof(action));
> > +           break;
> > +#endif
> > +   }
> > +
> > +   error = sysctl_string(oldp, oldlenp, newp, newlen, action, 
> > sizeof(action));
> > +   if (error)
> > +           return error;
> > +
> > +   if (newp != NULL) {
> > +           if (strcmp(action, "none") == 0)
> > +                   lid_action = 0;
> > +           else if (strcmp(action, "suspend") == 0)
> > +                   lid_action = 1;
> > +#ifdef HIBERNATE
> > +           else if (strcmp(action, "hibernate") == 0)
> > +                   lid_action = 2;
> > +#endif
> > +           else
> > +                   return EINVAL;
> > +   }
> > +
> > +   return 0;
> >  }
> >  
> >  int
> > Index: sys/arch/i386/include/cpu.h
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/i386/include/cpu.h,v
> > retrieving revision 1.156
> > diff -u -p -r1.156 cpu.h
> > --- sys/arch/i386/include/cpu.h     12 Jul 2017 06:26:33 -0000      1.156
> > +++ sys/arch/i386/include/cpu.h     23 Jul 2017 16:22:53 -0000
> > @@ -546,7 +546,7 @@ int     cpu_paenable(void *);
> >     { "sse2", CTLTYPE_INT }, \
> >     { "xcrypt", CTLTYPE_INT }, \
> >     { 0, 0 }, \
> > -   { "lidaction", CTLTYPE_INT }, \
> > +   { "lidaction", CTLTYPE_STRING }, \
> >     { "forceukbd", CTLTYPE_INT }, \
> >  }
> >  
> > Index: sys/arch/loongson/loongson/machdep.c
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/loongson/loongson/machdep.c,v
> > retrieving revision 1.79
> > diff -u -p -r1.79 machdep.c
> > --- sys/arch/loongson/loongson/machdep.c    12 Jul 2017 06:26:33 -0000      
> > 1.79
> > +++ sys/arch/loongson/loongson/machdep.c    25 Jul 2017 19:33:32 -0000
> > @@ -152,6 +152,8 @@ extern  void parsepmonbp(void);
> >  const struct platform *loongson_identify(const char *, int);
> >  vaddr_t    mips_init(uint64_t, uint64_t, uint64_t, uint64_t, char *);
> >  
> > +int        sysctl_cpulidaction(void *, size_t *, void *, size_t);
> > +
> >  extern     void htb_early_setup(void);
> >  
> >  extern     void loongson2e_setup(u_long, u_long);
> > @@ -1021,26 +1023,57 @@ int
> >  cpu_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void 
> > *newp,
> >      size_t newlen, struct proc *p)
> >  {
> > -   int val, error;
> > -
> >     /* All sysctl names at this level are terminal. */
> >     if (namelen != 1)
> >             return ENOTDIR;         /* Overloaded */
> >  
> >     switch (name[0]) {
> >     case CPU_LIDACTION:
> > -           val = lid_action;
> > -           error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
> > -           if (!error) {
> > -                   if (val < 0 || val > 2)
> > -                           error = EINVAL;
> > -                   else
> > -                           lid_action = val;
> > -           }
> > -           return error;
> > +           return (sysctl_cpulidaction(oldp, oldlenp, newp, newlen));
> >     default:
> >             return EOPNOTSUPP;
> >     }
> > +}
> > +
> > +int
> > +sysctl_cpulidaction(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
> > +{
> > +   char action[10];
> > +   int error;
> > +
> > +   switch (lid_action) {
> > +   case 0:
> > +   default:
> > +           strlcpy(action, "none", sizeof(action));
> > +           break;
> > +   case 1:
> > +           strlcpy(action, "suspend", sizeof(action));
> > +           break;
> > +#ifdef HIBERNATE
> > +   case 2:
> > +           strlcpy(action, "hibernate", sizeof(action));
> > +           break;
> > +#endif
> > +   }
> > +
> > +   error = sysctl_string(oldp, oldlenp, newp, newlen, action, 
> > sizeof(action));
> > +   if (error)
> > +           return error;
> > +
> > +   if (newp != NULL) {
> > +           if (strcmp(action, "none") == 0)
> > +                   lid_action = 0;
> > +           else if (strcmp(action, "suspend") == 0)
> > +                   lid_action = 1;
> > +#ifdef HIBERNATE
> > +           else if (strcmp(action, "hibernate") == 0)
> > +                   lid_action = 2;
> > +#endif
> > +           else
> > +                   return EINVAL;
> > +   }
> > +
> > +   return 0;
> >  }
> >  
> >  int        waittime = -1;
> > Index: sys/arch/mips64/include/cpu.h
> > ===================================================================
> > RCS file: /cvs/src/sys/arch/mips64/include/cpu.h,v
> > retrieving revision 1.119
> > diff -u -p -r1.119 cpu.h
> > --- sys/arch/mips64/include/cpu.h   12 Jul 2017 06:26:33 -0000      1.119
> > +++ sys/arch/mips64/include/cpu.h   23 Jul 2017 16:23:02 -0000
> > @@ -373,7 +373,7 @@ void    cp0_calibrate(struct cpu_info *);
> >     { "allowaperture", CTLTYPE_INT },       \
> >     { 0, 0 },                               \
> >     { 0, 0 },                               \
> > -   { "lidaction", CTLTYPE_INT },           \
> > +   { "lidaction", CTLTYPE_STRING },        \
> >  }
> >  
> >  /*
> > Index: sbin/init/init.c
> > ===================================================================
> > RCS file: /cvs/src/sbin/init/init.c,v
> > retrieving revision 1.65
> > diff -u -p -r1.65 init.c
> > --- sbin/init/init.c        16 Jun 2017 06:46:54 -0000      1.65
> > +++ sbin/init/init.c        26 Jul 2017 19:11:02 -0000
> > @@ -1327,10 +1327,10 @@ f_nice_death(void)
> >  
> >  #ifdef CPU_LIDACTION
> >     int mib[] = {CTL_MACHDEP, CPU_LIDACTION};
> > -   int lidaction = 0;
> > +   char lidaction[] = "none";
> >  
> >     if ((death_howto & RB_POWERDOWN) &&
> > -       (sysctl(mib, 2, NULL, NULL, &lidaction,
> > +       (sysctl(mib, 2, NULL, NULL, lidaction,
> >                 sizeof(lidaction)) == -1) && (errno != EOPNOTSUPP))
> >                     warning("cannot disable lid action");
> >  #endif
> > Index: sbin/reboot/reboot.c
> > ===================================================================
> > RCS file: /cvs/src/sbin/reboot/reboot.c,v
> > retrieving revision 1.37
> > diff -u -p -r1.37 reboot.c
> > --- sbin/reboot/reboot.c    16 Jun 2017 06:46:54 -0000      1.37
> > +++ sbin/reboot/reboot.c    26 Jul 2017 19:14:19 -0000
> > @@ -116,9 +116,9 @@ main(int argc, char *argv[])
> >     if (howto & RB_POWERDOWN) {
> >             /* Disable suspending on laptop lid close */
> >             int mib[] = {CTL_MACHDEP, CPU_LIDACTION};
> > -           int lidaction = 0;
> > +           char lidaction[] = "none";
> >  
> > -           if (sysctl(mib, 2, NULL, NULL, &lidaction,
> > +           if (sysctl(mib, 2, NULL, NULL, lidaction,
> >                 sizeof(lidaction)) == -1 && errno != EOPNOTSUPP)
> >                     warn("sysctl");
> >     }
> > 
> > 

Reply via email to