minit

A small yet feature-complete init (http://fefe.de/minit/)
Log | Files | Refs | README | LICENSE

powersave.c (2231B)


      1 #define _GNU_SOURCE
      2 #include <sys/types.h>
      3 #include <string.h>
      4 #include <stdlib.h>
      5 #include <dirent.h>
      6 #include <netinet/in.h>
      7 #include <sys/socket.h>
      8 #include <net/if.h>
      9 #include <sys/ioctl.h>
     10 
     11 #include <libowfat/open.h>
     12 
     13 int write_to(const char* filename,const char* string) {
     14   int fd=open_write(filename);
     15   if (fd!=-1) {
     16     write(fd,string,strlen(string));
     17     fd=close(fd);
     18   }
     19   return fd;
     20 }
     21 
     22 void handle(const char* s) {
     23   size_t l=strlen(s);
     24   char* x=alloca(l+20);
     25   strcpy(x,s);
     26   strcpy(x+l,"/power/control");
     27   write_to(x,"auto");
     28 }
     29 
     30 void dwim(const char* name) {
     31   DIR* D;
     32   struct dirent* d;
     33   if (chdir(name)) return;
     34   if (!(D=opendir("."))) return;
     35   while ((d=readdir(D))) {
     36 //    puts(d->d_name);
     37     handle(d->d_name);
     38   }
     39   closedir(D);
     40 }
     41 
     42 void pci() {
     43   dwim("/sys/bus/pci/devices");
     44 }
     45 
     46 void usb() {
     47   dwim("/sys/bus/usb/devices");
     48 }
     49 
     50 void wakeonlan(const char* interface) {
     51   struct ethtool_wolinfo {
     52    uint32_t cmd,supported,wolopts;
     53    char sopass[6];
     54   } wol;
     55   int fd=socket(PF_INET,SOCK_DGRAM,0);
     56   struct ifreq ifr;
     57   if (strlen(interface) > IFNAMSIZ)
     58     return;
     59   memset(&ifr,0,sizeof(ifr));
     60   strncpy(ifr.ifr_name,interface,sizeof(ifr.ifr_name));
     61   ifr.ifr_data=(void*)&wol;
     62   memset(&wol,0,sizeof(wol));
     63   wol.cmd=6;	// ETHTOOL_SWOL
     64   wol.wolopts=0;
     65   ioctl(fd,SIOCETHTOOL,&ifr);
     66   close(fd);
     67 }
     68 
     69 int main() {
     70   write_to("/sys/class/scsi_host/host0/link_power_management_policy","min_power");
     71   write_to("/sys/class/scsi_host/host1/link_power_management_policy","min_power");
     72   write_to("/sys/class/scsi_host/host2/link_power_management_policy","min_power");
     73   write_to("/sys/class/scsi_host/host3/link_power_management_policy","min_power");
     74   write_to("/sys/class/scsi_host/host4/link_power_management_policy","min_power");
     75   write_to("/sys/class/scsi_host/host5/link_power_management_policy","min_power");
     76   write_to("/sys/class/scsi_host/host6/link_power_management_policy","min_power");
     77   write_to("/sys/module/snd_hda_intel/parameters/power_save","1");
     78   write_to("/sys/bus/usb/devices/1-3/power/control","auto");
     79   write_to("/sys/bus/usb/devices/1-4/power/control","auto");
     80   write_to("/proc/sys/vm/dirty_writeback_centisecs","1500");
     81   pci();
     82   wakeonlan("eth0");
     83 //  usb();
     84   return 0;
     85 }