From 8d89d05cf2460fad11b8e16dc4cdb503d80548b6 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 9 Jun 2012 16:25:28 +0200 Subject: [PATCH 001/235] Initial commit of enhanced version --- Makefile | 56 ++++++++ README | 21 ++- bin/mbpfan | Bin 0 -> 20950 bytes mbpfan.c | 147 ------------------- mbpfan.rc => mbpfan.rc-BROKEN | 0 src/mbpfan.c | 261 ++++++++++++++++++++++++++++++++++ src/mbpfan.h | 41 ++++++ src/mbpfan.o | Bin 0 -> 19464 bytes 8 files changed, 378 insertions(+), 148 deletions(-) create mode 100644 Makefile create mode 100755 bin/mbpfan delete mode 100644 mbpfan.c rename mbpfan.rc => mbpfan.rc-BROKEN (100%) create mode 100644 src/mbpfan.c create mode 100644 src/mbpfan.h create mode 100644 src/mbpfan.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7a6f096 --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +COMPILER=G++ + +# todo: object files into output path, processing c / c++ files in the same time (?), nested directories for source files (?) +C = c +OUTPUT_PATH = bin/ +SOURCE_PATH = src/ +EXE = bin/mbpfan + +ifeq ($(COMPILER), G++) + ifeq ($(OS),Windows_NT) + OBJ = obj + else + OBJ = o + endif + COPT = -O2 + CCMD = g++ + OBJFLAG = -o + EXEFLAG = -o +# INCLUDES = -I../.includes + INCLUDES = +# LIBS = -lgc + LIBS = -lm +# LIBPATH = -L../gc/.libs + LIBPATH = + CPPFLAGS = $(COPT) -g $(INCLUDES) + LDFLAGS = $(LIBPATH) -g $(LIBS) + DEP = dep +else + OBJ = obj + COPT = /O2 + CCMD = cl + OBJFLAG = /Fo + EXEFLAG = /Fe +# INCLUDES = /I..\\.includes + INCLUDES = +# LIBS = ..\\.libs\\libgc.lib + LIBS = + CPPFLAGS = $(COPT) /DEBUG $(INCLUDES) + LDFLAGS = /DEBUG +endif + +OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) + +%.$(OBJ):%.$(C) + @echo Compiling $(basename $<)... + $(CCMD) -c $(CPPFLAGS) $(CXXFLAGS) $< $(OBJFLAG)$@ + +all: $(OBJS) + @echo Linking... + $(CCMD) $(LDFLAGS) $^ $(LIBS) $(EXEFLAG) $(EXE) + +clean: + rm -rf $(SOURCE_PATH)*.$(OBJ) $(EXE) + +rebuild: clean all +#rebuild is not entirely correct \ No newline at end of file diff --git a/README b/README index a85248e..a8a499e 100644 --- a/README +++ b/README @@ -1,4 +1,23 @@ -This is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. It includes a PKGBUILD for easy installation in Arch Linux. +This is an enhanced version of rvega's Fan-Control-Daemon , +which itself is an enhanced version of Allan McRae mbpfan + +Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. +This enhanced version assumes any number of processors and fans (max. 10). + + * It only uses the temperatures from the processors as input. + * It requires coretemp and applesmc kernel modules to be loaded. + * It requires root use + + +Compile with +gcc -o mbpfan -lm mbpfan.c + +Run with +sudo ./mbpfan + +Original README below. +-- +It includes a PKGBUILD for easy installation in Arch Linux. Based on: diff --git a/bin/mbpfan b/bin/mbpfan new file mode 100755 index 0000000000000000000000000000000000000000..cb5a8c24479d128efefe60681ab0e32b82a2952b GIT binary patch literal 20950 zcmeHvdw5jUx%b*LJ7G(PTp&UqTn3mRXqY723<@Qf$ju0pn<$jcBs0lOnasqwaPz_? zBE~drXg&Sfqo-(XJ@%`$w$We=_c$rg#Wl@)s^$jhbo)ZI+-=w-|-F zBR}^$>#JA&?Clkg?Avc%{^U1bxv%uMH?iGFle8goMB?~vRaJJRHyr?wU2q68{X5l}`!slk;yRz`gEPP28-jaoXH47gF{M1)< zgSjmd=y20)Zi&atWGawInWjKDHL`nV33Kr>P=k?}nNEg+BAH4wwr+!XYa$X$HJOd! zO`<8@8j6t<1u)GfC#x|UPliNGAR3J~il$^^Al3vC$i^B2DXE=|hC;212FsEJ0f!AK zZcCdcL$l&+2}EL|DG>^Zh4qWCs5Phgt1`99Ol`WKGm^7cN8umG6905i?LQ4;$IU4u z3xFt!M22wm9kOVJ5_gN|Ej%?caslnW44l_#%(vV@s63<_gD?PeLJUlU5KcXWNO&P3 z25B)N26+h~^hOgJLU;*b*+)1SbD1zth;l*?)*V7bHiIxl z+toe&5+OR;KI!Z0+jA(D)6))W&&!CR=P5X7d)ipnO+j__&HNB8=qvjKpQQAau_a+v zd*6bEzB0BbOkVFXiL+&4rmXjn#M#0yr)=*-5@$=pUgCF4oGlJdCf+4+wmi(C+`Cia z%s{xBc&o&jiEs_^jS^=@!i$J6mpC&MUQWD5;>=K(Q@6KL;>=WdBXO_9nXxb%)LSTV zW-i=HTu7W53~wcV`qRF?)$Q+Iv*yWT=$Vyme>mNCYGcn}^jdqJZ%(`J>xqH>Or1|B zzx|mGi`s+!EI5#}2!<%s3o``=E?fZ=)a3wEjsagncAc*Vw0cxuN45ReQx3O3+qqaP z`0?TP!-spvY0v)pLrZ3{v$%tw3c)hQh zBx(Kdi9;uD^sO&AQ0H4;*7v>;Lj(px6ZjiWL{UD$C%9+YQ%d2)b8oEoEqbHQx9G%k zulK%jqG0bmi0qByFtUM0HVibfp6b%b21qQhbt$>vVGrHZL89*U^}ZUw1s(9x)tQ;-gUtb5<9c16=GunU04qSz=IVcrE$@7fv zLxiH|FeR-n-6M@$$WCbQhBeuFCy>deg4&tvQe?NOV8KkLtZB+eO>G~Eg0JmF$ER|@ z*>VB$-+L{FR~M6i;Fkq$^+1s6DbIdx_G#71{)|la-~Md&F_Hf}+5cVdmg%3(x~1&M zdhZ@Y-PHBQiIcg^>l-K74d+UK?=C2GRsCf{``efy%U7)kci}rF?8V0y*M!Bby3w-` zYtIK=^1^(=(xmU4y9&({>3R381$@kl)n?bp(fB<4v8(N*Hv4#TZ0FC>38L+gb}vQx ziuaH$#b+N+^c>Bh0j=$6echM38EEI@7f~Me+fJ3-~yP=PghROx#RMPs6J=d)Q zu|8K{v!?0{BC79<`sQ`q=-Y_Vz{QNy+H)Fh05mup0CmFla$nDDANTd0yj+eu*ewUO z#|KjTLcM)G1|J4p4p5Ap@{S!IqObm=udnT>*7oV(f?E#Kn5Fes(2v4qH)c!Wxjcqo zkCD(9BhkNxfjHXN^CcKyM6Uo+pZxYHVi@him@#_BWa+(+7E!NE$?G3l_#?pgaNd^} z+_Dlwvt!VnLj`-rv9?g}odnqV@Fdt4XyC@Sr|Z{kFu&A$`Xj85nf%rH<{^S}=MJg3 zQ+Z~fT%VAMsi^h*0k+PWgzio_U@otNfun9_!JsCyQEp{p8I0Is-KaAol(DiQSrG3T zq+-bE|HH~cfzrujWj}CaU4BcF!{lJ+LiD){j1v1BG4mbyhK8_ME`{Qi>@L z#}s{Bsn(`)t1G%~6F2(GPaaS+r}HTqy=DytcOeG0cTZocru}0^#7^&U)6A3bfd(E; zjq9u(81M{V&-Gxo1C9jjSm)XEdiqG)(}nFHqVK!g&sg)#vcKNvEjVza&$s9G)FQ0G zO~ex1Bu!XLMx^Jj-9(sX53&4TB$#QHiO& zsp>_HBlmKnqlO7R!)5$+48-FzcfvnbuX3(ly8 zea#d@q8MK@g@D81vas#xIo&7zdh)>m%SRW+|H_^S61}VYm*cTqof_Q{^;LF0#{V^X zww%F8Xk&HCGl%WZd74EJ?sm1~SALa9(otG}{>0DhK5gWgrBddq^Bd6$N^NaQkL<=zE>n0IAs zCb{)+&=HUR4d1DNHzKunVLe`tCVUI{U0CRj1J1$3C;|O0ze2Pv!1sbS8s+o{kOMCAjRpChB?WmY zeXE!^>hg=H`6kQ(xd!EN%e+dk&pa*7uXHW>Y%96yeh<0^`V-wH zFVY?b?F{VUADHvk`3#)T!1)ZE&%pT%oX^1d44luv`3#)T!1)ZE&%pnu4B*zekEU(N zI&R10o*AEHJ$j37U~5Xefagqq`?N3q0QQ)9A9!jv_QpsD&U^W{P_U&X^)3YD#aqY^ z@C1YRdOJ~gf6hJK5h&HL%X%ds_fN6!AotA)aeE&>-G&FefQ;od;0wS}{U2Z4Gq;s; z_vprOlu0PFP!^!9L1{wSjB+c=H&O0G`8mo_lvh#yh;jzSwlheq5m$(LqPpbRpoO)y zmw6|zZb-*c>5BSDERcwMXZUCNr%$a)OXAEa3Ku@8tGixxpZgY09ZX{tPu1n!wq?Er zX|&|Dt3lH}$iXq@!MbEVQFnRtgCM*09BkYQUFR0FOUuoLvdhq)0xM?$NI5UBAwwUu zjttHCCm1f*pexZkAg+sI#?t;# zeB^$Wd(pXi4j(NHs_Nz^d-@KeNV^3Vj06y4sK%!iu8G3<9%&=AOC_osls8H&)G*$F z4`ZZ-dW^47c$9?}8bJz|T4=Emr|@VCEit}9?idRlZ5*KPSPS(UA5qwAp+4i=M8{j` zWTS$*6D+jc;Ddd`r~L{hToWs0&l{7pmnAyGxQV)xtrk@q9TYCN&^g9~NQ&Xtz748t zVvTVX(P`o! zbcx2z6xYO6hQTndve30g0nvI3U2lx1@YNQ&(U?K*Qtc|K8#G=cy4;F6Y}`rV720X( zH!3ceiGgYSd~hppkMZ~sfV__>@&v2s#k_n9Jo&7|^t=$`JfeEoz)fet6_R=psJTG$DVs9}9~X=m4t(%Th!$$q>rl^Yz=t+ljUbV?f!Jl% zc*^S}cDWq#6OFu2pry^Bl53&|rfI4`3xn$NGF-RT0ZJiGB!EB@MQbVRWjsS6S%YS} z|BVc{_7rGEd_JZKMUy3ul0_%+;r=(~7CIV{W~I}U(UAnD(@Y%*ei^o08&w!XVXSBz zK3p4RRfe%qg`w+@LVlRmjj;8}t~wy@2tI{BlcuRU<6abP*{e|YQn>*^rcm9thW zXQLuQOOno^DKj<^@$3XayZ%>j=*-wSX3V|M+%HCc3j!)*H!)*dY~?7FIjprUd1%Vn zmB%w_PeAZJ6m9!u5R{3#8{~0J99_u79Reao9>a$ZLPBd}pkp~>$0K1qB<*gc`fovc z6UF;$e0+i;%GN?+6N(yew=sDIh*Vw^weJIJ-+mO5WE_Irh)&&@00B3?7Sy=?Hdz?A zWzU1154JLO7s=m*WZ5y$2ZOE>v7d=}g!CTJ2Z64F{w{;&H9|XqPwhdjR$j1oqr3$9 zu8hYAEsrH8d;h^8#9jOJvI+>^fTH~WghC539?PbKwgpA`f0+K8K%cvu2I+`S>ZZ~~>0MbZ-h)TCn<*4ig z&`Uog`V6%f169#IM~w)|MI~^12v)h*qy5TzG0?rBkE{ef`BGs24s1#-DwCJt<43?I zuS4Z9D!eY%UPNr#OEP`@ZwfQ{74Y7{Kc*uWF&yraXs-nMi9t}hE`weoQTmnKG|`{X zm_+X(njrU8Ht0Q~%}9;*8cmNvZSfaCu%3Pda%hm!N%a#q@s~a0>gKcQWd*tVY)@(L7$4(XlJHH;ju2E z6=`Mp!}8qurTInL3T<_6A-d1y6M1=G018{w)06~Zg^N_OPGcu84?a655 z?X5vVhH5prbX3xB+l44c<{?p-15mZxyMUHjme3TUqa7%8#`L43(X6p!w5nmu_u9N` zWn|-WMAa}F_sR+kjHd`=utg>~30Rv31iM&@s&IY%zOeq~p^t~6rIQ?ufccA@5nxWt zpnuS(u!41^-@4QEC5#SpMoJ^vsSYc$=W#sM?uL1uIUa1LI|*!{7>SYZ60dV~Y9Hqh z!KXZz&heW}G>VFJGEotUHAd6HP(@^V^(=M1dTMhlUC|IpC6xe=UN<*3RzzkE5WwxH ztvkSw5G@U@*xdIwVmjk=Z!i*{<`45;6Q_40vF7OtX$hplkr>W%hiqmr7-~WtA*Z&r zhLRa_B#x?%ef&s+U9~)f=>fCE#sBl%^E9_-jyvVoUGByuy5=6U#IDV9RcN}~UHAu3 zlLr^ib+_(*?6J$iUE>G&Z`TWUmM#WwV#heQMv_%XHoSrpxfc$$dAuCTKu^<6;}`yJ@%apnLlg zmyS_^6!Q6i`z1}==-#?^+t+W|S^3zXT=OoxK4LUp&A{A+h8NJ4Yxr~b8P~$hPhAG{ z`Kz0lJ)+M_>^nM6Me?v&BMhZW78t|sYM zb3g_A+8sbE5{(s^X&a5ln!WOa4Gc$`!%Ec3 zh#7`QVd7x^CL|{qN~VO_m`<3D@o2gwCU6wU;oWSFCxvygU(g}59%~MXXxvWGb+cuN zh&Z`?b}|#Hxj7YRh=$B~Qxh5_OondNWo*HC8f-)W;;b3yf{JjwB~%e?P6W0`;;Bfi zVtFF|SOs{F0*u^9cix{*?PPeyNd} z3NiwY?pR!PA3BdiBa{fF(ip##YzQ=NO1G+sAb&;z$?MY?>4@RFs+n2S!Vx1TM?9!( z$i%69n(8MejazuPPy0#EWXX%ATN=>yW|G|@Gt>}jMjqwQM?!%XR`6YkAmTXNMqQ44 z#9{pyMLuMB8(%;!kYdL>j-bHfS+vPLm$n@{l0?yS2}LL;a5?hL~yfI zT1lY1R_^aNu+k22H6-l-Daw{Q##au|p^~(mfbB+S_uElF2!t^{**T`16C6juK)ip4 z?T3736rI_xor$6X9M#Cr7(X; zsku)Oejf#?v}IAXC2g)tRPO&Fqz*~R6}BYFPSM|kP1?EmXLm%y)Yb%_JIRaaUq4Ht zZ48U3Q_P~_NVaw1c0fa61_<4%E+HR$xkQ@JXXNz+c<=Dpk+_E7QxvEfeSj2oEF|TS ztVBTz2&f1y0fo`q1jTsV11dqOtS%=|mNMg)mZ+zT!N&&eXH7o65^L_Icj+}Vn%@Dp z7sU{*TB{g4t#m)Jlai9yk&u)CTr{Kf%Mgo5iY?=8@AJ^s#205lUsx9~+ zgnlXo*Ab|^rK)ZAqoAo2tRt}4Gi>%K(8i-6Sp*iFd#Cc6g@(?TSNoo37P2`xLV7UgP`e?{K`8TRIX zp_@3OZTZimv#Z}gc^idYZp-uWDZBeH%5y0Hiq3~AT=3+H0f%4VPi||$JsDssp?w$w8_C&>o7xEO z&YXw|ULq8wysEU~ZrvY=Mev#8ZHymxVG*$6!9Xe?{GqVflnAth%y1BzHYWUyDc;Td zg9?1P5lom2v_u-=E)FyJ@JrjuPD2vXjq#S25N?9~!B9iG*$gCj?@B8Yjl`Pbc0JJ0 zkO*zIF+SI_>&TH^<&W7dUCWZ)8UGDf3K>4{!jm-h9ta@A9D4Fu81RWG*g}wU@_h|p zc!ArO{f%1%f-QF&&ez-qIiD>AXv@{1j-SrN>6hnn9R1z$ee<&@a#ByZ|L1>b76fSvH~YED!5E z&vP1bHK;rN0u|d)u#qHncpj({0dX?e_81@WHWXWq=ZC7n*a;ft@RU`ymvUc0VXXAS z^GlCH&X2l%(H^h8D7M^b$ejkG&~7An%n4<`?vUf$qv01avsKAKM10F3$MaGzVDn%U z8P;X{{SI)tVP1M5*8{n`EExh@?mpCQIljAt@9|iF7P*HVa&JNIEy%Ht?aQ|Jq$S5b z<}b_mUJpkzGJb#IkmC<^M}Lmo3l2H{AlC;u+ZR}w`ma%EU(m07j|t?SwP*lRj&b}3 zg<(^U?_M~47JIb+JBJ+4mwf=adeH2PGAunPw!Iqc^wfCKZqV$@j`gn~&_Dh1m&mKU zXV1&~;L~0u3NOlCENkk6JeE%^sHj5R>_xk6#KGg<3+3(WKCckhbD~<8&EwGnyH3`K zay*IW;IS^7SC%E$fk8ipjmnafCk8;X+i^b@oJDprK&L&HIa&5j&qTj5QPxEnUJJA2 z9{qw4-m}D41X)Ko;&=^y<82!O&#n?W)WwGjLBIg=0q~zNhRV6gwZLibLmZdm-oAa= z_H5h<`K}(~>J30bzCTCd$}`tpg)4(xUlpzr#dTESYL()8sc?>TwJs`rki@zEDO>`1 z?or`vg*p$ZaBdH&bxYxDKyZChxQ9TV8YFPzoejLW-VnM4786`tR|wsF7Yr`0AB5_S zC|K7sID18{2ZV0*qiAsz;N z$RY0>r}Uo!zr^8x7x4S{+3^&KuK{=ZzZ3Y~R)5p~Pk`GO?HvSSw};2tehJ*}KMno& zau)wj!0rCi#0bIjA6>A|nK2ahhd2Tr3L3ZFx&Kan5#I2`a~u3$wFZ##ivYiF#W&P7 zMDWatJs!M}=Q%k0qP^|FwCNRU&-APocf+Z@!CZ zI&eOh*bSWeOMu%K<5{1D$FgvqrsOL)0+6LX4{Lg1uff9qPnP`iz}H*xFn|9AobNcJ zPwKx4oa=)f-y2!_e+TZ&zne=S@0kE)`yFd6oBa8}dDenvIUbfc`z0QxkT+O|y2@ zvK40i;+3oLRxH!38-OjTb+CeQvpE`X2t-YJYhnh{Td^P2f?vv~LP7tms+qF|8D=E7 z71*pQyts;YH^FpE3*V(=ybqFIG8=x_ImGQ0?rsu+ zZEAn0?%JjEuU=dW`wotL$(Jw})-Stae!aPD!Ge`nt}<86zoPz1Hc`G`t1+22Nf0`!llr3So0qK0lTbAg#ckcSP!TE?fm{|#<;nR1xq$-z596|C=l}o! literal 0 HcmV?d00001 diff --git a/mbpfan.c b/mbpfan.c deleted file mode 100644 index 7cf3844..0000000 --- a/mbpfan.c +++ /dev/null @@ -1,147 +0,0 @@ -/* - * mbpfan.c - automatically control fan for MacBook Pro - * Copyright (C) 2010 Allan McRae - * Modifications by Rafael Vega - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * 20110811 - v1.1 - * - * Notes: - * Uses only the temperatures from the processors as input. - * Assumes two processors and one fan. - * Requires coretemp and applesmc kernel modules to be loaded. - * - * Tested models: - * MacBook Pro 8.1 13" (Intel i7 - Linux 3.2) - */ - - -#include -#include -#include - -/* lazy min/max... */ -#define min(a,b) a < b ? a : b -#define max(a,b) a > b ? a : b - -/* basic fan speed parameters */ -unsigned short min_fan_speed=2000; -unsigned short max_fan_speed=6200; - -/* temperature thresholds - * low_temp - temperature below which fan speed will be at minimum - * high_temp - fan will increase speed when higher than this temperature - * max_temp - fan will run at full speed above this temperature */ -unsigned short low_temp=63; -unsigned short high_temp=66; -unsigned short max_temp=86; -/* unsigned short low_temp=55; */ -/* unsigned short high_temp=65; */ -/* unsigned short max_temp=80; */ - -/* temperature polling interval */ -unsigned short polling_interval=10; - - -/* Controls the speed of the fan */ -void set_fan_speed(unsigned short speed) -{ - FILE *file; - - file=fopen("/sys/devices/platform/applesmc.768/fan1_min", "w"); - fprintf(file, "%d", speed); - fclose(file); -} - - -/* Takes "manual" control of fan */ -/* void prepare_fan() */ -/* { */ -/* FILE *file; */ -/* */ -/* file=fopen("/sys/devices/platform/applesmc.768/fan1_manual", "w"); */ -/* fprintf(file, "%d", 1); */ -/* fclose(file); */ -/* } */ - - -/* Returns average CPU temp in degrees (ceiling) */ -unsigned short get_temp() -{ - FILE *file; - unsigned short temp; - unsigned int t0, t1; - - file=fopen("/sys/devices/platform/coretemp.0/temp2_input", "r"); - fscanf(file, "%d", &t0); - fclose(file); - - file=fopen("/sys/devices/platform/coretemp.0/temp3_input", "r"); - fscanf(file, "%d", &t1); - fclose(file); - - temp = (unsigned short)(ceil((float)(t0 + t1) / 2000.)); - return temp; -} - - -int main() -{ - unsigned short old_temp, new_temp, fan_speed, steps; - short temp_change; - float step_up, step_down; - - /* prepare_fan(); */ - - /* assume running on boot so set fan speed to minimum */ - new_temp = get_temp(); - fan_speed = 2000; - set_fan_speed(fan_speed); - sleep(polling_interval); - - step_up = (float)(max_fan_speed - min_fan_speed) / - (float)((max_temp - high_temp) * (max_temp - high_temp + 1) / 2); - - step_down = (float)(max_fan_speed - min_fan_speed) / - (float)((max_temp - low_temp) * (max_temp - low_temp + 1) / 2); - - while(1) - { - old_temp = new_temp; - new_temp = get_temp(); - - if(new_temp >= max_temp && fan_speed != max_fan_speed) { - fan_speed = max_fan_speed; - } - - if(new_temp <= low_temp && fan_speed != min_fan_speed) { - fan_speed = min_fan_speed; - } - - temp_change = new_temp - old_temp; - - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { - steps = (new_temp - high_temp) * (new_temp - high_temp + 1) / 2; - fan_speed = max(fan_speed, ceil(min_fan_speed + steps * step_up)); - } - - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { - steps = (max_temp - new_temp) * (max_temp - new_temp + 1) / 2; - fan_speed = min(fan_speed, floor(max_fan_speed - steps * step_down)); - } - - set_fan_speed(fan_speed); - sleep(polling_interval); - } - - return 0; -} diff --git a/mbpfan.rc b/mbpfan.rc-BROKEN similarity index 100% rename from mbpfan.rc rename to mbpfan.rc-BROKEN diff --git a/src/mbpfan.c b/src/mbpfan.c new file mode 100644 index 0000000..5ca8931 --- /dev/null +++ b/src/mbpfan.c @@ -0,0 +1,261 @@ +/** + * mbpfan.c - automatically control fan for MacBook Pro + * Copyright (C) 2010 Allan McRae + * Modifications by Rafael Vega + * Modifications (2012) by Daniel Graziotin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 2012-06-09 - v1.2 + * + * Notes: + * Assumes any number of processors and fans (max. 10) + * It Uses only the temperatures from the processors as input. + * Requires coretemp and applesmc kernel modules to be loaded. + * Requires root use + * + * Tested models: + * MacBook Pro 8.1 13" (Intel i7 - Linux 3.2) + * Macbook Pro 6,2 15" (Intel i7 - Linux 3.2) + */ + + +#include +#include +#include +#include +#include +#include "mbpfan.h" + +/* lazy min/max... */ +#define min(a,b) a < b ? a : b +#define max(a,b) a > b ? a : b + +unsigned short min_fan_speed = 2000; +unsigned short max_fan_speed = 6200; + +/* temperature thresholds + * low_temp - temperature below which fan speed will be at minimum + * high_temp - fan will increase speed when higher than this temperature + * max_temp - fan will run at full speed above this temperature */ +unsigned short low_temp = 63; // try ranges 55-63 +unsigned short high_temp = 66; // try ranges 58-66 +unsigned short max_temp = 86; // do not set it > 90 + +unsigned short polling_interval = 7; + + +struct s_sensors { + char* path; + char* fan_path; + unsigned int temperature; + struct s_sensors *next; +}; + +void find_fans(t_sensors* sensors) +{ + t_sensors *tmp = sensors; + + char *path = NULL; + const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_end = "_min"; + int path_size = strlen(path_begin) + strlen(path_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int counter = 0; + + for(counter = 0; counter<10; counter++) { + path = (char*) malloc(sizeof( char ) * path_size); + path[0] = '\0'; + sprintf(number,"%d",counter); + + strncat( path, path_begin, strlen(path_begin) ); + strncat( path, number, strlen(number) ); + strncat( path, path_end, strlen(path_begin) ); + + FILE *file = fopen(path, "r"); + + if(file != NULL) { + if (tmp->path != NULL) + tmp->fan_path = (char *) malloc(sizeof( char ) * path_size); + strcpy(tmp->fan_path, path); + tmp = tmp->next; + fclose(file); + } + } + + free(path); + path = NULL; +} + + +t_sensors *find_sensors() +{ + + t_sensors *sensors_head = NULL; + t_sensors *s = NULL; + + char *path = NULL; + const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; + const char *path_end = "_input"; + + int path_size = strlen(path_begin) + strlen(path_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int i = 0; + for(i = 0; i<10; i++) { + path = (char*) malloc(sizeof( char ) * path_size); + + sprintf(number,"%d",i); + path[0] = '\0'; + strncat( path, path_begin, strlen(path_begin) ); + strncat( path, number, strlen(number) ); + strncat( path, path_end, strlen(path_begin) ); + + FILE *file = fopen(path, "r"); + + if(file != NULL) { + s = (t_sensors *) malloc( sizeof( t_sensors ) ); + s->path = (char *) malloc(sizeof( char ) * path_size); + strcpy(s->path, path); + int result = fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) { + sensors_head = s; + sensors_head->next = NULL; + } else { + t_sensors *tmp = sensors_head; + while (tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = s; + tmp->next->next = NULL; + } + fclose(file); + } + free(path); + path = NULL; + } + if(sensors_head != NULL) + find_fans(sensors_head); + return sensors_head; +} + +t_sensors *refresh_sensors(t_sensors *sensors) +{ + + t_sensors *tmp = sensors; + + while(tmp != NULL) { + FILE *file = fopen(tmp->path, "r"); + + if(file != NULL) { + int result = fscanf(file, "%d", &tmp->temperature); + fclose(file); + } + + tmp = tmp->next; + } + return sensors; +} + + + +/* Controls the speed of the fan */ +void set_fan_speed(t_sensors* sensors, unsigned short speed) +{ + + t_sensors *tmp = sensors; + + while(tmp != NULL) { + FILE *file = fopen(tmp->path, "rw"); + + if(file != NULL) { + fprintf(file, "%d", speed); + fclose(file); + } + + tmp = tmp->next; + } + +} + + + +/* Returns average CPU temp in degrees (ceiling) */ +unsigned short get_temp(t_sensors* sensors) +{ + sensors = refresh_sensors(sensors); + int sum_temp = 0; + unsigned short temp = 0; + + t_sensors* tmp = sensors; + while(tmp != NULL) { + sum_temp += tmp->temperature; + tmp = tmp->next; + } + temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); + return temp; +} + + +int main() +{ + unsigned short old_temp, new_temp, fan_speed, steps; + short temp_change; + float step_up, step_down; + + t_sensors* sensors = find_sensors(); + + new_temp = get_temp(sensors); + fan_speed = 2000; + set_fan_speed(sensors, fan_speed); + sleep(polling_interval); + + step_up = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); + + step_down = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); + + while(1) { + old_temp = new_temp; + new_temp = get_temp(sensors); + + if(new_temp >= max_temp && fan_speed != max_fan_speed) { + fan_speed = max_fan_speed; + } + + if(new_temp <= low_temp && fan_speed != min_fan_speed) { + fan_speed = min_fan_speed; + } + + temp_change = new_temp - old_temp; + + if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { + steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; + fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); + } + + if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { + steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; + fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); + } + + set_fan_speed(sensors, fan_speed); + sleep(polling_interval); + printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + + } + + return 0; +} diff --git a/src/mbpfan.h b/src/mbpfan.h new file mode 100644 index 0000000..33a4a9b --- /dev/null +++ b/src/mbpfan.h @@ -0,0 +1,41 @@ +/** Basic fan speed parameters + */ +extern unsigned short min_fan_speed; +extern unsigned short max_fan_speed; + +/** Temperature Thresholds + * low_temp - temperature below which fan speed will be at minimum + * high_temp - fan will increase speed when higher than this temperature + * max_temp - fan will run at full speed above this temperature */ +extern unsigned short low_temp; +extern unsigned short high_temp; +extern unsigned short max_temp; + + +/** Temperature polling interval + * Default value was 10 (seconds) + */ +extern unsigned short polling_interval; + +/** Represents a Temperature sensor + */ +struct s_sensors; +typedef struct s_sensors t_sensors; + +/** + * Detect the sensors in /sys/devices/platform/coretemp.0/temp + * Return a linked list of t_sensors (first temperature detected) + */ +t_sensors *find_sensors(); + +/** + * Detect the fans in /sys/devices/platform/applesmc.768/ + * Associate each fan to a sensor + */ +void find_fans(t_sensors *sensors); + +/** + * Given a linked list of t_sensors, refresh their detected + * temperature + */ +t_sensors *refresh_sensors(t_sensors *sensors); \ No newline at end of file diff --git a/src/mbpfan.o b/src/mbpfan.o new file mode 100644 index 0000000000000000000000000000000000000000..da206eea55e27fee0d041aeba2ca2e53fd4f031a GIT binary patch literal 19464 zcma)@3w#vS^~dk*ZjzBKNg&882n%d@h9p41@RA^JjF5sn6v~pvZW2j$!|sNcSS$e{ z#whe}Ti>AdZ*8m8wpyxa1;WePzoKX*MG#+Y5pC^X>$~#*{qCH}PNLZVd_J?^d(J)g z+` z4gM8Jruj3N1A2uf9YXeyjXD0meR=-;`EwJtH^&F5BOZSav|80T)6=-j?`iZ{7EROq zIZYeAw%BmkX!xpA`sOF;%u(tve6}>k{TctMM&z5Ui!A|EV;`#?yG1O|{Kguzp^v{2 z=DJzVa&AXC`Y|2ymVGFalfHQYde@Y)WncQ1eoW2uoE-!dp`{rW*s(n9W)w`8WG{w*b~K!3$n(-h zqM@^3-WC|($0?_`4qc>%`;8LpUzOMZPGMd<iUAzdBI|CEjx8;1isPPNCdrfZJ4Tk-2ykGOw_6;tOxg~5`>fLg({y@XQ zjK)tf_AQMcH$OGl;eVOmm%e9%-@oN#d{+9N^P%mmrwa@^)Z=g5;P)br5!OSe4=I@z zjqNIX8kMmsnRrrP!+R6a@1jKG<10tLgn8sHF`J4Q(2pCh@@JgHL_9Hub+jTP>3a~V z?DHQg)mfq95Jo1qwFdi__|1Vu_fyOKJx@*Z_oQX_$Go)YhP-Ykm-#cm?x-@O;o!uU z*1wxhPOXxcyEPuWKHu=v_r;`VSeKg^r}sp zz9)&{vW?RBTuf24Lyw7!xD<&FAueisap?ZWqs_C8^k438+`s>9KjY|&pLVc0@!8M= zjmMhj!{$Kaf&K6HGmcp{t;_s{baMJLqzcYnj+uaQY^(UW(75C2H@<>KgvPa`H~dkz z=B37?Lyt9=wDXm|hvzM4vrZKI3#roH_qXoD2ruKvE^9vrmSbd>>p6hjk8uXp1zMlQ zWSE7?Fst?1lV?x0rf`UX9cHb_RdX zbLkDm5D@91NB=+3FKCPY8()t;PKZ3d{!{c{Kf7l1*P||l9Z^5K1+5NkKh=6Jnb+e} ztsI7L3;)^gqX6Rd`Lx|$(2zIi_cv#XI0 zxCScI4KHR7<5DepOq~Ce*;7Q*TmmO%FA=@wIXdVqVupDejk84TX1+sLeMIbOvTE}Z z5&M|kncG)HpBblTpNM`lonn6xhnRC%c7TXE=DQUA#`CD&H85Xa7tBG%%UUcjKcwd& z(xXCiFO73VoM`?Xkuh_PJD_z9EHaxYjxs8=IL&;W;%FnP#Th1Dngzz6wda{;n7M_< zyIP!OUPsSUj9+NuV)HbO=NP<@x&~fl*032@h`7XzQ7jg5nOREXD@9yk-p<^)#ud73 zg*l(yFOfD^nV-{mzVU&sw?8L^Qi+sYJd)&W)h>AdJ7GHe^g7J)c=ZDr}eQaq5Lz@gCjkZaJ__?SD2eMO7Br}Y{ zrAVh{qjF=MHG@>@0jP}eGM`f2%$uO6{6I7HaY_?e$TiRl*L0O`JjQ1G*j%^K1WiUD z5QBm)x;#Z&AKTLvmWNQ@{X=HBjc1_iay@!#DY`tQ^Jv*6q?3#-$^@ZtS6XHM)>cL< ztTL4>1H+%gmurR9jIQv|WhZ`ID|AvfV};cWkLP!=&ol<0m}iJ<8U%MWOuGdFYi-5F z-7kk4xkG{OyxBmSuDicu=_{D7K`H2&)(-LO>25jXh+7UVg*fc)aM<19u)B6*IkeL3 z(Ps9j(&|ww99q%T{q3rH7(3AcWAYIgaVrvfl9lS&0EID`N!Je5i`l*7SY{YgsA`6o zr3ZZ~!}>7xKDWWWF^vJ@bWv(&pGEXO-O^CC^OC{T%wThJ`fQ^8Onsc~2xMJ>8DPv| z&imN=mco!lhF~{l-Hpp!zwbd7&psYmJ=oe_v+$F3j8+RdOqW90pM|$V)k5{WKcTRX zh0BnqOWwqi131r~L*ffs&G-EV;^VY<2#KRi+=Il2G{xreq(T~Kuu>zq5shUSiXicP zaS8Tjtd)ALfN54KZK{~R2m0PIWmHpm6M_}9m5f;r__JbGV?~Hvm6+998ZOi%W)Vha zgcY+oiCOPzWyGjo0#`a>Q&IB`P#CM8M}^La^<%``I|4gZuiIf@MeKS;Y>i#`W)$YM zHrAw~E8kqWKZAA(hF#DZWBqs->cH)Sx*r3_Dsi-puW@0{OFM#gH*sb~>WBnoM2k`V1 z$9^+*V~;Nh`pulbpF`$Rz-^#0f{kgZLQSmaQRIz+;giT8b&*no8lW^wV2`&mYxmKYTdIeQH_0YPk1{6uRU4M^FFRPwnE=4AM%h(A>9dS=u{+z z%)`$kkcKQpVm}hxh8bf~+Bj~9j}d(Zd2eAx_nO7#a4BlMQUO~7lOU^!2`vt%h+V{b ziWzjKbMK@0YnJ`fI@tV^;&aS>m9BeZG_s1@#BmfG=&F@HTLQ5!d)5`@SJ21Dols~v zd)I3%WZknhu+lxtVb9$0*HjLB=8m6+qy8*@HB7J5#b?{avu)i_2FF?av|ao)6z|Jx z#6}d|&f;HeWAW>t>h%!hA={9;iM8`YnzIW(tTg9gonY}1ydt~;(|s(ij0aKP_ebdN zK`Awu&bMPqoY2lK#~;{^AMfB8-Lf3BTbQaI&sdn|_Jfao%^fVnR#%WHV6rvlyZ&9Ub|LbYJrzV8A&(~ z*$wFep{-S2xnjy}D}!eEbamaN1QbeLoQjZjFKBBi&OL1rEekc<-uOz%XT@PpL zaLj0Hw04BUiyryhMBS6U6Y&%+Ps^@jE+mx}A>x0u{anAjeth4061c_L38&k7HkE6u9GDMD~Fbtto`qR(%QxWmjC>nyrZ=oEE)W-$#I-Y7Ln@by8?r3MIg*0KcNPc7O&msR!?1n1&hSJRa zw$gUn5hR*^OT5_3DY`px56Op67+;g*nhR+!Im1e4kouM#bAaSMnw}g?v|#7tAP-vU z4Dyhb_L7IKv`HSZ(jJnFP3_3;E?dSsAeYMD$t9CzyrbAQcFY>5=|q?oVMpMI|I*3D z7bv9fir}BzjU9!Edv&1&gjH+)&V+*nb_45SX*ZCj?5SgZWkVdLRV4)bi@{F#S!ny= zqLWuMR8D+~9TV}Pjm~^V6oZ*4-PVeE$gw(kq1;8fY}+)lm!O84Z`W`J7H?q3*m+2< z=Q#<;x2&|6{EL;&AoZgk759+^*kPYRj!Q?J0?IrKC(k97; zu(QE)AoI45XD*Uu9Ssgc^;|mY?P>hJ1tXj> zh_A5I^gO8V+7p+b99VK?j~);#h(ZL7G1^2o%vNZLES)SNEKhCom!4S8qdFZ+*~!#( z+QXus04^Qlw%Pm^a?fHnRh?0%`i{!lN$H%{L^npYhELcku_tUORcnoHg93VEZGXUS zsPBx0!5r*J@;m)F!+1oNs#7mgiK zQys2fJEAgNpI25Lk68va)n$2=<>h(RW7`=NCD}RDZhLN0`yk^pBcW{&Y zl8Nr}O>W=zhNs+-C*9r5O`*A!<^VV_E4R4UPIRZvF$aZio4Zi!(2$39%U)4t+|DkU z^vsS)<2@etX_x6kB@bTj8el;Dvx|Lr@cK>WZuj~*xG-T(NC=M`#tQe^CF_2$`G)+x ze|1g1cvZDoekB{`&M}jW znz)JuV!?1M5{;=)O(YUk6_NU~nxG2Io)?%gyLh^ax8(-{t4nL@t*lt->R=!ku2A}a z{8&{a8u#%J#8~zEU?8q$&Ryu6I()bfM@{Z%6^MjFu^=*~qHu6cK$l}BYogWhV4yCJ zw+uoxrIj%itFN_kYfIOPna&uc+f-H`3Y3+`f+|#vRz`y%bfe1A#PT(GX`wW_GKdb* zeKc5Fp&J?qR9A-a{sCGM4C-># za8-3>m1Qcu3sj*-6{v}nuT*VTfj~tt7FTsRBg8{0P+lJmlt*gnYr_gJSU6e}sEc46 z#EcE#sD&$o@MMSVnsK_x=s~PxTw?G7tK+2@H#^h;c16OjTVW3?m17GqM0r(_+F)Kq zWwdmCbtGOL&MS#Vt__yQV|g=5!y~3f!trRNX2i78U~MF<>ccTaFIeG2bE2v|QXh^7 zqlyERh-4s8S6We^0@oDU(QDVfYetT8#(FiTO2T6zrfoIp*(8x@r{t(wBdh{?;$ccC zUHM35&FsXq(laGcAFhuDE3EmW=L2V3EFP@0rU<7Lr*e6)x<=LEL>=HPR=TUTJZ9=5 zH8^uu;w>2T8pnVx8>on^K^DzfI1mo5#YBlm%j?!ztwDW^2&VrvBS*KJbdI>hEu%4* z&X|WBRAn7Z?YkTZ)yDOq%lfS9>+7uP)!wk}fYNQ^F%piT3Jf}ioD(V-EsfXXC}51r zO3PQ)*I9=XuUuAlV7{sz2MwBb&B!qwjxa~lahydeEJr%1R;&Z45gy9d@Q*iEqR$jM zFI-<+hFPP}DuEbBO-HXRSc#hP=V=^nYMH>b8qJB|jUydYdg0-O7QrT5jssH7oH}*9 zZ^*(jOvL)U;_7f|G-91>MvSc2$}vOb@v+Y>rGx^BjUX%P+&uvbTL( z`E^F8uREBM${Ufpv7f;OT|Xy-@2#x=M%Lee`hBh|(lVx|c~P^8nmtGKJERFI!$;#p0viQ1&>o40QTBmZ$ zBek_boW*m)k$5l{SFI6f6i(dQORy>sik8*}8G`@s)(?gZCm*$PE5dFsRVioi2U`|) z-p1Hl5pKS=qagJx&s98cbF)Rp(CLkjS-g@a?A88oq-k&4-C$?X2%9Z(&e8uUDzVYd zK0YWjkNaeyZO8w2)9;F(ZiP_VZKupM_f^w<^_^5_x=5?5ua&Sf$p&mi6)$h`w0w_1K;M9?8VI z9LJ@-*?G5iuJFd>_rcPC`0 zzkRdtX9{%mmyX@`_ba6BGDxZ}n2^42=*J_-bNV|0e|%U+f4nWU{k0(N^f$6U{(-S4 znIrERq@DhbUSZ93`a>w&{JkJ$I1jna^(*y1tV}l6#r@Ah+b(M;{&y4`!nS)$(stGi z*f}@aar*>2({?Ck!LG>8gx+q#he$j7aRU8d8`uwh9tAnvjFBS@YTGdQclfiD&N@*< zb%V2GXaui2c3d)$W}QxdZ{xsQYCA!mBMB+7IrSd(&+~~K`g7(|cGf`I?zL6Qtskx= zJhFZUD?^bwEK6~(2;;Ba zh*-tz)>)RZ^3pKwC;r3I_dhJ@>4B zTXnCKy_Nb0^I`vq38m4K~)3N5ZduCmCEo8Q0M=vAdRXKc#LZ(ce2soPhbJn+qr5k3jnW ziZc9V;@F^^1+v^g66KbOK13OIYen8hc>vaLB<%K*KBf4Un1O?ZuU@l0u4!Bt6f})* zzp4VVzxA)Sk&R_JmLP{!XEDeNcePh-E~rw0lDeiHRDp7gg~WE52fP6Awa8dz-bEKMTRn*B+f z+EJx4@Tv>{Az^-zaHgFDcHx6UK4ND%z7sSgyCQP&iFkB3ve1DQpz}P z3BP@X14y)EIEnV|YT1WfJwDCSm_q687gL|4S0~87Y>&n{Xfr`!T`^B>WeV@HdZyp9P{XBVoT%xJvT* zJ}2!qi~d#;<$ox=NAmfiC++^5grD7#|CH$W315)>mqk7yJVnCqUnT!N(SIQPm*gkm znnJr&VLFL=Gle}!)bEr0Tp{m~X;&niN5bwZk@>PI^S?{Nf2HKti9RmeDEe)}ACR!W zUAT*cp9h2wk*McalD}8veI&-?kmz3!{mUYs7WsYQ$0YoIPNG~AZkp(~6A3v}2nM8Zu7QQd}>9__nKT|kRI9NDWIG;rMGT}-RacY+Q9iqQa^uG}OFGass zcu?~HLLx5bgr5qN;FJ2Uh%X?-ZxN>rJyF3R+@exs@8J8kn1ekPZSmjXA7?sE)?qZ3G7Qn4hn09 ztAy)>`q>fen?>FsyhC`8P_Iv5_o&Ff6Fwzu5grjfFMLVZDm*28U-(bqXTpCA`OKI7 zHibQeS;7IrK|;O`N_*bJkyC{;g}is8e!j3w7!uZ!I1fif*6SnK^Hn z@DAZF;RC|Qg--~N3jZiPE__vZTKJ}r-@&lHPlS9uh_VYid6AI!6_j~DLG~986^;;& z5$g3A(o;m9F61j4EdL$h)xz%z^?D6y-jC2ODqJo6o^X@!CgCkYzFxv|yni9@7wYvL z(!8Icp0BQuEkeFtLYc2=kjI5@3jZSfyYPR7p9%jh^l;sVG+)Ic_4*OyeG+B8js*E^ zfwF$J4;&@(IN>DWbm43vKM7(veV+wZh+HLHBfL(yNqD1BuS-#Ghse8x4+wuD{H1W8 z@Su<{$+7+yg#5yX@|(i5!uN&z@Q3+4L6Q94hs+cX5DpUZwH@a3{{V8l@G{{H;T++7 z;UeL3VVUq+VOY3YxL&wPc%$$~!aIfc3m+8f&m7UNCq&*SJSgPHLu~h}!q2)^rcvW7@dc6(#Qjup0^?Don1tRNpH)MXC z#QLrkt`gP@`H>Ryw+U}0vGbcH>URl$D*T0z-z+iz_rk-%KM0QtUlrQx@wY_g2Tm;i zsgMuIDD#6KGF6x^Y_~2){xH$!3dab?3Hfyt{ah*JCl-{i7V>i`%6fec#zkH$v^Pmirh!Y&#h>m zCmbu}mwnVv5l$Bt3rmEHg-eBc-H&ugWWC-;dY#A(!mYwvgg+48C)DeHq<wAnPzt^Jv2_aO0xs=LkEl^9n_8ulHt&Tr4aRUL{;1tPoZSYlL;exbOzyX5lvB&BEJ- zcMEq39}qqyd|23V9k@^Q2ZhIkFAGlyUlaaK_@3|s;YUI|_P63KwAX`Pk$VdD^Dm5x zPh>vp=Q!mH3xxWk6X^L|llikqjBBy5M0k~Og|I?cC9Dyy6>bpTAlxk6F4WK0P|xim z-z|Jd_^|L%;bX#m!h^!2!aoX62wxMP5xy<_K=_gHQ{k6Fes9G%=;wD}24%Ffo6sJg Z;UfDmE?k4I5bhFke5p6#m-LhH^MBqf52OG9 literal 0 HcmV?d00001 From ed4f18680306161c75a0e55ff882cc83003099cf Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 9 Jun 2012 16:27:45 +0200 Subject: [PATCH 002/235] Added .gitignore --- .gitignore | 17 +++++++++++++++++ bin/mbpfan | Bin 20950 -> 0 bytes src/mbpfan.o | Bin 19464 -> 0 bytes 3 files changed, 17 insertions(+) create mode 100644 .gitignore delete mode 100755 bin/mbpfan delete mode 100644 src/mbpfan.o diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..afe16db --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Intellij +.idea +*.iml +# Object files +*.o + +# Libraries +*.lib + +# Shared objects (inc. Windows DLLs) +*.dll +*.so + +# Executables +*.exe +*.out + diff --git a/bin/mbpfan b/bin/mbpfan deleted file mode 100755 index cb5a8c24479d128efefe60681ab0e32b82a2952b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20950 zcmeHvdw5jUx%b*LJ7G(PTp&UqTn3mRXqY723<@Qf$ju0pn<$jcBs0lOnasqwaPz_? zBE~drXg&Sfqo-(XJ@%`$w$We=_c$rg#Wl@)s^$jhbo)ZI+-=w-|-F zBR}^$>#JA&?Clkg?Avc%{^U1bxv%uMH?iGFle8goMB?~vRaJJRHyr?wU2q68{X5l}`!slk;yRz`gEPP28-jaoXH47gF{M1)< zgSjmd=y20)Zi&atWGawInWjKDHL`nV33Kr>P=k?}nNEg+BAH4wwr+!XYa$X$HJOd! zO`<8@8j6t<1u)GfC#x|UPliNGAR3J~il$^^Al3vC$i^B2DXE=|hC;212FsEJ0f!AK zZcCdcL$l&+2}EL|DG>^Zh4qWCs5Phgt1`99Ol`WKGm^7cN8umG6905i?LQ4;$IU4u z3xFt!M22wm9kOVJ5_gN|Ej%?caslnW44l_#%(vV@s63<_gD?PeLJUlU5KcXWNO&P3 z25B)N26+h~^hOgJLU;*b*+)1SbD1zth;l*?)*V7bHiIxl z+toe&5+OR;KI!Z0+jA(D)6))W&&!CR=P5X7d)ipnO+j__&HNB8=qvjKpQQAau_a+v zd*6bEzB0BbOkVFXiL+&4rmXjn#M#0yr)=*-5@$=pUgCF4oGlJdCf+4+wmi(C+`Cia z%s{xBc&o&jiEs_^jS^=@!i$J6mpC&MUQWD5;>=K(Q@6KL;>=WdBXO_9nXxb%)LSTV zW-i=HTu7W53~wcV`qRF?)$Q+Iv*yWT=$Vyme>mNCYGcn}^jdqJZ%(`J>xqH>Or1|B zzx|mGi`s+!EI5#}2!<%s3o``=E?fZ=)a3wEjsagncAc*Vw0cxuN45ReQx3O3+qqaP z`0?TP!-spvY0v)pLrZ3{v$%tw3c)hQh zBx(Kdi9;uD^sO&AQ0H4;*7v>;Lj(px6ZjiWL{UD$C%9+YQ%d2)b8oEoEqbHQx9G%k zulK%jqG0bmi0qByFtUM0HVibfp6b%b21qQhbt$>vVGrHZL89*U^}ZUw1s(9x)tQ;-gUtb5<9c16=GunU04qSz=IVcrE$@7fv zLxiH|FeR-n-6M@$$WCbQhBeuFCy>deg4&tvQe?NOV8KkLtZB+eO>G~Eg0JmF$ER|@ z*>VB$-+L{FR~M6i;Fkq$^+1s6DbIdx_G#71{)|la-~Md&F_Hf}+5cVdmg%3(x~1&M zdhZ@Y-PHBQiIcg^>l-K74d+UK?=C2GRsCf{``efy%U7)kci}rF?8V0y*M!Bby3w-` zYtIK=^1^(=(xmU4y9&({>3R381$@kl)n?bp(fB<4v8(N*Hv4#TZ0FC>38L+gb}vQx ziuaH$#b+N+^c>Bh0j=$6echM38EEI@7f~Me+fJ3-~yP=PghROx#RMPs6J=d)Q zu|8K{v!?0{BC79<`sQ`q=-Y_Vz{QNy+H)Fh05mup0CmFla$nDDANTd0yj+eu*ewUO z#|KjTLcM)G1|J4p4p5Ap@{S!IqObm=udnT>*7oV(f?E#Kn5Fes(2v4qH)c!Wxjcqo zkCD(9BhkNxfjHXN^CcKyM6Uo+pZxYHVi@him@#_BWa+(+7E!NE$?G3l_#?pgaNd^} z+_Dlwvt!VnLj`-rv9?g}odnqV@Fdt4XyC@Sr|Z{kFu&A$`Xj85nf%rH<{^S}=MJg3 zQ+Z~fT%VAMsi^h*0k+PWgzio_U@otNfun9_!JsCyQEp{p8I0Is-KaAol(DiQSrG3T zq+-bE|HH~cfzrujWj}CaU4BcF!{lJ+LiD){j1v1BG4mbyhK8_ME`{Qi>@L z#}s{Bsn(`)t1G%~6F2(GPaaS+r}HTqy=DytcOeG0cTZocru}0^#7^&U)6A3bfd(E; zjq9u(81M{V&-Gxo1C9jjSm)XEdiqG)(}nFHqVK!g&sg)#vcKNvEjVza&$s9G)FQ0G zO~ex1Bu!XLMx^Jj-9(sX53&4TB$#QHiO& zsp>_HBlmKnqlO7R!)5$+48-FzcfvnbuX3(ly8 zea#d@q8MK@g@D81vas#xIo&7zdh)>m%SRW+|H_^S61}VYm*cTqof_Q{^;LF0#{V^X zww%F8Xk&HCGl%WZd74EJ?sm1~SALa9(otG}{>0DhK5gWgrBddq^Bd6$N^NaQkL<=zE>n0IAs zCb{)+&=HUR4d1DNHzKunVLe`tCVUI{U0CRj1J1$3C;|O0ze2Pv!1sbS8s+o{kOMCAjRpChB?WmY zeXE!^>hg=H`6kQ(xd!EN%e+dk&pa*7uXHW>Y%96yeh<0^`V-wH zFVY?b?F{VUADHvk`3#)T!1)ZE&%pT%oX^1d44luv`3#)T!1)ZE&%pnu4B*zekEU(N zI&R10o*AEHJ$j37U~5Xefagqq`?N3q0QQ)9A9!jv_QpsD&U^W{P_U&X^)3YD#aqY^ z@C1YRdOJ~gf6hJK5h&HL%X%ds_fN6!AotA)aeE&>-G&FefQ;od;0wS}{U2Z4Gq;s; z_vprOlu0PFP!^!9L1{wSjB+c=H&O0G`8mo_lvh#yh;jzSwlheq5m$(LqPpbRpoO)y zmw6|zZb-*c>5BSDERcwMXZUCNr%$a)OXAEa3Ku@8tGixxpZgY09ZX{tPu1n!wq?Er zX|&|Dt3lH}$iXq@!MbEVQFnRtgCM*09BkYQUFR0FOUuoLvdhq)0xM?$NI5UBAwwUu zjttHCCm1f*pexZkAg+sI#?t;# zeB^$Wd(pXi4j(NHs_Nz^d-@KeNV^3Vj06y4sK%!iu8G3<9%&=AOC_osls8H&)G*$F z4`ZZ-dW^47c$9?}8bJz|T4=Emr|@VCEit}9?idRlZ5*KPSPS(UA5qwAp+4i=M8{j` zWTS$*6D+jc;Ddd`r~L{hToWs0&l{7pmnAyGxQV)xtrk@q9TYCN&^g9~NQ&Xtz748t zVvTVX(P`o! zbcx2z6xYO6hQTndve30g0nvI3U2lx1@YNQ&(U?K*Qtc|K8#G=cy4;F6Y}`rV720X( zH!3ceiGgYSd~hppkMZ~sfV__>@&v2s#k_n9Jo&7|^t=$`JfeEoz)fet6_R=psJTG$DVs9}9~X=m4t(%Th!$$q>rl^Yz=t+ljUbV?f!Jl% zc*^S}cDWq#6OFu2pry^Bl53&|rfI4`3xn$NGF-RT0ZJiGB!EB@MQbVRWjsS6S%YS} z|BVc{_7rGEd_JZKMUy3ul0_%+;r=(~7CIV{W~I}U(UAnD(@Y%*ei^o08&w!XVXSBz zK3p4RRfe%qg`w+@LVlRmjj;8}t~wy@2tI{BlcuRU<6abP*{e|YQn>*^rcm9thW zXQLuQOOno^DKj<^@$3XayZ%>j=*-wSX3V|M+%HCc3j!)*H!)*dY~?7FIjprUd1%Vn zmB%w_PeAZJ6m9!u5R{3#8{~0J99_u79Reao9>a$ZLPBd}pkp~>$0K1qB<*gc`fovc z6UF;$e0+i;%GN?+6N(yew=sDIh*Vw^weJIJ-+mO5WE_Irh)&&@00B3?7Sy=?Hdz?A zWzU1154JLO7s=m*WZ5y$2ZOE>v7d=}g!CTJ2Z64F{w{;&H9|XqPwhdjR$j1oqr3$9 zu8hYAEsrH8d;h^8#9jOJvI+>^fTH~WghC539?PbKwgpA`f0+K8K%cvu2I+`S>ZZ~~>0MbZ-h)TCn<*4ig z&`Uog`V6%f169#IM~w)|MI~^12v)h*qy5TzG0?rBkE{ef`BGs24s1#-DwCJt<43?I zuS4Z9D!eY%UPNr#OEP`@ZwfQ{74Y7{Kc*uWF&yraXs-nMi9t}hE`weoQTmnKG|`{X zm_+X(njrU8Ht0Q~%}9;*8cmNvZSfaCu%3Pda%hm!N%a#q@s~a0>gKcQWd*tVY)@(L7$4(XlJHH;ju2E z6=`Mp!}8qurTInL3T<_6A-d1y6M1=G018{w)06~Zg^N_OPGcu84?a655 z?X5vVhH5prbX3xB+l44c<{?p-15mZxyMUHjme3TUqa7%8#`L43(X6p!w5nmu_u9N` zWn|-WMAa}F_sR+kjHd`=utg>~30Rv31iM&@s&IY%zOeq~p^t~6rIQ?ufccA@5nxWt zpnuS(u!41^-@4QEC5#SpMoJ^vsSYc$=W#sM?uL1uIUa1LI|*!{7>SYZ60dV~Y9Hqh z!KXZz&heW}G>VFJGEotUHAd6HP(@^V^(=M1dTMhlUC|IpC6xe=UN<*3RzzkE5WwxH ztvkSw5G@U@*xdIwVmjk=Z!i*{<`45;6Q_40vF7OtX$hplkr>W%hiqmr7-~WtA*Z&r zhLRa_B#x?%ef&s+U9~)f=>fCE#sBl%^E9_-jyvVoUGByuy5=6U#IDV9RcN}~UHAu3 zlLr^ib+_(*?6J$iUE>G&Z`TWUmM#WwV#heQMv_%XHoSrpxfc$$dAuCTKu^<6;}`yJ@%apnLlg zmyS_^6!Q6i`z1}==-#?^+t+W|S^3zXT=OoxK4LUp&A{A+h8NJ4Yxr~b8P~$hPhAG{ z`Kz0lJ)+M_>^nM6Me?v&BMhZW78t|sYM zb3g_A+8sbE5{(s^X&a5ln!WOa4Gc$`!%Ec3 zh#7`QVd7x^CL|{qN~VO_m`<3D@o2gwCU6wU;oWSFCxvygU(g}59%~MXXxvWGb+cuN zh&Z`?b}|#Hxj7YRh=$B~Qxh5_OondNWo*HC8f-)W;;b3yf{JjwB~%e?P6W0`;;Bfi zVtFF|SOs{F0*u^9cix{*?PPeyNd} z3NiwY?pR!PA3BdiBa{fF(ip##YzQ=NO1G+sAb&;z$?MY?>4@RFs+n2S!Vx1TM?9!( z$i%69n(8MejazuPPy0#EWXX%ATN=>yW|G|@Gt>}jMjqwQM?!%XR`6YkAmTXNMqQ44 z#9{pyMLuMB8(%;!kYdL>j-bHfS+vPLm$n@{l0?yS2}LL;a5?hL~yfI zT1lY1R_^aNu+k22H6-l-Daw{Q##au|p^~(mfbB+S_uElF2!t^{**T`16C6juK)ip4 z?T3736rI_xor$6X9M#Cr7(X; zsku)Oejf#?v}IAXC2g)tRPO&Fqz*~R6}BYFPSM|kP1?EmXLm%y)Yb%_JIRaaUq4Ht zZ48U3Q_P~_NVaw1c0fa61_<4%E+HR$xkQ@JXXNz+c<=Dpk+_E7QxvEfeSj2oEF|TS ztVBTz2&f1y0fo`q1jTsV11dqOtS%=|mNMg)mZ+zT!N&&eXH7o65^L_Icj+}Vn%@Dp z7sU{*TB{g4t#m)Jlai9yk&u)CTr{Kf%Mgo5iY?=8@AJ^s#205lUsx9~+ zgnlXo*Ab|^rK)ZAqoAo2tRt}4Gi>%K(8i-6Sp*iFd#Cc6g@(?TSNoo37P2`xLV7UgP`e?{K`8TRIX zp_@3OZTZimv#Z}gc^idYZp-uWDZBeH%5y0Hiq3~AT=3+H0f%4VPi||$JsDssp?w$w8_C&>o7xEO z&YXw|ULq8wysEU~ZrvY=Mev#8ZHymxVG*$6!9Xe?{GqVflnAth%y1BzHYWUyDc;Td zg9?1P5lom2v_u-=E)FyJ@JrjuPD2vXjq#S25N?9~!B9iG*$gCj?@B8Yjl`Pbc0JJ0 zkO*zIF+SI_>&TH^<&W7dUCWZ)8UGDf3K>4{!jm-h9ta@A9D4Fu81RWG*g}wU@_h|p zc!ArO{f%1%f-QF&&ez-qIiD>AXv@{1j-SrN>6hnn9R1z$ee<&@a#ByZ|L1>b76fSvH~YED!5E z&vP1bHK;rN0u|d)u#qHncpj({0dX?e_81@WHWXWq=ZC7n*a;ft@RU`ymvUc0VXXAS z^GlCH&X2l%(H^h8D7M^b$ejkG&~7An%n4<`?vUf$qv01avsKAKM10F3$MaGzVDn%U z8P;X{{SI)tVP1M5*8{n`EExh@?mpCQIljAt@9|iF7P*HVa&JNIEy%Ht?aQ|Jq$S5b z<}b_mUJpkzGJb#IkmC<^M}Lmo3l2H{AlC;u+ZR}w`ma%EU(m07j|t?SwP*lRj&b}3 zg<(^U?_M~47JIb+JBJ+4mwf=adeH2PGAunPw!Iqc^wfCKZqV$@j`gn~&_Dh1m&mKU zXV1&~;L~0u3NOlCENkk6JeE%^sHj5R>_xk6#KGg<3+3(WKCckhbD~<8&EwGnyH3`K zay*IW;IS^7SC%E$fk8ipjmnafCk8;X+i^b@oJDprK&L&HIa&5j&qTj5QPxEnUJJA2 z9{qw4-m}D41X)Ko;&=^y<82!O&#n?W)WwGjLBIg=0q~zNhRV6gwZLibLmZdm-oAa= z_H5h<`K}(~>J30bzCTCd$}`tpg)4(xUlpzr#dTESYL()8sc?>TwJs`rki@zEDO>`1 z?or`vg*p$ZaBdH&bxYxDKyZChxQ9TV8YFPzoejLW-VnM4786`tR|wsF7Yr`0AB5_S zC|K7sID18{2ZV0*qiAsz;N z$RY0>r}Uo!zr^8x7x4S{+3^&KuK{=ZzZ3Y~R)5p~Pk`GO?HvSSw};2tehJ*}KMno& zau)wj!0rCi#0bIjA6>A|nK2ahhd2Tr3L3ZFx&Kan5#I2`a~u3$wFZ##ivYiF#W&P7 zMDWatJs!M}=Q%k0qP^|FwCNRU&-APocf+Z@!CZ zI&eOh*bSWeOMu%K<5{1D$FgvqrsOL)0+6LX4{Lg1uff9qPnP`iz}H*xFn|9AobNcJ zPwKx4oa=)f-y2!_e+TZ&zne=S@0kE)`yFd6oBa8}dDenvIUbfc`z0QxkT+O|y2@ zvK40i;+3oLRxH!38-OjTb+CeQvpE`X2t-YJYhnh{Td^P2f?vv~LP7tms+qF|8D=E7 z71*pQyts;YH^FpE3*V(=ybqFIG8=x_ImGQ0?rsu+ zZEAn0?%JjEuU=dW`wotL$(Jw})-Stae!aPD!Ge`nt}<86zoPz1Hc`G`t1+22Nf0`!llr3So0qK0lTbAg#ckcSP!TE?fm{|#<;nR1xq$-z596|C=l}o! diff --git a/src/mbpfan.o b/src/mbpfan.o deleted file mode 100644 index da206eea55e27fee0d041aeba2ca2e53fd4f031a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 19464 zcma)@3w#vS^~dk*ZjzBKNg&882n%d@h9p41@RA^JjF5sn6v~pvZW2j$!|sNcSS$e{ z#whe}Ti>AdZ*8m8wpyxa1;WePzoKX*MG#+Y5pC^X>$~#*{qCH}PNLZVd_J?^d(J)g z+` z4gM8Jruj3N1A2uf9YXeyjXD0meR=-;`EwJtH^&F5BOZSav|80T)6=-j?`iZ{7EROq zIZYeAw%BmkX!xpA`sOF;%u(tve6}>k{TctMM&z5Ui!A|EV;`#?yG1O|{Kguzp^v{2 z=DJzVa&AXC`Y|2ymVGFalfHQYde@Y)WncQ1eoW2uoE-!dp`{rW*s(n9W)w`8WG{w*b~K!3$n(-h zqM@^3-WC|($0?_`4qc>%`;8LpUzOMZPGMd<iUAzdBI|CEjx8;1isPPNCdrfZJ4Tk-2ykGOw_6;tOxg~5`>fLg({y@XQ zjK)tf_AQMcH$OGl;eVOmm%e9%-@oN#d{+9N^P%mmrwa@^)Z=g5;P)br5!OSe4=I@z zjqNIX8kMmsnRrrP!+R6a@1jKG<10tLgn8sHF`J4Q(2pCh@@JgHL_9Hub+jTP>3a~V z?DHQg)mfq95Jo1qwFdi__|1Vu_fyOKJx@*Z_oQX_$Go)YhP-Ykm-#cm?x-@O;o!uU z*1wxhPOXxcyEPuWKHu=v_r;`VSeKg^r}sp zz9)&{vW?RBTuf24Lyw7!xD<&FAueisap?ZWqs_C8^k438+`s>9KjY|&pLVc0@!8M= zjmMhj!{$Kaf&K6HGmcp{t;_s{baMJLqzcYnj+uaQY^(UW(75C2H@<>KgvPa`H~dkz z=B37?Lyt9=wDXm|hvzM4vrZKI3#roH_qXoD2ruKvE^9vrmSbd>>p6hjk8uXp1zMlQ zWSE7?Fst?1lV?x0rf`UX9cHb_RdX zbLkDm5D@91NB=+3FKCPY8()t;PKZ3d{!{c{Kf7l1*P||l9Z^5K1+5NkKh=6Jnb+e} ztsI7L3;)^gqX6Rd`Lx|$(2zIi_cv#XI0 zxCScI4KHR7<5DepOq~Ce*;7Q*TmmO%FA=@wIXdVqVupDejk84TX1+sLeMIbOvTE}Z z5&M|kncG)HpBblTpNM`lonn6xhnRC%c7TXE=DQUA#`CD&H85Xa7tBG%%UUcjKcwd& z(xXCiFO73VoM`?Xkuh_PJD_z9EHaxYjxs8=IL&;W;%FnP#Th1Dngzz6wda{;n7M_< zyIP!OUPsSUj9+NuV)HbO=NP<@x&~fl*032@h`7XzQ7jg5nOREXD@9yk-p<^)#ud73 zg*l(yFOfD^nV-{mzVU&sw?8L^Qi+sYJd)&W)h>AdJ7GHe^g7J)c=ZDr}eQaq5Lz@gCjkZaJ__?SD2eMO7Br}Y{ zrAVh{qjF=MHG@>@0jP}eGM`f2%$uO6{6I7HaY_?e$TiRl*L0O`JjQ1G*j%^K1WiUD z5QBm)x;#Z&AKTLvmWNQ@{X=HBjc1_iay@!#DY`tQ^Jv*6q?3#-$^@ZtS6XHM)>cL< ztTL4>1H+%gmurR9jIQv|WhZ`ID|AvfV};cWkLP!=&ol<0m}iJ<8U%MWOuGdFYi-5F z-7kk4xkG{OyxBmSuDicu=_{D7K`H2&)(-LO>25jXh+7UVg*fc)aM<19u)B6*IkeL3 z(Ps9j(&|ww99q%T{q3rH7(3AcWAYIgaVrvfl9lS&0EID`N!Je5i`l*7SY{YgsA`6o zr3ZZ~!}>7xKDWWWF^vJ@bWv(&pGEXO-O^CC^OC{T%wThJ`fQ^8Onsc~2xMJ>8DPv| z&imN=mco!lhF~{l-Hpp!zwbd7&psYmJ=oe_v+$F3j8+RdOqW90pM|$V)k5{WKcTRX zh0BnqOWwqi131r~L*ffs&G-EV;^VY<2#KRi+=Il2G{xreq(T~Kuu>zq5shUSiXicP zaS8Tjtd)ALfN54KZK{~R2m0PIWmHpm6M_}9m5f;r__JbGV?~Hvm6+998ZOi%W)Vha zgcY+oiCOPzWyGjo0#`a>Q&IB`P#CM8M}^La^<%``I|4gZuiIf@MeKS;Y>i#`W)$YM zHrAw~E8kqWKZAA(hF#DZWBqs->cH)Sx*r3_Dsi-puW@0{OFM#gH*sb~>WBnoM2k`V1 z$9^+*V~;Nh`pulbpF`$Rz-^#0f{kgZLQSmaQRIz+;giT8b&*no8lW^wV2`&mYxmKYTdIeQH_0YPk1{6uRU4M^FFRPwnE=4AM%h(A>9dS=u{+z z%)`$kkcKQpVm}hxh8bf~+Bj~9j}d(Zd2eAx_nO7#a4BlMQUO~7lOU^!2`vt%h+V{b ziWzjKbMK@0YnJ`fI@tV^;&aS>m9BeZG_s1@#BmfG=&F@HTLQ5!d)5`@SJ21Dols~v zd)I3%WZknhu+lxtVb9$0*HjLB=8m6+qy8*@HB7J5#b?{avu)i_2FF?av|ao)6z|Jx z#6}d|&f;HeWAW>t>h%!hA={9;iM8`YnzIW(tTg9gonY}1ydt~;(|s(ij0aKP_ebdN zK`Awu&bMPqoY2lK#~;{^AMfB8-Lf3BTbQaI&sdn|_Jfao%^fVnR#%WHV6rvlyZ&9Ub|LbYJrzV8A&(~ z*$wFep{-S2xnjy}D}!eEbamaN1QbeLoQjZjFKBBi&OL1rEekc<-uOz%XT@PpL zaLj0Hw04BUiyryhMBS6U6Y&%+Ps^@jE+mx}A>x0u{anAjeth4061c_L38&k7HkE6u9GDMD~Fbtto`qR(%QxWmjC>nyrZ=oEE)W-$#I-Y7Ln@by8?r3MIg*0KcNPc7O&msR!?1n1&hSJRa zw$gUn5hR*^OT5_3DY`px56Op67+;g*nhR+!Im1e4kouM#bAaSMnw}g?v|#7tAP-vU z4Dyhb_L7IKv`HSZ(jJnFP3_3;E?dSsAeYMD$t9CzyrbAQcFY>5=|q?oVMpMI|I*3D z7bv9fir}BzjU9!Edv&1&gjH+)&V+*nb_45SX*ZCj?5SgZWkVdLRV4)bi@{F#S!ny= zqLWuMR8D+~9TV}Pjm~^V6oZ*4-PVeE$gw(kq1;8fY}+)lm!O84Z`W`J7H?q3*m+2< z=Q#<;x2&|6{EL;&AoZgk759+^*kPYRj!Q?J0?IrKC(k97; zu(QE)AoI45XD*Uu9Ssgc^;|mY?P>hJ1tXj> zh_A5I^gO8V+7p+b99VK?j~);#h(ZL7G1^2o%vNZLES)SNEKhCom!4S8qdFZ+*~!#( z+QXus04^Qlw%Pm^a?fHnRh?0%`i{!lN$H%{L^npYhELcku_tUORcnoHg93VEZGXUS zsPBx0!5r*J@;m)F!+1oNs#7mgiK zQys2fJEAgNpI25Lk68va)n$2=<>h(RW7`=NCD}RDZhLN0`yk^pBcW{&Y zl8Nr}O>W=zhNs+-C*9r5O`*A!<^VV_E4R4UPIRZvF$aZio4Zi!(2$39%U)4t+|DkU z^vsS)<2@etX_x6kB@bTj8el;Dvx|Lr@cK>WZuj~*xG-T(NC=M`#tQe^CF_2$`G)+x ze|1g1cvZDoekB{`&M}jW znz)JuV!?1M5{;=)O(YUk6_NU~nxG2Io)?%gyLh^ax8(-{t4nL@t*lt->R=!ku2A}a z{8&{a8u#%J#8~zEU?8q$&Ryu6I()bfM@{Z%6^MjFu^=*~qHu6cK$l}BYogWhV4yCJ zw+uoxrIj%itFN_kYfIOPna&uc+f-H`3Y3+`f+|#vRz`y%bfe1A#PT(GX`wW_GKdb* zeKc5Fp&J?qR9A-a{sCGM4C-># za8-3>m1Qcu3sj*-6{v}nuT*VTfj~tt7FTsRBg8{0P+lJmlt*gnYr_gJSU6e}sEc46 z#EcE#sD&$o@MMSVnsK_x=s~PxTw?G7tK+2@H#^h;c16OjTVW3?m17GqM0r(_+F)Kq zWwdmCbtGOL&MS#Vt__yQV|g=5!y~3f!trRNX2i78U~MF<>ccTaFIeG2bE2v|QXh^7 zqlyERh-4s8S6We^0@oDU(QDVfYetT8#(FiTO2T6zrfoIp*(8x@r{t(wBdh{?;$ccC zUHM35&FsXq(laGcAFhuDE3EmW=L2V3EFP@0rU<7Lr*e6)x<=LEL>=HPR=TUTJZ9=5 zH8^uu;w>2T8pnVx8>on^K^DzfI1mo5#YBlm%j?!ztwDW^2&VrvBS*KJbdI>hEu%4* z&X|WBRAn7Z?YkTZ)yDOq%lfS9>+7uP)!wk}fYNQ^F%piT3Jf}ioD(V-EsfXXC}51r zO3PQ)*I9=XuUuAlV7{sz2MwBb&B!qwjxa~lahydeEJr%1R;&Z45gy9d@Q*iEqR$jM zFI-<+hFPP}DuEbBO-HXRSc#hP=V=^nYMH>b8qJB|jUydYdg0-O7QrT5jssH7oH}*9 zZ^*(jOvL)U;_7f|G-91>MvSc2$}vOb@v+Y>rGx^BjUX%P+&uvbTL( z`E^F8uREBM${Ufpv7f;OT|Xy-@2#x=M%Lee`hBh|(lVx|c~P^8nmtGKJERFI!$;#p0viQ1&>o40QTBmZ$ zBek_boW*m)k$5l{SFI6f6i(dQORy>sik8*}8G`@s)(?gZCm*$PE5dFsRVioi2U`|) z-p1Hl5pKS=qagJx&s98cbF)Rp(CLkjS-g@a?A88oq-k&4-C$?X2%9Z(&e8uUDzVYd zK0YWjkNaeyZO8w2)9;F(ZiP_VZKupM_f^w<^_^5_x=5?5ua&Sf$p&mi6)$h`w0w_1K;M9?8VI z9LJ@-*?G5iuJFd>_rcPC`0 zzkRdtX9{%mmyX@`_ba6BGDxZ}n2^42=*J_-bNV|0e|%U+f4nWU{k0(N^f$6U{(-S4 znIrERq@DhbUSZ93`a>w&{JkJ$I1jna^(*y1tV}l6#r@Ah+b(M;{&y4`!nS)$(stGi z*f}@aar*>2({?Ck!LG>8gx+q#he$j7aRU8d8`uwh9tAnvjFBS@YTGdQclfiD&N@*< zb%V2GXaui2c3d)$W}QxdZ{xsQYCA!mBMB+7IrSd(&+~~K`g7(|cGf`I?zL6Qtskx= zJhFZUD?^bwEK6~(2;;Ba zh*-tz)>)RZ^3pKwC;r3I_dhJ@>4B zTXnCKy_Nb0^I`vq38m4K~)3N5ZduCmCEo8Q0M=vAdRXKc#LZ(ce2soPhbJn+qr5k3jnW ziZc9V;@F^^1+v^g66KbOK13OIYen8hc>vaLB<%K*KBf4Un1O?ZuU@l0u4!Bt6f})* zzp4VVzxA)Sk&R_JmLP{!XEDeNcePh-E~rw0lDeiHRDp7gg~WE52fP6Awa8dz-bEKMTRn*B+f z+EJx4@Tv>{Az^-zaHgFDcHx6UK4ND%z7sSgyCQP&iFkB3ve1DQpz}P z3BP@X14y)EIEnV|YT1WfJwDCSm_q687gL|4S0~87Y>&n{Xfr`!T`^B>WeV@HdZyp9P{XBVoT%xJvT* zJ}2!qi~d#;<$ox=NAmfiC++^5grD7#|CH$W315)>mqk7yJVnCqUnT!N(SIQPm*gkm znnJr&VLFL=Gle}!)bEr0Tp{m~X;&niN5bwZk@>PI^S?{Nf2HKti9RmeDEe)}ACR!W zUAT*cp9h2wk*McalD}8veI&-?kmz3!{mUYs7WsYQ$0YoIPNG~AZkp(~6A3v}2nM8Zu7QQd}>9__nKT|kRI9NDWIG;rMGT}-RacY+Q9iqQa^uG}OFGass zcu?~HLLx5bgr5qN;FJ2Uh%X?-ZxN>rJyF3R+@exs@8J8kn1ekPZSmjXA7?sE)?qZ3G7Qn4hn09 ztAy)>`q>fen?>FsyhC`8P_Iv5_o&Ff6Fwzu5grjfFMLVZDm*28U-(bqXTpCA`OKI7 zHibQeS;7IrK|;O`N_*bJkyC{;g}is8e!j3w7!uZ!I1fif*6SnK^Hn z@DAZF;RC|Qg--~N3jZiPE__vZTKJ}r-@&lHPlS9uh_VYid6AI!6_j~DLG~986^;;& z5$g3A(o;m9F61j4EdL$h)xz%z^?D6y-jC2ODqJo6o^X@!CgCkYzFxv|yni9@7wYvL z(!8Icp0BQuEkeFtLYc2=kjI5@3jZSfyYPR7p9%jh^l;sVG+)Ic_4*OyeG+B8js*E^ zfwF$J4;&@(IN>DWbm43vKM7(veV+wZh+HLHBfL(yNqD1BuS-#Ghse8x4+wuD{H1W8 z@Su<{$+7+yg#5yX@|(i5!uN&z@Q3+4L6Q94hs+cX5DpUZwH@a3{{V8l@G{{H;T++7 z;UeL3VVUq+VOY3YxL&wPc%$$~!aIfc3m+8f&m7UNCq&*SJSgPHLu~h}!q2)^rcvW7@dc6(#Qjup0^?Don1tRNpH)MXC z#QLrkt`gP@`H>Ryw+U}0vGbcH>URl$D*T0z-z+iz_rk-%KM0QtUlrQx@wY_g2Tm;i zsgMuIDD#6KGF6x^Y_~2){xH$!3dab?3Hfyt{ah*JCl-{i7V>i`%6fec#zkH$v^Pmirh!Y&#h>m zCmbu}mwnVv5l$Bt3rmEHg-eBc-H&ugWWC-;dY#A(!mYwvgg+48C)DeHq<wAnPzt^Jv2_aO0xs=LkEl^9n_8ulHt&Tr4aRUL{;1tPoZSYlL;exbOzyX5lvB&BEJ- zcMEq39}qqyd|23V9k@^Q2ZhIkFAGlyUlaaK_@3|s;YUI|_P63KwAX`Pk$VdD^Dm5x zPh>vp=Q!mH3xxWk6X^L|llikqjBBy5M0k~Og|I?cC9Dyy6>bpTAlxk6F4WK0P|xim z-z|Jd_^|L%;bX#m!h^!2!aoX62wxMP5xy<_K=_gHQ{k6Fes9G%=;wD}24%Ffo6sJg Z;UfDmE?k4I5bhFke5p6#m-LhH^MBqf52OG9 From e63b4d1667e8ad5a420faae31b2d6e0a9c7df8d1 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 9 Jun 2012 16:40:12 +0200 Subject: [PATCH 003/235] Enhanced README --- README | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/README b/README index a8a499e..92e629d 100644 --- a/README +++ b/README @@ -1,5 +1,11 @@ -This is an enhanced version of rvega's Fan-Control-Daemon , -which itself is an enhanced version of Allan McRae mbpfan +Fan-Control-Daemon +==================== + +Introduction +--------------------- + +This is an enhanced version of [rvega's Fan-Control-Daemon](https://github.com/rvega/Fan-Control-Daemon), +which itself is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). @@ -9,17 +15,30 @@ This enhanced version assumes any number of processors and fans (max. 10). * It requires root use +Compile Instructions +--------------------- + Compile with -gcc -o mbpfan -lm mbpfan.c +make + +Manually compile with +gcc -o bin/mbpfan src/mbpfan.c -lm + + +Run Instructions +--------------------- Run with -sudo ./mbpfan +sudo bin/mbpfan + + +License +--------------------- +GNU General Public License version 3 -Original README below. --- -It includes a PKGBUILD for easy installation in Arch Linux. -Based on: +Based On +--------------------- http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ From 0428dafb3ac9d13bac3446da58552e3616fd8cf8 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 9 Jun 2012 19:07:25 +0200 Subject: [PATCH 004/235] Take manual control of the fans Converted all unsigned short to int Changed Polling Interval to 5 --- .gitignore | 2 +- Makefile | 4 +- src/mbpfan.c | 141 +++++++++++++++++++++++++++++++-------------------- src/mbpfan.h | 24 +++++---- 4 files changed, 104 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index afe16db..01b7530 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ *.iml # Object files *.o - +bin/* # Libraries *.lib diff --git a/Makefile b/Makefile index 7a6f096..0ed28a1 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ ifeq ($(COMPILER), G++) else OBJ = o endif - COPT = -O2 + COPT = CCMD = g++ OBJFLAG = -o EXEFLAG = -o @@ -53,4 +53,4 @@ clean: rm -rf $(SOURCE_PATH)*.$(OBJ) $(EXE) rebuild: clean all -#rebuild is not entirely correct \ No newline at end of file +#rebuild is not entirely correct diff --git a/src/mbpfan.c b/src/mbpfan.c index 5ca8931..613703b 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -39,64 +39,28 @@ #define min(a,b) a < b ? a : b #define max(a,b) a > b ? a : b -unsigned short min_fan_speed = 2000; -unsigned short max_fan_speed = 6200; +int min_fan_speed = 2000; +int max_fan_speed = 6200; /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature * max_temp - fan will run at full speed above this temperature */ -unsigned short low_temp = 63; // try ranges 55-63 -unsigned short high_temp = 66; // try ranges 58-66 -unsigned short max_temp = 86; // do not set it > 90 +int low_temp = 63; // try ranges 55-63 +int high_temp = 66; // try ranges 58-66 +int max_temp = 86; // do not set it > 90 -unsigned short polling_interval = 7; +int polling_interval = 5; struct s_sensors { char* path; - char* fan_path; + char* fan_min_path; + char* fan_man_path; unsigned int temperature; struct s_sensors *next; }; -void find_fans(t_sensors* sensors) -{ - t_sensors *tmp = sensors; - - char *path = NULL; - const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; - const char *path_end = "_min"; - int path_size = strlen(path_begin) + strlen(path_end) + 2; - char number[1]; - sprintf(number,"%d",0); - - int counter = 0; - - for(counter = 0; counter<10; counter++) { - path = (char*) malloc(sizeof( char ) * path_size); - path[0] = '\0'; - sprintf(number,"%d",counter); - - strncat( path, path_begin, strlen(path_begin) ); - strncat( path, number, strlen(number) ); - strncat( path, path_end, strlen(path_begin) ); - - FILE *file = fopen(path, "r"); - - if(file != NULL) { - if (tmp->path != NULL) - tmp->fan_path = (char *) malloc(sizeof( char ) * path_size); - strcpy(tmp->fan_path, path); - tmp = tmp->next; - fclose(file); - } - } - - free(path); - path = NULL; -} - t_sensors *find_sensors() { @@ -128,7 +92,7 @@ t_sensors *find_sensors() s = (t_sensors *) malloc( sizeof( t_sensors ) ); s->path = (char *) malloc(sizeof( char ) * path_size); strcpy(s->path, path); - int result = fscanf(file, "%d", &s->temperature); + fscanf(file, "%d", &s->temperature); if (sensors_head == NULL) { sensors_head = s; sensors_head->next = NULL; @@ -150,6 +114,75 @@ t_sensors *find_sensors() return sensors_head; } +void find_fans(t_sensors* sensors) +{ + t_sensors *tmp = sensors; + + char *path_min = NULL; + char *path_man = NULL; + + const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_min_end = "_min"; + const char *path_man_end = "_manual"; + + int path_min_size = strlen(path_begin) + strlen(path_min_end) + 2; + int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int counter = 0; + + for(counter = 0; counter<10; counter++) { + path_min = (char*) malloc(sizeof( char ) * path_min_size); + path_min[0] = '\0'; + path_man = (char*) malloc(sizeof( char ) * path_man_size); + path_man[0] = '\0'; + sprintf(number,"%d",counter); + + strncat( path_min, path_begin, strlen(path_begin) ); + strncat( path_min, number, strlen(number) ); + strncat( path_min, path_min_end, strlen(path_begin) ); + + strncat( path_man, path_begin, strlen(path_begin) ); + strncat( path_man, number, strlen(number) ); + strncat( path_man, path_man_end, strlen(path_begin) ); + + + FILE *file = fopen(path_min, "r"); + + if(file != NULL) { + if (tmp->path != NULL){ + tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); + tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); + } + strcpy(tmp->fan_min_path, path_min); + strcpy(tmp->fan_man_path, path_man); + tmp = tmp->next; + fclose(file); + } + } + + free(path_min); + path_min = NULL; + free(path_man); + path_man = NULL; +} + +void set_fans_man(t_sensors *sensors) +{ + + t_sensors *tmp = sensors; + FILE *file; + while(tmp != NULL) { + file = fopen(tmp->fan_man_path, "rw+"); + if(file != NULL) { + fprintf(file, "%d", 0); + fclose(file); + } + tmp = tmp->next; + } +} + t_sensors *refresh_sensors(t_sensors *sensors) { @@ -159,7 +192,7 @@ t_sensors *refresh_sensors(t_sensors *sensors) FILE *file = fopen(tmp->path, "r"); if(file != NULL) { - int result = fscanf(file, "%d", &tmp->temperature); + fscanf(file, "%d", &tmp->temperature); fclose(file); } @@ -171,14 +204,12 @@ t_sensors *refresh_sensors(t_sensors *sensors) /* Controls the speed of the fan */ -void set_fan_speed(t_sensors* sensors, unsigned short speed) +void set_fan_speed(t_sensors* sensors, int speed) { - t_sensors *tmp = sensors; - + FILE *file; while(tmp != NULL) { - FILE *file = fopen(tmp->path, "rw"); - + file = fopen(tmp->fan_min_path, "rw+"); if(file != NULL) { fprintf(file, "%d", speed); fclose(file); @@ -210,12 +241,12 @@ unsigned short get_temp(t_sensors* sensors) int main() { - unsigned short old_temp, new_temp, fan_speed, steps; - short temp_change; - float step_up, step_down; + int old_temp, new_temp, fan_speed, steps; + int temp_change; + int step_up, step_down; t_sensors* sensors = find_sensors(); - + set_fans_man(sensors); new_temp = get_temp(sensors); fan_speed = 2000; set_fan_speed(sensors, fan_speed); diff --git a/src/mbpfan.h b/src/mbpfan.h index 33a4a9b..c111be6 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -1,21 +1,21 @@ /** Basic fan speed parameters */ -extern unsigned short min_fan_speed; -extern unsigned short max_fan_speed; +extern int min_fan_speed; +extern int max_fan_speed; /** Temperature Thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature * max_temp - fan will run at full speed above this temperature */ -extern unsigned short low_temp; -extern unsigned short high_temp; -extern unsigned short max_temp; +extern int low_temp; +extern int high_temp; +extern int max_temp; /** Temperature polling interval * Default value was 10 (seconds) */ -extern unsigned short polling_interval; +extern int polling_interval; /** Represents a Temperature sensor */ @@ -28,6 +28,12 @@ typedef struct s_sensors t_sensors; */ t_sensors *find_sensors(); +/** + * Given a linked list of t_sensors, refresh their detected + * temperature + */ +t_sensors *refresh_sensors(t_sensors *sensors); + /** * Detect the fans in /sys/devices/platform/applesmc.768/ * Associate each fan to a sensor @@ -35,7 +41,7 @@ t_sensors *find_sensors(); void find_fans(t_sensors *sensors); /** - * Given a linked list of t_sensors, refresh their detected - * temperature + * Given a list of sensors with associated fans + * Set them to manual control */ -t_sensors *refresh_sensors(t_sensors *sensors); \ No newline at end of file +void set_fans_man(t_sensors *sensors); \ No newline at end of file From a179c7dc7357c11536ddf4e12a3f483fc1a88337 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 9 Jun 2012 19:14:17 +0200 Subject: [PATCH 005/235] Added non-maintained files --- PKGBUILD => PKGBUILD-not-maintained | 1 + README => README.md | 0 mbpfan.rc-BROKEN => mbpfan.rc-not-maintained | 2 ++ 3 files changed, 3 insertions(+) rename PKGBUILD => PKGBUILD-not-maintained (93%) rename README => README.md (100%) rename mbpfan.rc-BROKEN => mbpfan.rc-not-maintained (97%) diff --git a/PKGBUILD b/PKGBUILD-not-maintained similarity index 93% rename from PKGBUILD rename to PKGBUILD-not-maintained index 9442188..5ee9cf6 100644 --- a/PKGBUILD +++ b/PKGBUILD-not-maintained @@ -1,3 +1,4 @@ +# not maintained in this project # Maintainer: Allan McRae pkgname=mbpfan diff --git a/README b/README.md similarity index 100% rename from README rename to README.md diff --git a/mbpfan.rc-BROKEN b/mbpfan.rc-not-maintained similarity index 97% rename from mbpfan.rc-BROKEN rename to mbpfan.rc-not-maintained index 630ea4a..412f154 100644 --- a/mbpfan.rc-BROKEN +++ b/mbpfan.rc-not-maintained @@ -1,5 +1,7 @@ #!/bin/bash +# not maintained in this proejct + daemon_name=mbpfan . /etc/rc.conf From cd3c15ba9d7524d995a368c089b0809f269fbb1a Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 11 Jun 2012 21:43:53 +0200 Subject: [PATCH 006/235] Fixes #1 and #2 --- README.md | 16 ++++---- mbpfan.init | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ src/daemon.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/daemon.h | 9 +++++ src/global.h | 2 + src/main.c | 50 ++++++++++++++++++++++++ src/mbpfan.c | 56 ++++++++++++++++++++------- src/mbpfan.h | 10 +++-- 8 files changed, 327 insertions(+), 25 deletions(-) create mode 100755 mbpfan.init create mode 100644 src/daemon.c create mode 100644 src/daemon.h create mode 100644 src/global.h create mode 100644 src/main.c diff --git a/README.md b/README.md index 92e629d..538a7ab 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ which itself is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.co Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). - * It only uses the temperatures from the processors as input. - * It requires coretemp and applesmc kernel modules to be loaded. - * It requires root use +* It only uses the temperatures from the processors as input. +* It requires coretemp and applesmc kernel modules to be loaded. +* It requires root use Compile Instructions @@ -40,8 +40,8 @@ GNU General Public License version 3 Based On --------------------- -http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ -http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ -https://launchpad.net/macfanctld -http://paste2.org/p/862259 -http://www.lobotomo.com/products/FanControl/ +* http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ +* http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ +* https://launchpad.net/macfanctld +* http://paste2.org/p/862259 +* http://www.lobotomo.com/products/FanControl/ diff --git a/mbpfan.init b/mbpfan.init new file mode 100755 index 0000000..6fd72db --- /dev/null +++ b/mbpfan.init @@ -0,0 +1,103 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: mbpfan +# Required-Start: $remote_fs +# Required-Stop: $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: mbpfan initscript +# Description: Start the mbpfan daemon +### END INIT INFO + +DESC="Start the mbpfan daemon" +NAME=mbpfan +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DAEMON=/usr/sbin/$NAME +PIDFILE=/var/run/$NAME.pid +SCRIPTNAME=/etc/init.d/$NAME +DAEMON_ARGS="" + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.0-6) to ensure that this file is present. +. /lib/lsb/init-functions + +# Function that starts the daemon/service +do_start() +{ + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + || return 1 + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \ + $DAEMON_ARGS \ + || return 2 +} + +# Function that stops the daemon/service +do_stop() +{ + # Return + start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON + [ "$?" = 2 ] && return 2 + return "$RETVAL" +} + +# Function that sends a SIGHUP to the daemon/service +do_reload() +{ + start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME + return 0 +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + restart|force-reload) + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/src/daemon.c b/src/daemon.c new file mode 100644 index 0000000..0dc4e11 --- /dev/null +++ b/src/daemon.c @@ -0,0 +1,106 @@ +/** + * Credits: http://peterlombardo.wikidot.com/linux-daemon-in-c + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "mbpfan.h" +#include "global.h" + +#define DAEMON_NAME "mbpfan" +#define PID_FILE "/var/run/mbpfan.pid" + + +void signal_handler(int signal) +{ + + switch(signal) { + case SIGHUP: + //TODO: restart myself + syslog(LOG_WARNING, "Received SIGHUP signal."); + exit(0); + break; + case SIGTERM: + syslog(LOG_WARNING, "Received SIGTERM signal."); + //TODO: free resources + exit(0); + break; + case SIGINT: + syslog(LOG_WARNING, "Received SIGINT signal."); + //TODO: free resources + exit(0); + default: + syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); + break; + } +} + + +void go_daemon(void (*function)()) +{ + + // Setup signal handling before we start + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); + + syslog(LOG_INFO, "%s starting up", DAEMON_NAME); + + // Setup syslog logging - see SETLOGMASK(3) + if(verbose) { + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + } else { + setlogmask(LOG_UPTO(LOG_INFO)); + openlog(DAEMON_NAME, LOG_CONS, LOG_USER); + } + + + pid_t pid; + pid_t sid; + + if (daemonize) { + + pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + umask(0); + + // new SID for the child process + sid = setsid(); + if (sid < 0) { + exit(EXIT_FAILURE); + } + + if ((chdir("/")) < 0) { + exit(EXIT_FAILURE); + } + + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } + + + function(); + + if(daemonize) + syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME); + + return; +} diff --git a/src/daemon.h b/src/daemon.h new file mode 100644 index 0000000..2e2d30d --- /dev/null +++ b/src/daemon.h @@ -0,0 +1,9 @@ +/** + * ...handles signals :-) + */ +void signal_handler(int signal); + +/** + * Daemonizes + */ +int go_daemon(void (*function)()); \ No newline at end of file diff --git a/src/global.h b/src/global.h new file mode 100644 index 0000000..910ed89 --- /dev/null +++ b/src/global.h @@ -0,0 +1,2 @@ +extern int daemonize; +extern int verbose; \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..693efcd --- /dev/null +++ b/src/main.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include "mbpfan.h" +#include "daemon.h" +#include "global.h" + +int daemonize = 1; +int verbose = 0; + +void print_usage(int argc, char *argv[]) +{ + if (argc >=1) { + printf("Usage: %s OPTION(S) \n", argv[0]); + printf("Options:\n"); + printf("\t-h Show this help screen\n"); + printf("\t-f Run in foreground\n"); + printf("\t-v Be (a lot) verbose\n"); + printf("\n"); + } +} + +int main(int argc, char *argv[]) +{ + + int c; + while( (c = getopt(argc, argv, "hfv|help")) != -1) { + switch(c) { + case 'h': + print_usage(argc, argv); + exit(0); + break; + case 'f': + daemonize = 0; + break; + case 'v': + verbose = 1; + break; + default: + print_usage(argc, argv); + exit(0); + break; + } + } + + // pointer to mbpfan() function in mbpfan.c + void (*function)() = mbpfan; + go_daemon(function); + exit(0); +} \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index 613703b..419cc1c 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -28,12 +28,14 @@ */ -#include #include #include #include #include +#include +#include #include "mbpfan.h" +#include "global.h" /* lazy min/max... */ #define min(a,b) a < b ? a : b @@ -62,7 +64,7 @@ struct s_sensors { }; -t_sensors *find_sensors() +t_sensors *retrieve_sensors() { t_sensors *sensors_head = NULL; @@ -130,14 +132,15 @@ void find_fans(t_sensors* sensors) char number[1]; sprintf(number,"%d",0); - int counter = 0; + int n_sensors = 0; + int n_fans = 0; - for(counter = 0; counter<10; counter++) { + for(n_sensors = 0; n_sensors<10; n_sensors++) { path_min = (char*) malloc(sizeof( char ) * path_min_size); path_min[0] = '\0'; path_man = (char*) malloc(sizeof( char ) * path_man_size); path_man[0] = '\0'; - sprintf(number,"%d",counter); + sprintf(number,"%d",n_sensors); strncat( path_min, path_begin, strlen(path_begin) ); strncat( path_min, number, strlen(number) ); @@ -151,17 +154,25 @@ void find_fans(t_sensors* sensors) FILE *file = fopen(path_min, "r"); if(file != NULL) { - if (tmp->path != NULL){ - tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); - tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); + if (tmp->path != NULL) { + tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); + tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); } strcpy(tmp->fan_min_path, path_min); strcpy(tmp->fan_man_path, path_man); tmp = tmp->next; + n_fans++; fclose(file); } } + if(verbose) { + printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); + if(daemonize) { + syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); + } + } + free(path_min); path_min = NULL; free(path_man); @@ -239,17 +250,24 @@ unsigned short get_temp(t_sensors* sensors) } -int main() +void mbpfan() { int old_temp, new_temp, fan_speed, steps; int temp_change; int step_up, step_down; - t_sensors* sensors = find_sensors(); + t_sensors* sensors = retrieve_sensors(); set_fans_man(sensors); new_temp = get_temp(sensors); fan_speed = 2000; set_fan_speed(sensors, fan_speed); + + if(verbose) { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { + syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); + } + } sleep(polling_interval); step_up = (float)( max_fan_speed - min_fan_speed ) / @@ -282,11 +300,21 @@ int main() fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); } + if(verbose) { + printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(daemonize) { + syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + } + } + set_fan_speed(sensors, fan_speed); - sleep(polling_interval); - printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(verbose) { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { + syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); + } + } + sleep(polling_interval); } - - return 0; } diff --git a/src/mbpfan.h b/src/mbpfan.h index c111be6..20bcfe2 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -11,7 +11,6 @@ extern int low_temp; extern int high_temp; extern int max_temp; - /** Temperature polling interval * Default value was 10 (seconds) */ @@ -26,7 +25,7 @@ typedef struct s_sensors t_sensors; * Detect the sensors in /sys/devices/platform/coretemp.0/temp * Return a linked list of t_sensors (first temperature detected) */ -t_sensors *find_sensors(); +t_sensors *retrieve_sensors(); /** * Given a linked list of t_sensors, refresh their detected @@ -44,4 +43,9 @@ void find_fans(t_sensors *sensors); * Given a list of sensors with associated fans * Set them to manual control */ -void set_fans_man(t_sensors *sensors); \ No newline at end of file +void set_fans_man(t_sensors *sensors); + +/** + * Main Program + */ +void mbpfan(); \ No newline at end of file From 7e65d1e6582b2051ac27c91f96b3b478f4997749 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 15 Jun 2012 21:07:11 +0200 Subject: [PATCH 007/235] Fixes #9 --- mbpfan.init | 34 +++++++-------- src/daemon.c | 114 +++++++++++++++++++++++++++++++++++++++++---------- src/daemon.h | 41 ++++++++++++++++++ src/global.h | 5 ++- src/main.c | 22 +++++++++- src/mbpfan.c | 2 +- src/mbpfan.h | 16 ++++++++ 7 files changed, 192 insertions(+), 42 deletions(-) diff --git a/mbpfan.init b/mbpfan.init index 6fd72db..5fbe7d0 100755 --- a/mbpfan.init +++ b/mbpfan.init @@ -77,23 +77,23 @@ case "$1" in status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? ;; restart|force-reload) - log_daemon_msg "Restarting $DESC" "$NAME" - do_stop - case "$?" in - 0|1) - do_start - case "$?" in - 0) log_end_msg 0 ;; - 1) log_end_msg 1 ;; # Old process is still running - *) log_end_msg 1 ;; # Failed to start - esac - ;; - *) - # Failed to stop - log_end_msg 1 - ;; - esac - ;; + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; *) echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 exit 3 diff --git a/src/daemon.c b/src/daemon.c index 0dc4e11..a9b7fa1 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,7 +1,20 @@ /** - * Credits: http://peterlombardo.wikidot.com/linux-daemon-in-c + * Copyright (C) 2012 Peter Lombardo + * Modifications (2012) by Daniel Graziotin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * */ + #include #include #include @@ -16,8 +29,33 @@ #include "mbpfan.h" #include "global.h" -#define DAEMON_NAME "mbpfan" -#define PID_FILE "/var/run/mbpfan.pid" +int write_pid(int pid){ + FILE *file = NULL; + file = fopen(program_pid, "w"); + if(file != NULL) { + fprintf(file, "%d", pid); + fclose(file); + return 1; + }else{ + return 0; + } +} + +int read_pid(){ + FILE *file = NULL; + int pid = -1; + file = fopen(program_pid, "r"); + if(file != NULL) { + fscanf(file, "%d", &pid); + fclose(file); + return pid; + } + return -1; +} + +int delete_pid(){ + return remove(program_pid); +} void signal_handler(int signal) @@ -27,15 +65,18 @@ void signal_handler(int signal) case SIGHUP: //TODO: restart myself syslog(LOG_WARNING, "Received SIGHUP signal."); + delete_pid(); exit(0); break; case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); + delete_pid(); //TODO: free resources exit(0); break; case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); + delete_pid(); //TODO: free resources exit(0); default: @@ -44,8 +85,7 @@ void signal_handler(int signal) } } - -void go_daemon(void (*function)()) +void go_daemon(void (*mbpfan)()) { // Setup signal handling before we start @@ -53,36 +93,37 @@ void go_daemon(void (*function)()) signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - syslog(LOG_INFO, "%s starting up", DAEMON_NAME); + syslog(LOG_INFO, "%s starting up", program_name); // Setup syslog logging - see SETLOGMASK(3) if(verbose) { setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); } else { setlogmask(LOG_UPTO(LOG_INFO)); - openlog(DAEMON_NAME, LOG_CONS, LOG_USER); + openlog(program_name, LOG_CONS, LOG_USER); } - pid_t pid; - pid_t sid; + pid_t pid_slave; + pid_t sid_slave; if (daemonize) { - pid = fork(); - if (pid < 0) { + pid_slave = fork(); + if (pid_slave < 0) { exit(EXIT_FAILURE); } - if (pid > 0) { - exit(EXIT_SUCCESS); + if (pid_slave > 0) { + // kill the father + exit(EXIT_SUCCESS); } - umask(0); + umask(0022); - // new SID for the child process - sid = setsid(); - if (sid < 0) { + // new sid_slave for the child process + sid_slave = setsid(); + if (sid_slave < 0) { exit(EXIT_FAILURE); } @@ -90,17 +131,48 @@ void go_daemon(void (*function)()) exit(EXIT_FAILURE); } + + /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } + + int current_pid = getpid(); + + if (read_pid() == -1){ + if (verbose){ + printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid); + syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); + } + if (write_pid(current_pid) == 0){ + syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); + if (verbose){ + printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid); + } + exit(EXIT_FAILURE); + }else{ + if (verbose){ + printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + } + } + }else{ + syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); + if (verbose){ + printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); + } + exit(EXIT_FAILURE); + } + - function(); + mbpfan(); - if(daemonize) - syslog(LOG_INFO, "%s daemon exiting", DAEMON_NAME); + if(daemonize){ + syslog(LOG_INFO, "%s daemon exiting", program_name); + } return; } diff --git a/src/daemon.h b/src/daemon.h index 2e2d30d..e46bff1 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -1,8 +1,49 @@ +/** + * Copyright (C) (2012) Daniel Graziotin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + + +/** + * Write the PID of the forked daemon to the + * .pid file defined in char *program_pid + * Return TRUE on success + * Return FALSE otherwise + */ +int write_pid(int pid); + +/** + * Read PID of the forked daemon from the + * .pid file defined in char *program_pid + * Return the pid on success + * Return -1 otherwise + */ +int read_pid(); + +/** + * Deletes the .pid file defined in + * char *program_pid + * Return TRUE on success + * Return FALSE otherwise + */ + int delete_pid(); + /** * ...handles signals :-) */ void signal_handler(int signal); + /** * Daemonizes */ diff --git a/src/global.h b/src/global.h index 910ed89..e1dd842 100644 --- a/src/global.h +++ b/src/global.h @@ -1,2 +1,5 @@ extern int daemonize; -extern int verbose; \ No newline at end of file +extern int verbose; + +extern const char* program_name; +extern const char* program_pid; \ No newline at end of file diff --git a/src/main.c b/src/main.c index 693efcd..ba2880e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,18 @@ +/** + * Copyright (C) (2012) Daniel Graziotin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + #include #include #include @@ -8,6 +23,9 @@ int daemonize = 1; int verbose = 0; +const char *program_name = "mbpfan"; +const char *program_pid = "/var/run/mbpfan.pid"; + void print_usage(int argc, char *argv[]) { if (argc >=1) { @@ -44,7 +62,7 @@ int main(int argc, char *argv[]) } // pointer to mbpfan() function in mbpfan.c - void (*function)() = mbpfan; - go_daemon(function); + void (*mbpfan)() = mbpfan; + go_daemon(mbpfan); exit(0); } \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index 419cc1c..91f39a9 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -18,7 +18,7 @@ * * Notes: * Assumes any number of processors and fans (max. 10) - * It Uses only the temperatures from the processors as input. + * It uses only the temperatures from the processors as input. * Requires coretemp and applesmc kernel modules to be loaded. * Requires root use * diff --git a/src/mbpfan.h b/src/mbpfan.h index 20bcfe2..80bf96c 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -1,3 +1,19 @@ +/** + * Copyright (C) 2010 Allan McRae + * Modifications (2012) by Daniel Graziotin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + /** Basic fan speed parameters */ extern int min_fan_speed; From 3551dcc3c41ab53a3c33d5c8186395c185c0e81b Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 15 Jun 2012 21:12:51 +0200 Subject: [PATCH 008/235] Fixes #7 --- Makefile | 3 +++ README.md | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0ed28a1..7d38d07 100644 --- a/Makefile +++ b/Makefile @@ -52,5 +52,8 @@ all: $(OBJS) clean: rm -rf $(SOURCE_PATH)*.$(OBJ) $(EXE) +install: + cp $(EXE) /usr/sbin + rebuild: clean all #rebuild is not entirely correct diff --git a/README.md b/README.md index 538a7ab..f230a2e 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,20 @@ Compile Instructions --------------------- Compile with -make + make Manually compile with -gcc -o bin/mbpfan src/mbpfan.c -lm + gcc -o bin/mbpfan src/mbpfan.c -lm +Install Instructions +-------------------- + +Install with + make install + +It actually copies mbpfan to /usr/sbin, + Run Instructions --------------------- From a6cc7a45b68794153ca28b546274e318be02a534 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 15 Jun 2012 21:47:23 +0200 Subject: [PATCH 009/235] (sort of) fixes #8 --- Makefile | 7 +++++++ README.md | 19 ++++++++++++++++--- src/daemon.c | 4 ++-- src/main.c | 4 ++-- src/mbpfan.c | 2 +- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 7d38d07..dcbbdf5 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,13 @@ clean: install: cp $(EXE) /usr/sbin + @echo "-----------------------------------------------------------------------------" + @echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure)" + @echo "Is located in the main folder of the source files. It is called mbpfan.init." + @echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)" + @echo "and move it to /etc/init.d" + @echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults" + @echo "-----------------------------------------------------------------------------" rebuild: clean all #rebuild is not entirely correct diff --git a/README.md b/README.md index f230a2e..82893f4 100644 --- a/README.md +++ b/README.md @@ -29,16 +29,29 @@ Install Instructions -------------------- Install with - make install + sudo make install + +It actually copies mbpfan to /usr/sbin. + +An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure) +Is located in the main folder of the source files. It is called mbpfan.init. +Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) +and move it to /etc/init.d +Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults (Ubuntu example) -It actually copies mbpfan to /usr/sbin, Run Instructions --------------------- -Run with +If not installed, run with sudo bin/mbpfan +If installed, manually run with +sudo mbpfan + +If installed and using the init file, run with (Ubuntu example) +sudo service mbpfan start + License --------------------- diff --git a/src/daemon.c b/src/daemon.c index a9b7fa1..c6a5a3f 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -85,7 +85,7 @@ void signal_handler(int signal) } } -void go_daemon(void (*mbpfan)()) +void go_daemon(void (*fan_control)()) { // Setup signal handling before we start @@ -168,7 +168,7 @@ void go_daemon(void (*mbpfan)()) } - mbpfan(); + fan_control(); if(daemonize){ syslog(LOG_INFO, "%s daemon exiting", program_name); diff --git a/src/main.c b/src/main.c index ba2880e..6750b18 100644 --- a/src/main.c +++ b/src/main.c @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) } // pointer to mbpfan() function in mbpfan.c - void (*mbpfan)() = mbpfan; - go_daemon(mbpfan); + void (*fan_control)() = mbpfan; + go_daemon(fan_control); exit(0); } \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index 91f39a9..de1ea9d 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -52,7 +52,7 @@ int low_temp = 63; // try ranges 55-63 int high_temp = 66; // try ranges 58-66 int max_temp = 86; // do not set it > 90 -int polling_interval = 5; +int polling_interval = 7; struct s_sensors { From 10788141337013cfa7cc46dcb7dd52eb3b0dcd3e Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 16 Jun 2012 11:02:48 +0200 Subject: [PATCH 010/235] Fixes #10 --- README.md | 5 + src/daemon.c | 255 ++++++++++++++++-------------- src/daemon.h | 2 +- src/main.c | 73 +++++---- src/mbpfan.c | 428 +++++++++++++++++++++++++++----------------------- src/mbpfan.h | 5 + src/minunit.c | 120 ++++++++++++++ src/minunit.h | 16 ++ 8 files changed, 559 insertions(+), 345 deletions(-) create mode 100644 src/minunit.c create mode 100644 src/minunit.h diff --git a/README.md b/README.md index 82893f4..34ac20d 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,11 @@ Compile with Manually compile with gcc -o bin/mbpfan src/mbpfan.c -lm +Run The Tests (Recommended) +--------------------------- + +It is recommended to run the tests before installing the program + sudo ./bin/mbpfan -t Install Instructions -------------------- diff --git a/src/daemon.c b/src/daemon.c index c6a5a3f..3909127 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -29,150 +29,177 @@ #include "mbpfan.h" #include "global.h" -int write_pid(int pid){ - FILE *file = NULL; - file = fopen(program_pid, "w"); - if(file != NULL) { - fprintf(file, "%d", pid); - fclose(file); - return 1; - }else{ - return 0; - } +int write_pid(int pid) +{ + FILE *file = NULL; + file = fopen(program_pid, "w"); + if(file != NULL) + { + fprintf(file, "%d", pid); + fclose(file); + return 1; + } + else + { + return 0; + } } -int read_pid(){ - FILE *file = NULL; - int pid = -1; - file = fopen(program_pid, "r"); - if(file != NULL) { - fscanf(file, "%d", &pid); - fclose(file); - return pid; - } - return -1; +int read_pid() +{ + FILE *file = NULL; + int pid = -1; + file = fopen(program_pid, "r"); + if(file != NULL) + { + fscanf(file, "%d", &pid); + fclose(file); + return pid; + } + return -1; } -int delete_pid(){ - return remove(program_pid); +int delete_pid() +{ + return remove(program_pid); } void signal_handler(int signal) { - switch(signal) { - case SIGHUP: - //TODO: restart myself - syslog(LOG_WARNING, "Received SIGHUP signal."); - delete_pid(); - exit(0); - break; - case SIGTERM: - syslog(LOG_WARNING, "Received SIGTERM signal."); - delete_pid(); - //TODO: free resources - exit(0); - break; - case SIGINT: - syslog(LOG_WARNING, "Received SIGINT signal."); - delete_pid(); - //TODO: free resources - exit(0); - default: - syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); - break; - } + switch(signal) + { + case SIGHUP: + //TODO: restart myself + syslog(LOG_WARNING, "Received SIGHUP signal."); + delete_pid(); + exit(0); + break; + case SIGTERM: + syslog(LOG_WARNING, "Received SIGTERM signal."); + delete_pid(); + //TODO: free resources + exit(0); + break; + case SIGINT: + syslog(LOG_WARNING, "Received SIGINT signal."); + delete_pid(); + //TODO: free resources + exit(0); + default: + syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); + break; + } } void go_daemon(void (*fan_control)()) { - // Setup signal handling before we start - signal(SIGHUP, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGINT, signal_handler); + // Setup signal handling before we start + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); - syslog(LOG_INFO, "%s starting up", program_name); + syslog(LOG_INFO, "%s starting up", program_name); - // Setup syslog logging - see SETLOGMASK(3) - if(verbose) { - setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); - } else { - setlogmask(LOG_UPTO(LOG_INFO)); - openlog(program_name, LOG_CONS, LOG_USER); - } + // Setup syslog logging - see SETLOGMASK(3) + if(verbose) + { + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + } + else + { + setlogmask(LOG_UPTO(LOG_INFO)); + openlog(program_name, LOG_CONS, LOG_USER); + } - pid_t pid_slave; - pid_t sid_slave; + pid_t pid_slave; + pid_t sid_slave; - if (daemonize) { + if (daemonize) + { - pid_slave = fork(); - if (pid_slave < 0) { - exit(EXIT_FAILURE); - } - if (pid_slave > 0) { - // kill the father - exit(EXIT_SUCCESS); - } + pid_slave = fork(); + if (pid_slave < 0) + { + exit(EXIT_FAILURE); + } + if (pid_slave > 0) + { + // kill the father + exit(EXIT_SUCCESS); + } - umask(0022); + umask(0022); - // new sid_slave for the child process - sid_slave = setsid(); - if (sid_slave < 0) { - exit(EXIT_FAILURE); - } + // new sid_slave for the child process + sid_slave = setsid(); + if (sid_slave < 0) + { + exit(EXIT_FAILURE); + } - if ((chdir("/")) < 0) { - exit(EXIT_FAILURE); - } + if ((chdir("/")) < 0) + { + exit(EXIT_FAILURE); + } + + + + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + } + int current_pid = getpid(); - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); + if (read_pid() == -1) + { + if (verbose) + { + printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid); + syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); } - - - int current_pid = getpid(); - - if (read_pid() == -1){ - if (verbose){ - printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid); - syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); - } - if (write_pid(current_pid) == 0){ - syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); - if (verbose){ - printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid); - } - exit(EXIT_FAILURE); - }else{ - if (verbose){ - printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); - syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); - } - } - }else{ - syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); - if (verbose){ - printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); - } - exit(EXIT_FAILURE); + if (write_pid(current_pid) == 0) + { + syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); + if (verbose) + { + printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid); + } + exit(EXIT_FAILURE); } - + else + { + if (verbose) + { + printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + } + } + } + else + { + syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); + if (verbose) + { + printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); + } + exit(EXIT_FAILURE); + } - fan_control(); - if(daemonize){ - syslog(LOG_INFO, "%s daemon exiting", program_name); - } + fan_control(); + + if(daemonize) + { + syslog(LOG_INFO, "%s daemon exiting", program_name); + } - return; + return; } diff --git a/src/daemon.h b/src/daemon.h index e46bff1..0f76787 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -36,7 +36,7 @@ int read_pid(); * Return TRUE on success * Return FALSE otherwise */ - int delete_pid(); +int delete_pid(); /** * ...handles signals :-) diff --git a/src/main.c b/src/main.c index 6750b18..2c2358a 100644 --- a/src/main.c +++ b/src/main.c @@ -12,13 +12,14 @@ * GNU General Public License for more details. * */ - + #include #include #include #include "mbpfan.h" #include "daemon.h" #include "global.h" +#include "minunit.h" int daemonize = 1; int verbose = 0; @@ -28,41 +29,51 @@ const char *program_pid = "/var/run/mbpfan.pid"; void print_usage(int argc, char *argv[]) { - if (argc >=1) { - printf("Usage: %s OPTION(S) \n", argv[0]); - printf("Options:\n"); - printf("\t-h Show this help screen\n"); - printf("\t-f Run in foreground\n"); - printf("\t-v Be (a lot) verbose\n"); - printf("\n"); - } + if (argc >=1) + { + printf("Usage: %s OPTION(S) \n", argv[0]); + printf("Options:\n"); + printf("\t-h Show this help screen\n"); + printf("\t-f Run in foreground\n"); + printf("\t-t Run the tests\n"); + printf("\t-v Be (a lot) verbose\n"); + printf("\n"); + } } + + int main(int argc, char *argv[]) { - int c; - while( (c = getopt(argc, argv, "hfv|help")) != -1) { - switch(c) { - case 'h': - print_usage(argc, argv); - exit(0); - break; - case 'f': - daemonize = 0; - break; - case 'v': - verbose = 1; - break; - default: - print_usage(argc, argv); - exit(0); - break; - } + int c; + while( (c = getopt(argc, argv, "hftv|help")) != -1) + { + switch(c) + { + case 'h': + print_usage(argc, argv); + exit(0); + break; + case 'f': + daemonize = 0; + break; + case 't': + tests(); + exit(0); + break; + case 'v': + verbose = 1; + break; + default: + print_usage(argc, argv); + exit(0); + break; } + } - // pointer to mbpfan() function in mbpfan.c - void (*fan_control)() = mbpfan; - go_daemon(fan_control); - exit(0); + // pointer to mbpfan() function in mbpfan.c + void (*fan_control)() = mbpfan; + go_daemon(fan_control); + exit(0); } \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index de1ea9d..7155b79 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -55,161 +55,177 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; -struct s_sensors { - char* path; - char* fan_min_path; - char* fan_man_path; - unsigned int temperature; - struct s_sensors *next; +struct s_sensors +{ + char* path; + char* fan_min_path; + char* fan_man_path; + unsigned int temperature; + struct s_sensors *next; }; t_sensors *retrieve_sensors() { - t_sensors *sensors_head = NULL; - t_sensors *s = NULL; - - char *path = NULL; - const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; - const char *path_end = "_input"; - - int path_size = strlen(path_begin) + strlen(path_end) + 2; - char number[1]; - sprintf(number,"%d",0); - - int i = 0; - for(i = 0; i<10; i++) { - path = (char*) malloc(sizeof( char ) * path_size); - - sprintf(number,"%d",i); - path[0] = '\0'; - strncat( path, path_begin, strlen(path_begin) ); - strncat( path, number, strlen(number) ); - strncat( path, path_end, strlen(path_begin) ); - - FILE *file = fopen(path, "r"); - - if(file != NULL) { - s = (t_sensors *) malloc( sizeof( t_sensors ) ); - s->path = (char *) malloc(sizeof( char ) * path_size); - strcpy(s->path, path); - fscanf(file, "%d", &s->temperature); - if (sensors_head == NULL) { - sensors_head = s; - sensors_head->next = NULL; - } else { - t_sensors *tmp = sensors_head; - while (tmp->next != NULL) { - tmp = tmp->next; - } - tmp->next = s; - tmp->next->next = NULL; - } - fclose(file); + t_sensors *sensors_head = NULL; + t_sensors *s = NULL; + + char *path = NULL; + const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; + const char *path_end = "_input"; + + int path_size = strlen(path_begin) + strlen(path_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int i = 0; + for(i = 0; i<10; i++) + { + path = (char*) malloc(sizeof( char ) * path_size); + + sprintf(number,"%d",i); + path[0] = '\0'; + strncat( path, path_begin, strlen(path_begin) ); + strncat( path, number, strlen(number) ); + strncat( path, path_end, strlen(path_begin) ); + + FILE *file = fopen(path, "r"); + + if(file != NULL) + { + s = (t_sensors *) malloc( sizeof( t_sensors ) ); + s->path = (char *) malloc(sizeof( char ) * path_size); + strcpy(s->path, path); + fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) + { + sensors_head = s; + sensors_head->next = NULL; + } + else + { + t_sensors *tmp = sensors_head; + while (tmp->next != NULL) + { + tmp = tmp->next; } - free(path); - path = NULL; + tmp->next = s; + tmp->next->next = NULL; + } + fclose(file); } - if(sensors_head != NULL) - find_fans(sensors_head); - return sensors_head; + free(path); + path = NULL; + } + if(sensors_head != NULL) + find_fans(sensors_head); + return sensors_head; } void find_fans(t_sensors* sensors) { - t_sensors *tmp = sensors; - - char *path_min = NULL; - char *path_man = NULL; - - const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; - const char *path_min_end = "_min"; - const char *path_man_end = "_manual"; - - int path_min_size = strlen(path_begin) + strlen(path_min_end) + 2; - int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; - char number[1]; - sprintf(number,"%d",0); - - int n_sensors = 0; - int n_fans = 0; - - for(n_sensors = 0; n_sensors<10; n_sensors++) { - path_min = (char*) malloc(sizeof( char ) * path_min_size); - path_min[0] = '\0'; - path_man = (char*) malloc(sizeof( char ) * path_man_size); - path_man[0] = '\0'; - sprintf(number,"%d",n_sensors); - - strncat( path_min, path_begin, strlen(path_begin) ); - strncat( path_min, number, strlen(number) ); - strncat( path_min, path_min_end, strlen(path_begin) ); - - strncat( path_man, path_begin, strlen(path_begin) ); - strncat( path_man, number, strlen(number) ); - strncat( path_man, path_man_end, strlen(path_begin) ); - - - FILE *file = fopen(path_min, "r"); - - if(file != NULL) { - if (tmp->path != NULL) { - tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); - tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); - } - strcpy(tmp->fan_min_path, path_min); - strcpy(tmp->fan_man_path, path_man); - tmp = tmp->next; - n_fans++; - fclose(file); - } + t_sensors *tmp = sensors; + + char *path_min = NULL; + char *path_man = NULL; + + const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_min_end = "_min"; + const char *path_man_end = "_manual"; + + int path_min_size = strlen(path_begin) + strlen(path_min_end) + 2; + int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int n_sensors = 0; + int n_fans = 0; + + for(n_sensors = 0; n_sensors<10; n_sensors++) + { + path_min = (char*) malloc(sizeof( char ) * path_min_size); + path_min[0] = '\0'; + path_man = (char*) malloc(sizeof( char ) * path_man_size); + path_man[0] = '\0'; + sprintf(number,"%d",n_sensors); + + strncat( path_min, path_begin, strlen(path_begin) ); + strncat( path_min, number, strlen(number) ); + strncat( path_min, path_min_end, strlen(path_begin) ); + + strncat( path_man, path_begin, strlen(path_begin) ); + strncat( path_man, number, strlen(number) ); + strncat( path_man, path_man_end, strlen(path_begin) ); + + + FILE *file = fopen(path_min, "r"); + + if(file != NULL) + { + if (tmp->path != NULL) + { + tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); + tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); + } + strcpy(tmp->fan_min_path, path_min); + strcpy(tmp->fan_man_path, path_man); + tmp = tmp->next; + n_fans++; + fclose(file); } - - if(verbose) { - printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); - if(daemonize) { - syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); - } + } + + if(verbose) + { + printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); + if(daemonize) + { + syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); } + } - free(path_min); - path_min = NULL; - free(path_man); - path_man = NULL; + free(path_min); + path_min = NULL; + free(path_man); + path_man = NULL; } void set_fans_man(t_sensors *sensors) { - t_sensors *tmp = sensors; - FILE *file; - while(tmp != NULL) { - file = fopen(tmp->fan_man_path, "rw+"); - if(file != NULL) { - fprintf(file, "%d", 0); - fclose(file); - } - tmp = tmp->next; + t_sensors *tmp = sensors; + FILE *file; + while(tmp != NULL) + { + file = fopen(tmp->fan_man_path, "rw+"); + if(file != NULL) + { + fprintf(file, "%d", 0); + fclose(file); } + tmp = tmp->next; + } } t_sensors *refresh_sensors(t_sensors *sensors) { - t_sensors *tmp = sensors; - - while(tmp != NULL) { - FILE *file = fopen(tmp->path, "r"); + t_sensors *tmp = sensors; - if(file != NULL) { - fscanf(file, "%d", &tmp->temperature); - fclose(file); - } + while(tmp != NULL) + { + FILE *file = fopen(tmp->path, "r"); - tmp = tmp->next; + if(file != NULL) + { + fscanf(file, "%d", &tmp->temperature); + fclose(file); } - return sensors; + + tmp = tmp->next; + } + return sensors; } @@ -217,18 +233,20 @@ t_sensors *refresh_sensors(t_sensors *sensors) /* Controls the speed of the fan */ void set_fan_speed(t_sensors* sensors, int speed) { - t_sensors *tmp = sensors; - FILE *file; - while(tmp != NULL) { - file = fopen(tmp->fan_min_path, "rw+"); - if(file != NULL) { - fprintf(file, "%d", speed); - fclose(file); - } - - tmp = tmp->next; + t_sensors *tmp = sensors; + FILE *file; + while(tmp != NULL) + { + file = fopen(tmp->fan_min_path, "rw+"); + if(file != NULL) + { + fprintf(file, "%d", speed); + fclose(file); } + tmp = tmp->next; + } + } @@ -236,85 +254,97 @@ void set_fan_speed(t_sensors* sensors, int speed) /* Returns average CPU temp in degrees (ceiling) */ unsigned short get_temp(t_sensors* sensors) { - sensors = refresh_sensors(sensors); - int sum_temp = 0; - unsigned short temp = 0; - - t_sensors* tmp = sensors; - while(tmp != NULL) { - sum_temp += tmp->temperature; - tmp = tmp->next; - } - temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); - return temp; + sensors = refresh_sensors(sensors); + int sum_temp = 0; + unsigned short temp = 0; + + t_sensors* tmp = sensors; + while(tmp != NULL) + { + sum_temp += tmp->temperature; + tmp = tmp->next; + } + temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); + return temp; } void mbpfan() { - int old_temp, new_temp, fan_speed, steps; - int temp_change; - int step_up, step_down; - - t_sensors* sensors = retrieve_sensors(); - set_fans_man(sensors); - new_temp = get_temp(sensors); - fan_speed = 2000; - set_fan_speed(sensors, fan_speed); - - if(verbose) { - printf("Sleeping for %d seconds\n", polling_interval); - if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); - } + int old_temp, new_temp, fan_speed, steps; + int temp_change; + int step_up, step_down; + + t_sensors* sensors = retrieve_sensors(); + set_fans_man(sensors); + new_temp = get_temp(sensors); + fan_speed = 2000; + set_fan_speed(sensors, fan_speed); + + if(verbose) + { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) + { + syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); } - sleep(polling_interval); + } + sleep(polling_interval); - step_up = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); + step_up = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); - step_down = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); + step_down = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); - while(1) { - old_temp = new_temp; - new_temp = get_temp(sensors); + while(1) + { + old_temp = new_temp; + new_temp = get_temp(sensors); - if(new_temp >= max_temp && fan_speed != max_fan_speed) { - fan_speed = max_fan_speed; - } + if(new_temp >= max_temp && fan_speed != max_fan_speed) + { + fan_speed = max_fan_speed; + } - if(new_temp <= low_temp && fan_speed != min_fan_speed) { - fan_speed = min_fan_speed; - } + if(new_temp <= low_temp && fan_speed != min_fan_speed) + { + fan_speed = min_fan_speed; + } - temp_change = new_temp - old_temp; + temp_change = new_temp - old_temp; - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { - steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; - fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); - } + if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) + { + steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; + fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); + } - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { - steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; - fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); - } + if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) + { + steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; + fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); + } - if(verbose) { - printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); - if(daemonize) { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); - } - } + if(verbose) + { + printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(daemonize) + { + syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + } + } - set_fan_speed(sensors, fan_speed); + set_fan_speed(sensors, fan_speed); - if(verbose) { - printf("Sleeping for %d seconds\n", polling_interval); - if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); - } - } - sleep(polling_interval); + if(verbose) + { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) + { + syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); + } } + sleep(polling_interval); + } } diff --git a/src/mbpfan.h b/src/mbpfan.h index 80bf96c..71ab6ce 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -61,6 +61,11 @@ void find_fans(t_sensors *sensors); */ void set_fans_man(t_sensors *sensors); +/** + * Return average CPU temp in degrees (ceiling) + */ +unsigned short get_temp(t_sensors* sensors); + /** * Main Program */ diff --git a/src/minunit.c b/src/minunit.c new file mode 100644 index 0000000..8dab70b --- /dev/null +++ b/src/minunit.c @@ -0,0 +1,120 @@ +/* file minunit_example.c */ +#include +#include +#include +#include +#include "mbpfan.h" +#include "minunit.h" + +int tests_run = 0; + + +struct s_sensors +{ + char* path; + char* fan_min_path; + char* fan_man_path; + unsigned int temperature; + struct s_sensors *next; +}; +typedef s_sensors t_sensors; + + +static char *test_sensor_paths() +{ + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + t_sensors* tmp = sensors; + while(tmp != NULL) + { + mu_assert("Sensor does not have a valid path", tmp->path != NULL); + if(tmp->path != NULL) + mu_assert("Sensor does not have valid temperature", tmp->temperature > 0); + tmp = tmp->next; + } + return 0; +} + + +static char *test_fan_paths() +{ + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + t_sensors* tmp = sensors; + int found_fan_path = 0; + while(tmp != NULL) + { + if(tmp->fan_min_path != NULL) + found_fan_path++; + tmp = tmp->next; + } + mu_assert("No fans found", found_fan_path != 0); + return 0; +} + +unsigned time_seed() +{ + time_t now = time ( 0 ); + unsigned char *p = (unsigned char *)&now; + unsigned seed = 0; + size_t i; + for ( i = 0; i < sizeof now; i++ ) + seed = seed * ( UCHAR_MAX + 2U ) + p[i]; + return seed; +} + +// nothing better than a horrible piece of code to +// stress a little bit the CPU +int stress(int n) +{ + int f = n; + while (f > 0) + { + while(n > 0) + { + srand ( time_seed() ); + n--; + } + f--; + n = f; + } +} + +static char *test_get_temp() +{ + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + unsigned short temp_1 = get_temp(sensors); + mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150); + stress(2000); + unsigned short temp_2 = get_temp(sensors); + mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2); + return 0; +} + + +static char *all_tests() +{ + mu_run_test(test_sensor_paths); + mu_run_test(test_fan_paths); + mu_run_test(test_get_temp); + return 0; +} + +int tests() +{ + printf("Starting the tests..\n"); + printf("It is normal for them to take a bit to finish.\n"); + char *result = all_tests(); + if (result != 0) + { + printf("%s \n", result); + } + else + { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return result != 0; +} \ No newline at end of file diff --git a/src/minunit.h b/src/minunit.h new file mode 100644 index 0000000..d9dc93d --- /dev/null +++ b/src/minunit.h @@ -0,0 +1,16 @@ +/** + * This is the MinUnit testing framework - http://www.jera.com/techinfo/jtns/jtn002.html + */ +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(test) do { char *message = test(); tests_run++; \ + if (message) return message; } while (0) + +extern int tests_run; + +static char *test_sensor_paths(); +static char *test_fan_paths(); +static char *test_get_temp(); + +static char *all_tests(); + +int tests(); \ No newline at end of file From 202ebf2af5300530c3605f971dcde1bdc2fe6c4a Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 3 Jul 2012 09:37:44 +0200 Subject: [PATCH 011/235] Cleanup --- src/mbpfan.c | 1 - src/mbpfan.h | 11 ++++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 7155b79..ea72fef 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -251,7 +251,6 @@ void set_fan_speed(t_sensors* sensors, int speed) -/* Returns average CPU temp in degrees (ceiling) */ unsigned short get_temp(t_sensors* sensors) { sensors = refresh_sensors(sensors); diff --git a/src/mbpfan.h b/src/mbpfan.h index 71ab6ce..ce5afca 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -61,6 +61,12 @@ void find_fans(t_sensors *sensors); */ void set_fans_man(t_sensors *sensors); +/** + * Given a list of sensors with associated fans + * Change their speed + */ +void set_fan_speed(t_sensors* sensors, int speed) + /** * Return average CPU temp in degrees (ceiling) */ @@ -69,4 +75,7 @@ unsigned short get_temp(t_sensors* sensors); /** * Main Program */ -void mbpfan(); \ No newline at end of file +void mbpfan(); + + + From ccfed511aac096273c043be94cb71d5d2f1fae03 Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Tue, 3 Jul 2012 02:32:00 +0200 Subject: [PATCH 012/235] Added mbpfan.init.fedora for Fedora SysV-Init scripts --- mbpfan.init.fedora | 101 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 mbpfan.init.fedora diff --git a/mbpfan.init.fedora b/mbpfan.init.fedora new file mode 100755 index 0000000..1c77c8d --- /dev/null +++ b/mbpfan.init.fedora @@ -0,0 +1,101 @@ +#!/bin/sh +# +# mbpfan Start the mbpfan daemon +# +# chkconfig: 2 3 4 5 15 85 +# description: Start the mbpfan daemon + +### BEGIN INIT INFO +# Provides: mbpfan +# Required-Start: $remote_fs +# Required-Stop: $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: mbpfan initscript +# Description: Start the mbpfan daemon +### END INIT INFO + +# Source function library. +. /etc/rc.d/init.d/functions + +exec="/usr/sbin/mbpfan" +prog="mbpfan" + +[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog + +lockfile=/var/lock/subsys/$prog + +start() { + [ -x $exec ] || exit 5 + echo -n $"Starting $prog: " + daemon $exec + retval=$? + echo + [ $retval -eq 0 ] && touch $lockfile + return $retval +} + +stop() { + echo -n $"Stopping $prog: " + killproc $prog + retval=$? + echo + [ $retval -eq 0 ] && rm -f $lockfile + return $retval +} + +restart() { + stop + start +} + +reload() { + restart +} + +force_reload() { + restart +} + +rh_status() { + # run checks to determine if the service is running or use generic status + status $prog +} + +rh_status_q() { + rh_status >/dev/null 2>&1 +} + + +case "$1" in + start) + rh_status_q && exit 0 + $1 + ;; + stop) + rh_status_q || exit 0 + $1 + ;; + restart) + $1 + ;; + reload) + rh_status_q || exit 7 + $1 + ;; + force-reload) + force_reload + ;; + status) + rh_status + ;; + condrestart|try-restart) + rh_status_q || exit 0 + restart + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" + exit 2 +esac +exit $? + From de12aa9291ba03f477e5706490eb7a4c958dfd55 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 3 Jul 2012 09:58:38 +0200 Subject: [PATCH 013/235] Merged pull request 11, added authors, and README entry for Fedora --- AUTHORS | 12 ++++++++++++ README.md | 4 +++- mbpfan.init => mbpfan.init.debian | 0 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 AUTHORS rename mbpfan.init => mbpfan.init.debian (100%) diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..98414f3 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,12 @@ +MAINTAINERS AND CONTRIBUTORS +---------------------------- + +Daniel Graziotin +CeRiAl + + +ORIGINARY AUTHORS +----------------- + +Allan McRae mbpfan +rvega \ No newline at end of file diff --git a/README.md b/README.md index 34ac20d..fe39614 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,13 @@ Install with It actually copies mbpfan to /usr/sbin. An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure) -Is located in the main folder of the source files. It is called mbpfan.init. +is located in the main folder of the source files. It is called mbpfan.init.debian Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) and move it to /etc/init.d Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults (Ubuntu example) +An init file suitable for Fedora (and probably RedHat) can be found +in the file mbpfan.init.fedora Run Instructions --------------------- diff --git a/mbpfan.init b/mbpfan.init.debian similarity index 100% rename from mbpfan.init rename to mbpfan.init.debian From 4672a0fbfe1502abf25de467436d97ab79f561a5 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 3 Jul 2012 10:30:40 +0200 Subject: [PATCH 014/235] Fixes #12 --- src/mbpfan.c | 56 +++++++++++++++++++++++++-------------------------- src/mbpfan.h | 7 ++----- src/minunit.c | 6 +++--- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index ea72fef..56fdf32 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -58,8 +58,8 @@ int polling_interval = 7; struct s_sensors { char* path; - char* fan_min_path; - char* fan_man_path; + char* fan_output_path; + char* fan_manual_path; unsigned int temperature; struct s_sensors *next; }; @@ -127,14 +127,14 @@ void find_fans(t_sensors* sensors) { t_sensors *tmp = sensors; - char *path_min = NULL; - char *path_man = NULL; + char *path_output = NULL; + char *path_manual = NULL; const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; - const char *path_min_end = "_min"; + const char *path_output_end = "_output"; const char *path_man_end = "_manual"; - int path_min_size = strlen(path_begin) + strlen(path_min_end) + 2; + int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; char number[1]; sprintf(number,"%d",0); @@ -144,32 +144,32 @@ void find_fans(t_sensors* sensors) for(n_sensors = 0; n_sensors<10; n_sensors++) { - path_min = (char*) malloc(sizeof( char ) * path_min_size); - path_min[0] = '\0'; - path_man = (char*) malloc(sizeof( char ) * path_man_size); - path_man[0] = '\0'; + path_output = (char*) malloc(sizeof( char ) * path_min_size); + path_output[0] = '\0'; + path_manual = (char*) malloc(sizeof( char ) * path_man_size); + path_manual[0] = '\0'; sprintf(number,"%d",n_sensors); - strncat( path_min, path_begin, strlen(path_begin) ); - strncat( path_min, number, strlen(number) ); - strncat( path_min, path_min_end, strlen(path_begin) ); + strncat( path_output, path_begin, strlen(path_begin) ); + strncat( path_output, number, strlen(number) ); + strncat( path_output, path_output_end, strlen(path_begin) ); - strncat( path_man, path_begin, strlen(path_begin) ); - strncat( path_man, number, strlen(number) ); - strncat( path_man, path_man_end, strlen(path_begin) ); + strncat( path_manual, path_begin, strlen(path_begin) ); + strncat( path_manual, number, strlen(number) ); + strncat( path_manual, path_man_end, strlen(path_begin) ); - FILE *file = fopen(path_min, "r"); + FILE *file = fopen(path_output, "r"); if(file != NULL) { if (tmp->path != NULL) { - tmp->fan_min_path = (char *) malloc(sizeof( char ) * path_min_size); - tmp->fan_man_path = (char *) malloc(sizeof( char ) * path_man_size); + tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); + tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); } - strcpy(tmp->fan_min_path, path_min); - strcpy(tmp->fan_man_path, path_man); + strcpy(tmp->fan_output_path, path_output); + strcpy(tmp->fan_manual_path, path_manual); tmp = tmp->next; n_fans++; fclose(file); @@ -185,10 +185,10 @@ void find_fans(t_sensors* sensors) } } - free(path_min); - path_min = NULL; - free(path_man); - path_man = NULL; + free(path_output); + path_output = NULL; + free(path_manual); + path_manual = NULL; } void set_fans_man(t_sensors *sensors) @@ -198,10 +198,10 @@ void set_fans_man(t_sensors *sensors) FILE *file; while(tmp != NULL) { - file = fopen(tmp->fan_man_path, "rw+"); + file = fopen(tmp->fan_manual_path, "rw+"); if(file != NULL) { - fprintf(file, "%d", 0); + fprintf(file, "%d", 1); fclose(file); } tmp = tmp->next; @@ -237,7 +237,7 @@ void set_fan_speed(t_sensors* sensors, int speed) FILE *file; while(tmp != NULL) { - file = fopen(tmp->fan_min_path, "rw+"); + file = fopen(tmp->fan_output_path, "rw+"); if(file != NULL) { fprintf(file, "%d", speed); diff --git a/src/mbpfan.h b/src/mbpfan.h index ce5afca..97ba7c2 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -65,7 +65,7 @@ void set_fans_man(t_sensors *sensors); * Given a list of sensors with associated fans * Change their speed */ -void set_fan_speed(t_sensors* sensors, int speed) +void set_fan_speed(t_sensors* sensors, int speed); /** * Return average CPU temp in degrees (ceiling) @@ -75,7 +75,4 @@ unsigned short get_temp(t_sensors* sensors); /** * Main Program */ -void mbpfan(); - - - +void mbpfan(); \ No newline at end of file diff --git a/src/minunit.c b/src/minunit.c index 8dab70b..fadaf97 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -12,8 +12,8 @@ int tests_run = 0; struct s_sensors { char* path; - char* fan_min_path; - char* fan_man_path; + char* fan_output_path; + char* fan_manual_path; unsigned int temperature; struct s_sensors *next; }; @@ -44,7 +44,7 @@ static char *test_fan_paths() int found_fan_path = 0; while(tmp != NULL) { - if(tmp->fan_min_path != NULL) + if(tmp->fan_output_path != NULL) found_fan_path++; tmp = tmp->next; } From e84d5e2e053b0d3d54158be90836fa9e4d30e9db Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 3 Jul 2012 10:39:30 +0200 Subject: [PATCH 015/235] Prepare for v1.3.0 --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index fe39614..9c6df88 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ This enhanced version assumes any number of processors and fans (max. 10). * It only uses the temperatures from the processors as input. * It requires coretemp and applesmc kernel modules to be loaded. * It requires root use +* It daemonizes or stays in foreground +* Verbose mode for both syslog and stdout Compile Instructions @@ -59,6 +61,15 @@ sudo mbpfan If installed and using the init file, run with (Ubuntu example) sudo service mbpfan start +Usage +------- + +Usage: ./mbpfan OPTION(S) +-h Show the help screen +-f Run in foreground +-t Run the tests +-v Be (a lot) verbose + License --------------------- From ef8cda3b2dcd3eec65ddacfdde150e34c75bdd05 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 3 Jul 2012 13:30:43 +0200 Subject: [PATCH 016/235] Adds creation of bin/ dir if not present --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index dcbbdf5..a67d557 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ endif OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) %.$(OBJ):%.$(C) + mkdir -p bin @echo Compiling $(basename $<)... $(CCMD) -c $(CPPFLAGS) $(CXXFLAGS) $< $(OBJFLAG)$@ From cd1a34c253726f3094c9d0cc2095494c5aee8115 Mon Sep 17 00:00:00 2001 From: Per Ola Kristensson Date: Wed, 4 Jul 2012 02:39:48 +0200 Subject: [PATCH 017/235] Added Per Ola Kristensson's ANSI C application settings management More information here: http://pokristensson.com/settings.html --- src/settings.c | 955 +++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 507 ++++++++++++++++++++++++++ src/strmap.c | 514 ++++++++++++++++++++++++++ src/strmap.h | 355 ++++++++++++++++++ 4 files changed, 2331 insertions(+) create mode 100644 src/settings.c create mode 100644 src/settings.h create mode 100644 src/strmap.c create mode 100644 src/strmap.h diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..75c43ce --- /dev/null +++ b/src/settings.c @@ -0,0 +1,955 @@ +/* + * settings version 1.0.1 + * + * ANSI C implementation for managing application settings. + * + * Version History: + * 1.0.0 (2009) - Initial release + * 1.0.1 (2010) - Fixed small memory leak in settings_delete + * (Thanks to Edwin van den Oetelaar) + * 1.0.2 (2011) - Adapted code for new strmap API + * + * settings.c + * + * Copyright (c) 2009-2011 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * settings is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * settings is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with settings. If not, see . + */ +#include "settings.h" + +#define MAX_SECTIONCHARS 256 +#define MAX_KEYCHARS 256 +#define MAX_VALUECHARS 256 +#define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10) + +#define COMMENT_CHAR '#' +#define SECTION_START_CHAR '[' +#define SECTION_END_CHAR ']' +#define KEY_VALUE_SEPARATOR_CHAR '=' + +#define DEFAULT_STRMAP_CAPACITY 256 + +typedef struct Section Section; +typedef struct ParseState ParseState; +typedef enum ConvertMode ConvertMode; + +struct Settings { + Section *sections; + unsigned int section_count; +}; + +struct Section { + char *name; + StrMap *map; +}; + +struct ParseState { + char *current_section; + unsigned int current_section_n; + int has_section; +}; + +enum ConvertMode { + CONVERT_MODE_INT, + CONVERT_MODE_LONG, + CONVERT_MODE_DOUBLE, +}; + +static void trim_str(const char *str, char *out_buf); +static int parse_str(Settings *settings, char *str, ParseState *parse_state); +static int is_blank_char(char c); +static int is_blank_str(const char *str); +static int is_comment_str(const char *str); +static int is_section_str(const char *str); +static int is_key_value_str(const char *str); +static int is_key_without_value_str(const char *str); +static const char * get_token(char *str, char delim, char **last); +static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n); +static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n); +static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n); +static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out); +static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out); +static Section * get_section(Section *sections, unsigned int n, const char *name); +static void enum_map(const char *key, const char *value, const void *obj); + +Settings * settings_new() +{ + Settings *settings; + + settings = malloc(sizeof(Settings)); + if (settings == NULL) { + return NULL; + } + settings->section_count = 0; + settings->sections = NULL; + return settings; +} + +void settings_delete(Settings *settings) +{ + unsigned int i, n; + Section *section; + + if (settings == NULL) { + return; + } + section = settings->sections; + n = settings->section_count; + i = 0; + while (i < n) { + sm_delete(section->map); + if (section->name != NULL) { + free(section->name); + } + section++; + i++; + } + free(settings->sections); + free(settings); +} + +Settings * settings_open(FILE *stream) +{ + Settings *settings; + char buf[MAX_LINECHARS]; + char trimmed_buf[MAX_LINECHARS]; + char section_buf[MAX_LINECHARS]; + ParseState parse_state; + + if (stream == NULL) { + return NULL; + } + settings = settings_new(); + if (settings == NULL) { + return NULL; + } + parse_state.current_section = section_buf; + parse_state.current_section_n = sizeof(section_buf); + parse_state.has_section = 0; + trim_str("", trimmed_buf); + while (fgets(buf, MAX_LINECHARS, stream) != NULL) { + trim_str(buf, trimmed_buf); + if (!parse_str(settings, trimmed_buf, &parse_state)) { + return NULL; + } + } + return settings; +} + +int settings_save(const Settings *settings, FILE *stream) +{ + unsigned int i, n; + Section *section; + char buf[MAX_LINECHARS]; + + if (settings == NULL) { + return 0; + } + if (stream == NULL) { + return 0; + } + section = settings->sections; + n = settings->section_count; + i = 0; + while (i < n) { + sprintf(buf, "[%s]\n", section->name); + fputs(buf, stream); + sm_enum(section->map, enum_map, stream); + section++; + i++; + fputs("\n", stream); + } + return 0; +} + +int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf) +{ + Section *s; + + if (settings == NULL) { + return 0; + } + s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { + return 0; + } + return sm_get(s->map, key, out_buf, n_out_buf); +} + +int settings_get_int(const Settings *settings, const char *section, const char *key) +{ + int i; + + if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { + return i; + } + return 0; +} + +long settings_get_long(const Settings *settings, const char *section, const char *key) +{ + long l; + + if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { + return l; + } + return 0; +} + +double settings_get_double(const Settings *settings, const char *section, const char *key) +{ + double d; + + if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { + return d; + } + return 0; +} + +int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out) +{ + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, out, n_out); +} + +long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out) +{ + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, out, n_out); +} + +double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out) +{ + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, out, n_out); +} + +int settings_set(Settings *settings, const char *section, const char *key, const char *value) +{ + Section *s; + + if (settings == NULL) { + return 0; + } + if (section == NULL || key == NULL || value == NULL) { + return 0; + } + if (strlen(section) == 0) { + return 0; + } + /* Get a pointer to the section */ + s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { + /* The section is not created---create it */ + s = realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + if (s == NULL) { + return 0; + } + settings->sections = s; + settings->section_count++; + s = &(settings->sections[settings->section_count - 1]); + s->map = sm_new(DEFAULT_STRMAP_CAPACITY); + if (s->map == NULL) { + free(s); + return 0; + } + s->name = malloc((strlen(section) + 1) * sizeof(char)); + if (s->name == NULL) { + sm_delete(s->map); + free(s); + return 0; + } + strcpy(s->name, section); + } + return sm_put(s->map, key, value); +} + +int settings_section_get_count(const Settings *settings, const char *section) +{ + Section *sect; + + if (settings == NULL) { + return 0; + } + sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { + return 0; + } + return sm_get_count(sect->map); +} + +int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj) +{ + Section *sect; + + sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { + return 0; + } + return sm_enum(sect->map, enum_func, obj); +} + +/* Copies a trimmed variant without leading and trailing blank characters + * of the input string into the output buffer. The output buffer is assumed + * to be large enough to contain the entire input string. + */ +static void trim_str(const char *str, char *out_buf) +{ + unsigned int len; + const char *s0; + + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + s0 = str; + len = 0; + while (*str != '\0') { + len++; + str++; + } + if (len > 0) { + str--; + } + while (is_blank_char(*str)) { + str--; + len--; + } + memcpy(out_buf, s0, len); + out_buf[len] = '\0'; +} + +/* Parses a single input string and updates the provided settings object. + * The given parse state may be updated following a call. It is assumed this + * function is called in repeated succession for each input line read. The + * provided parse state should be initialized to the following before this + * function is called for the first time for an intended parse: + * + * parse_state->current_section: a pre-allocated character buffer this function + * can read and write to + * parse_state->current_section_n: sizeof(parse_state->current_section) + * parse_state->has_section: 0 (false) + */ +static int parse_str(Settings *settings, char *str, ParseState *parse_state) +{ + char buf[MAX_LINECHARS]; + char buf1[MAX_LINECHARS]; + char buf2[MAX_LINECHARS]; + int result; + + if (*str == '\0') { + return 1; + } + else if (is_blank_str(str)) { + return 1; + } + else if (is_comment_str(str)) { + return 1; + } + else if (is_section_str(str)) { + result = get_section_from_str(str, buf, sizeof(buf)); + if (!result) { + return 0; + } + if (strlen(buf) + 1 > parse_state->current_section_n) { + return 0; + } + strcpy(parse_state->current_section, buf); + parse_state->has_section = 1; + return 1; + } + else if (is_key_value_str(str)) { + result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2)); + if (!result) { + return 0; + } + if (!parse_state->has_section) { + return 0; + } + return settings_set(settings, parse_state->current_section, buf1, buf2); + } + else if (is_key_without_value_str(str)) { + result = get_key_without_value_from_str(str, buf, sizeof(buf)); + if (!result) { + return 0; + } + if (!parse_state->has_section) { + return 0; + } + return settings_set(settings, parse_state->current_section, buf, ""); + } + else { + return 0; + } +} + +/* Returns true if the input character is blank, + * false otherwise. + */ +static int is_blank_char(char c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} + +/* Returns true if the input string is blank, + * false otherwise. + */ +static int is_blank_str(const char *str) +{ + while (*str != '\0') { + if (!is_blank_char(*str)) { + return 0; + } + str++; + } + return 1; +} + +/* Returns true if the input string denotes a comment, + * false otherwise. + */ +static int is_comment_str(const char *str) +{ + if (*str == COMMENT_CHAR) { + /* To be a comment the first character must be the + * comment character. + */ + return 1; + } + return 0; +} + +/* Returns true if the input string denotes a section name, + * false otherwise. + */ +static int is_section_str(const char *str) +{ + if (*str != SECTION_START_CHAR) { + /* The first character must be the section start character */ + return 0; + } + while (*str != '\0' && *str != SECTION_END_CHAR) { + str++; + } + if (*str != SECTION_END_CHAR) { + /* The section end character must be present somewhere thereafter */ + return 0; + } + return 1; +} + +/* Returns true if the input string denotes a key-value pair, + * false otherwise. + */ +static int is_key_value_str(const char *str) +{ + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + if (*str != KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must be present after the key part */ + return 0; + } + return 1; +} + +/* Returns true if the input string denotes a key without a value, + * false otherwise. + */ +static int is_key_without_value_str(const char *str) +{ + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must not be present after the key part */ + return 0; + } + return 1; +} + +/* + * Parses a section name from an input string. The input string is assumed to + * already have been identified as a valid input string denoting a section name. + */ +static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n) +{ + unsigned int count; + + count = 0; + /* Jump past the section begin character */ + str++; + while (*str != '\0' && *str != SECTION_END_CHAR) { + /* Read in the section name into the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = *str; + out_buf++; + str++; + count++; + } + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = '\0'; + return 1; +} + +/* + * Parses a key and value from an input string. The input string is assumed to + * already have been identified as a valid input string denoting a key-value pair. + */ +static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n) +{ + unsigned int count1; + unsigned int count2; + + count1 = 0; + count2 = 0; + /* Read the key value from the input string and write it sequentially + * to the first output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + /* Ensure the first output buffer is large enough. */ + if (count1 == out_buf1_n) { + return 0; + } + /* Copy the character to the first output buffer */ + *out_buf1 = *str; + out_buf1++; + str++; + count1++; + } + /* Terminate the first output buffer */ + if (count1 == out_buf1_n) { + return 0; + } + *out_buf1 = '\0'; + /* Now trace the first input buffer backwards until we hit a non-blank character */ + while (is_blank_char(*(out_buf1 - 1))) { + out_buf1--; + } + *out_buf1 = '\0'; + /* Try to proceed one more character, past the last read key-value + * delimiter, in the input string. + */ + if (*str != '\0') { + str++; + } + /* Now find start of the value in the input string by walking the input + * string until we either hit the null-terminator or a blank character. + */ + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + while (*str != '\0') { + /* Fail if there is a possibility that we are overwriting the second + * input buffer. + */ + if (count2 == out_buf2_n) { + return 0; + } + /* Copy the character to the second output buffer */ + *out_buf2 = *str; + out_buf2++; + str++; + count2++; + } + /* Terminate the second output buffer */ + if (count2 == out_buf2_n) { + return 0; + } + *out_buf2 = '\0'; + return 1; +} + +/* + * Parses a key from an input string. The input string is assumed to already + * have been identified as a valid input string denoting a key without a value. + */ +static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n) +{ + unsigned int count; + + count = 0; + /* Now read the key value from the input string and write it sequentially + * to the output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0') { + /* Ensure the output buffer is large enough. */ + if (count == out_buf_n) { + return 0; + } + /* Copy the character to the input buffer */ + *out_buf = *str; + out_buf++; + str++; + count++; + } + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = '\0'; + return 1; +} + +/* Returns a pointer to the next token in the input string delimited + * by the specified delimiter or null if no such token exist. The provided + * last pointer will be changed to point one position after the pointed + * token. The currently ouputted token will be null-terminated. + * + * An idiom for tokenizing a (in this case, comma-separated) string is: + * + * char test_string[] = "Token1,Token2,Token3"; + * char token[255]; + * char *str; + * + * str = test_string; + * while ((token = get_token(str, ',', &str) != NULL) { + * printf("token: %s", token); + * } + */ +static const char * get_token(char *str, char delim, char **last) +{ + + char *s0; + + s0 = str; + /* If we hit the null-terminator the string + * is exhausted and another token does not + * exist. + */ + if (*str == '\0') { + return NULL; + } + /* Walk the string until we encounter a + * null-terminator or the delimiter. + */ + while (*str != '\0' && *str != delim) { + str++; + } + /* Terminate the return token, if necessary */ + if (*str != '\0') { + *str = '\0'; + str++; + } + *last = str; + return s0; +} + +/* Returns a converted value pointed to by the provided key in the given section. + * The mode specifies which conversion takes place and dictates what value out + * is pointing to. The value out is pointing to will be replaced by the converted + * value assuming conversion is succesful. The function returns 1 if conversion + * is succsessful and 0 if the convertion could not be carried out. + */ +static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out) +{ + char value[MAX_VALUECHARS]; + + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } + switch (mode) { + case CONVERT_MODE_INT: + *((int *)out) = atoi(value); + return 1; + case CONVERT_MODE_LONG: + *((long *)out) = atol(value); + return 1; + case CONVERT_MODE_DOUBLE: + *((double *)out) = atof(value); + return 1; + } + return 0; +} + +/* Returns a converted tuple pointed to by the provided key in the given section. + * The tuple is created by splitting the value by the supplied delimiter and then + * converting each token after the split according to the specified mode. + * The array out is pointing to will be replaced by the converted tuple + * assuming conversion is succesful. The function returns 1 if conversion + * is succsessful and 0 if the convertion could not be carried out. + */ +static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out) +{ + unsigned int count; + const char *token; + static char value[MAX_VALUECHARS]; + char *v; + + if (out == NULL) { + return 0; + } + if (n_out == 0) { + return 0; + } + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } + v = value; + count = 0; + /* Walk over all tokens in the value, and convert them and assign them + * to the output array as specified by the mode. + */ + while ((token = get_token(v, delim, &v)) != NULL && count < n_out) { + switch (mode) { + case CONVERT_MODE_INT: + ((int *)out)[count] = atoi(token); + break; + case CONVERT_MODE_LONG: + ((long *)out)[count] = atol(token); + break; + case CONVERT_MODE_DOUBLE: + ((double *)out)[count] = atof(token); + break; + default: + return 0; + } + count++; + } + return 1; +} + +/* Returns a pointer to the section or null if the named section does not + * exist. + */ +static Section * get_section(Section *sections, unsigned int n, const char *name) +{ + unsigned int i; + Section *section; + + if (name == NULL) { + return NULL; + } + section = sections; + i = 0; + while (i < n) { + if (strcmp(section->name, name) == 0) { + return section; + } + section++; + i++; + } + return NULL; +} + +/* Callback function that is passed into the enumeration function in the + * string map. It casts the passed into object into a FILE pointer and + * writes out the key and value to the file. + */ +static void enum_map(const char *key, const char *value, const void *obj) +{ + FILE *stream; + char buf[MAX_LINECHARS]; + + if (key == NULL || value == NULL) { + return; + } + if (obj == NULL) { + return; + } + stream = (FILE *)obj; + if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { + sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); + fputs(buf, stream); + } +} + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..6ec3ec8 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,507 @@ +/* + * settings version 1.0.0 + * + * ANSI C implementation for managing application settings. + * + * settings.h + * + * Copyright (c) 2009 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * settings is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * settings is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with settings. If not, see . + */ +#ifndef _SETTINGS_H_ +#define _SETTINGS_H_ + +#include +#include +#include +#include + +#include "strmap.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef struct Settings Settings; + +/* + * This callback function is called once per key-value when enumerating + * all keys inside a section. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a settings object. + * + * Return value: A pointer to a settings object, + * or null if a new settings object could not be allocated. + */ +Settings * settings_new(); + +/* + * Releases all memory held by a settings object. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * If the supplied settings object has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void settings_delete(Settings *settings); + +/* + * Constructs a settings object by loading settings in textual form + * from the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: A pointer to a settings object, + * or null if an error occurred. + */ +Settings * settings_open(FILE *stream); + +/* + * Saves the current settings object in textual form to the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: 1 if the operation succeeded, 0 otherwise. + */ +int settings_save(const Settings *settings, FILE *stream); + +/* + * Returns the value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Returns the integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The integer value associated to the provided section and + * key, or 0 if no such value exists. + */ +int settings_get_int(const Settings *settings, const char *section, const char *key); + +/* + * Returns the long integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The long integer value associated to the provided section and + * key, or 0 if no such value exists. + */ +long settings_get_long(const Settings *settings, const char *section, const char *key); + +/* + * Returns the double value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The double value associated to the provided section and + * key, or 0 if no such value exists. + */ +double settings_get_double(const Settings *settings, const char *section, const char *key); + +/* + * Returns the integer tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); + +/* + * Returns the long tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); + +/* + * Returns the double tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); + +/* + * Associates a value with the supplied key in the provided section. + * If the key is already associated with a value, the previous value + * is replaced. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. The string must have a string length > 0. The string will + * be copied. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int settings_set(Settings *setting, const char *section, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values that exist + * in the provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The number of associations between keys and values in + * the provided section. + */ +int settings_section_get_count(const Settings *settings, const char *section); + +/* + * Enumerates all associations between keys and values in the provided + * section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); + +#ifdef __cplusplus +} +#endif + +#endif + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ diff --git a/src/strmap.c b/src/strmap.c new file mode 100644 index 0000000..43989b4 --- /dev/null +++ b/src/strmap.c @@ -0,0 +1,514 @@ +/* + * strmap version 2.0.0 + * + * ANSI C hash table for strings. + * + * Version history: + * 1.0.0 - initial release + * 2.0.0 - changed function prefix from strmap to sm to ensure + * ANSI C compatibility + * + * strmap.c + * + * Copyright (c) 2009, 2011 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with strmap. If not, see . + */ +#include "strmap.h" + +typedef struct Pair Pair; + +typedef struct Bucket Bucket; + +struct Pair { + char *key; + char *value; +}; + +struct Bucket { + unsigned int count; + Pair *pairs; +}; + +struct StrMap { + unsigned int count; + Bucket *buckets; +}; + +static Pair * get_pair(Bucket *bucket, const char *key); +static unsigned long hash(const char *str); + +StrMap * sm_new(unsigned int capacity) +{ + StrMap *map; + + map = malloc(sizeof(StrMap)); + if (map == NULL) { + return NULL; + } + map->count = capacity; + map->buckets = malloc(map->count * sizeof(Bucket)); + if (map->buckets == NULL) { + free(map); + return NULL; + } + memset(map->buckets, 0, map->count * sizeof(Bucket)); + return map; +} + +void sm_delete(StrMap *map) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return; + } + n = map->count; + bucket = map->buckets; + i = 0; + while (i < n) { + m = bucket->count; + pair = bucket->pairs; + j = 0; + while(j < m) { + free(pair->key); + free(pair->value); + pair++; + j++; + } + free(bucket->pairs); + bucket++; + i++; + } + free(map->buckets); + free(map); +} + +int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + if (out_buf == NULL && n_out_buf == 0) { + return strlen(pair->value) + 1; + } + if (out_buf == NULL) { + return 0; + } + if (strlen(pair->value) >= n_out_buf) { + return 0; + } + strcpy(out_buf, pair->value); + return 1; +} + +int sm_exists(const StrMap *map, const char *key) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + return 1; +} + +int sm_put(StrMap *map, const char *key, const char *value) +{ + unsigned int key_len, value_len, index; + Bucket *bucket; + Pair *tmp_pairs, *pair; + char *tmp_value; + char *new_key, *new_value; + + if (map == NULL) { + return 0; + } + if (key == NULL || value == NULL) { + return 0; + } + key_len = strlen(key); + value_len = strlen(value); + /* Get a pointer to the bucket the key string hashes to */ + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + /* Check if we can handle insertion by simply replacing + * an existing value in a key-value pair in the bucket. + */ + if ((pair = get_pair(bucket, key)) != NULL) { + /* The bucket contains a pair that matches the provided key, + * change the value for that pair to the new value. + */ + if (strlen(pair->value) < value_len) { + /* If the new value is larger than the old value, re-allocate + * space for the new larger value. + */ + tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char)); + if (tmp_value == NULL) { + return 0; + } + pair->value = tmp_value; + } + /* Copy the new value into the pair that matches the key */ + strcpy(pair->value, value); + return 1; + } + /* Allocate space for a new key and value */ + new_key = malloc((key_len + 1) * sizeof(char)); + if (new_key == NULL) { + return 0; + } + new_value = malloc((value_len + 1) * sizeof(char)); + if (new_value == NULL) { + free(new_key); + return 0; + } + /* Create a key-value pair */ + if (bucket->count == 0) { + /* The bucket is empty, lazily allocate space for a single + * key-value pair. + */ + bucket->pairs = malloc(sizeof(Pair)); + if (bucket->pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->count = 1; + } + else { + /* The bucket wasn't empty but no pair existed that matches the provided + * key, so create a new key-value pair. + */ + tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + if (tmp_pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->pairs = tmp_pairs; + bucket->count++; + } + /* Get the last pair in the chain for the bucket */ + pair = &(bucket->pairs[bucket->count - 1]); + pair->key = new_key; + pair->value = new_value; + /* Copy the key and its value into the key-value pair */ + strcpy(pair->key, key); + strcpy(pair->value, value); + return 1; +} + +int sm_get_count(const StrMap *map) +{ + unsigned int i, j, n, m; + unsigned int count; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + count = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + count++; + pair++; + j++; + } + bucket++; + i++; + } + return count; +} + +int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (enum_func == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + enum_func(pair->key, pair->value, obj); + pair++; + j++; + } + bucket++; + i++; + } + return 1; +} + +/* + * Returns a pair from the bucket that matches the provided key, + * or null if no such pair exist. + */ +static Pair * get_pair(Bucket *bucket, const char *key) +{ + unsigned int i, n; + Pair *pair; + + n = bucket->count; + if (n == 0) { + return NULL; + } + pair = bucket->pairs; + i = 0; + while (i < n) { + if (pair->key != NULL && pair->value != NULL) { + if (strcmp(pair->key, key) == 0) { + return pair; + } + } + pair++; + i++; + } + return NULL; +} + +/* + * Returns a hash code for the provided string. + */ +static unsigned long hash(const char *str) +{ + unsigned long hash = 5381; + int c; + + while (c = *str++) { + hash = ((hash << 5) + hash) + c; + } + return hash; +} + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ diff --git a/src/strmap.h b/src/strmap.h new file mode 100644 index 0000000..bd97b98 --- /dev/null +++ b/src/strmap.h @@ -0,0 +1,355 @@ +/* + * strmap version 2.0.0 + * + * ANSI C hash table for strings. + * + * Version history: + * 1.0.0 - initial release + * 2.0.0 - changed function prefix from strmap to sm to ensure + * ANSI C compatibility + * + * strmap.h + * + * Copyright (c) 2009, 2011 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with strmap. If not, see . + */ +#ifndef _STRMAP_H_ +#define _STRMAP_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +typedef struct StrMap StrMap; + +/* + * This callback function is called once per key-value when enumerating + * all keys associated to values. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a string map. + * + * Parameters: + * + * capacity: The number of top-level slots this string map + * should allocate. This parameter must be > 0. + * + * Return value: A pointer to a string map object, + * or null if a new string map could not be allocated. + */ +StrMap * sm_new(unsigned int capacity); + +/* + * Releases all memory held by a string map object. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * If the supplied string map has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void sm_delete(StrMap *map); + +/* + * Returns the value associated with the supplied key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Queries the existence of a key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: 1 if the key exists, 0 otherwise. + */ +int sm_exists(const StrMap *map, const char *key); + +/* + * Associates a value with the supplied key. If the key is already + * associated with a value, the previous value is replaced. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int sm_put(StrMap *map, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * Return value: The number of associations between keys and values. + */ +int sm_get_count(const StrMap *map); + +/* + * Enumerates all associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); + +#ifdef __cplusplus +} +#endif + +#endif + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ From 9dfe447e6db8a00636ee0114c50010f46aa12115 Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Wed, 4 Jul 2012 23:34:24 +0200 Subject: [PATCH 018/235] Compilation fixes --- src/settings.c | 9 +++++---- src/strmap.c | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/settings.c b/src/settings.c index 75c43ce..c386167 100644 --- a/src/settings.c +++ b/src/settings.c @@ -50,7 +50,6 @@ typedef struct Section Section; typedef struct ParseState ParseState; -typedef enum ConvertMode ConvertMode; struct Settings { Section *sections; @@ -74,6 +73,8 @@ enum ConvertMode { CONVERT_MODE_DOUBLE, }; +typedef enum ConvertMode ConvertMode; + static void trim_str(const char *str, char *out_buf); static int parse_str(Settings *settings, char *str, ParseState *parse_state); static int is_blank_char(char c); @@ -95,7 +96,7 @@ Settings * settings_new() { Settings *settings; - settings = malloc(sizeof(Settings)); + settings = (Settings*)malloc(sizeof(Settings)); if (settings == NULL) { return NULL; } @@ -257,7 +258,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const s = get_section(settings->sections, settings->section_count, section); if (s == NULL) { /* The section is not created---create it */ - s = realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); if (s == NULL) { return 0; } @@ -269,7 +270,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const free(s); return 0; } - s->name = malloc((strlen(section) + 1) * sizeof(char)); + s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); if (s->name == NULL) { sm_delete(s->map); free(s); diff --git a/src/strmap.c b/src/strmap.c index 43989b4..5daa1ba 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -61,12 +61,12 @@ StrMap * sm_new(unsigned int capacity) { StrMap *map; - map = malloc(sizeof(StrMap)); + map = (StrMap*)malloc(sizeof(StrMap)); if (map == NULL) { return NULL; } map->count = capacity; - map->buckets = malloc(map->count * sizeof(Bucket)); + map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); if (map->buckets == NULL) { free(map); return NULL; @@ -187,7 +187,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* If the new value is larger than the old value, re-allocate * space for the new larger value. */ - tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char)); + tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); if (tmp_value == NULL) { return 0; } @@ -198,11 +198,11 @@ int sm_put(StrMap *map, const char *key, const char *value) return 1; } /* Allocate space for a new key and value */ - new_key = malloc((key_len + 1) * sizeof(char)); + new_key = (char*)malloc((key_len + 1) * sizeof(char)); if (new_key == NULL) { return 0; } - new_value = malloc((value_len + 1) * sizeof(char)); + new_value = (char*)malloc((value_len + 1) * sizeof(char)); if (new_value == NULL) { free(new_key); return 0; @@ -212,7 +212,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* The bucket is empty, lazily allocate space for a single * key-value pair. */ - bucket->pairs = malloc(sizeof(Pair)); + bucket->pairs = (Pair*)malloc(sizeof(Pair)); if (bucket->pairs == NULL) { free(new_key); free(new_value); @@ -224,7 +224,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* The bucket wasn't empty but no pair existed that matches the provided * key, so create a new key-value pair. */ - tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); if (tmp_pairs == NULL) { free(new_key); free(new_value); From b93430355600555cf7fccc29907500dab3a0ea0f Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Thu, 5 Jul 2012 01:06:40 +0200 Subject: [PATCH 019/235] Added Eclipse project files to .gitignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.gitignore b/.gitignore index 01b7530..3634137 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,15 @@ # Intellij .idea *.iml + +# Eclipse +.cproject +.project + # Object files *.o bin/* + # Libraries *.lib From 1b84085a810e5346618d6993d1064b39a8d78245 Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Thu, 5 Jul 2012 01:12:17 +0200 Subject: [PATCH 020/235] Added syslog as dependency for init scripts, added systemd service file --- mbpfan.init.debian | 4 ++-- mbpfan.init.fedora => mbpfan.init.redhat | 4 ++-- mbpfan.service | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) rename mbpfan.init.fedora => mbpfan.init.redhat (95%) create mode 100644 mbpfan.service diff --git a/mbpfan.init.debian b/mbpfan.init.debian index 5fbe7d0..e279704 100755 --- a/mbpfan.init.debian +++ b/mbpfan.init.debian @@ -1,8 +1,8 @@ #! /bin/sh ### BEGIN INIT INFO # Provides: mbpfan -# Required-Start: $remote_fs -# Required-Stop: $remote_fs +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: mbpfan initscript diff --git a/mbpfan.init.fedora b/mbpfan.init.redhat similarity index 95% rename from mbpfan.init.fedora rename to mbpfan.init.redhat index 1c77c8d..a83765b 100755 --- a/mbpfan.init.fedora +++ b/mbpfan.init.redhat @@ -7,8 +7,8 @@ ### BEGIN INIT INFO # Provides: mbpfan -# Required-Start: $remote_fs -# Required-Stop: $remote_fs +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: mbpfan initscript diff --git a/mbpfan.service b/mbpfan.service new file mode 100644 index 0000000..d440e0d --- /dev/null +++ b/mbpfan.service @@ -0,0 +1,12 @@ +[Unit] +Description=Start the mbpfan daemon +After=syslog.target +After=remote-fs.target + +[Service] +ExecStart=/usr/sbin/mbpfan +Type=forking +Restart=always + +[Install] +WantedBy=multi-user.target From 6ea021ad785ccdde84df2fd26c535c380ebee74f Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Thu, 5 Jul 2012 01:22:48 +0200 Subject: [PATCH 021/235] Added support for configfile /etc/mbpfan.conf --- AUTHORS | 8 +++---- Makefile | 22 ++++++++++++++++-- mbpfan.conf | 7 ++++++ src/daemon.c | 7 +++--- src/mbpfan.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 mbpfan.conf diff --git a/AUTHORS b/AUTHORS index 98414f3..fe4287c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,12 +1,12 @@ MAINTAINERS AND CONTRIBUTORS ---------------------------- -Daniel Graziotin -CeRiAl +Daniel Graziotin +Ismail Khatib ORIGINARY AUTHORS ----------------- -Allan McRae mbpfan -rvega \ No newline at end of file +Allan McRae mbpfan +rvega diff --git a/Makefile b/Makefile index a67d557..073f4fb 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ C = c OUTPUT_PATH = bin/ SOURCE_PATH = src/ EXE = bin/mbpfan +CONF = mbpfan.conf ifeq ($(COMPILER), G++) ifeq ($(OS),Windows_NT) @@ -55,12 +56,29 @@ clean: install: cp $(EXE) /usr/sbin + cp -n $(CONF) /etc @echo "-----------------------------------------------------------------------------" - @echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure)" - @echo "Is located in the main folder of the source files. It is called mbpfan.init." + @echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu for sure)" + @echo "is located in the main folder of the source files, called mbpfan.init.debian" @echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)" @echo "and move it to /etc/init.d" @echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults" + @echo "" + @echo "Additionally, an init file suitable for /etc/rc.d/init.d/functions" + @echo "(RHEL/CentOS & Fedora) is also located at the same place, this file is called" + @echo "mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions" + @echo "and move it to /etc/init.d" + @echo "To add the script to the default runlevels, run the following as root:" + @echo "chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off" + @echo "" + @echo "As a special bonus, a service file for systemd is also included. To use it," + @echo "execute the following as root:" + @echo "cp mbpfan.service /usr/lib/systemd/system" + @echo "ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service" + @echo "systemctl daemon-reload" + @echo "systemctl start mbpfan.service" + @echo "To start the service automatically at boot, also execute the following:" + @echo "systemctl enable mbpfan.service" @echo "-----------------------------------------------------------------------------" rebuild: clean all diff --git a/mbpfan.conf b/mbpfan.conf new file mode 100644 index 0000000..5f2fd5c --- /dev/null +++ b/mbpfan.conf @@ -0,0 +1,7 @@ +[general] +min_fan_speed = 2000 # default is 2000 +max_fan_speed = 6200 # default is 6200 +low_temp = 63 # try ranges 55-63, default is 63 +high_temp = 66 # try ranges 58-66, default is 66 +max_temp = 86 # do not set it > 90, default is 86 +polling_interval = 7 # default is 7 diff --git a/src/daemon.c b/src/daemon.c index 3909127..ba05549 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,6 +1,7 @@ /** * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Ismail Khatib * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -162,7 +163,7 @@ void go_daemon(void (*fan_control)()) { if (verbose) { - printf("Writing a new .pid file with value %d at: %s", current_pid, program_pid); + printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid); syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); } if (write_pid(current_pid) == 0) @@ -170,7 +171,7 @@ void go_daemon(void (*fan_control)()) syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); if (verbose) { - printf("ERROR: Can not create a .pid file at: %s. Aborting", program_pid); + printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid); } exit(EXIT_FAILURE); } @@ -178,7 +179,7 @@ void go_daemon(void (*fan_control)()) { if (verbose) { - printf("Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid); syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); } } diff --git a/src/mbpfan.c b/src/mbpfan.c index 56fdf32..d586c1d 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -3,6 +3,7 @@ * Copyright (C) 2010 Allan McRae * Modifications by Rafael Vega * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Ismail Khatib * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +25,8 @@ * * Tested models: * MacBook Pro 8.1 13" (Intel i7 - Linux 3.2) - * Macbook Pro 6,2 15" (Intel i7 - Linux 3.2) + * MacBook Pro 6,2 15" (Intel i7 - Linux 3.2) + * MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) */ @@ -36,6 +38,7 @@ #include #include "mbpfan.h" #include "global.h" +#include "settings.h" /* lazy min/max... */ #define min(a,b) a < b ? a : b @@ -273,6 +276,9 @@ void mbpfan() int old_temp, new_temp, fan_speed, steps; int temp_change; int step_up, step_down; + FILE *f = NULL; + Settings *settings = NULL; + int result = 0; t_sensors* sensors = retrieve_sensors(); set_fans_man(sensors); @@ -280,12 +286,62 @@ void mbpfan() fan_speed = 2000; set_fan_speed(sensors, fan_speed); + f = fopen("/etc/mbpfan.conf", "r"); + if (f == NULL) + { + /* Could not open configfile */ + if(verbose) + { + printf("Couldn't open configfile, using defaults\n"); + if(daemonize) + { + syslog(LOG_INFO, "Couldn't open configfile, using defaults"); + } + } + } + else + { + settings = settings_open(f); + fclose(f); + if (settings == NULL) + { + /* Could not read configfile */ + if(verbose) + { + printf("Couldn't read configfile\n"); + if(daemonize) + { + syslog(LOG_INFO, "Couldn't read configfile"); + } + } + } + else + { + /* Read configfile values */ + result = settings_get_int(settings, "general", "min_fan_speed"); + if (result != 0) min_fan_speed = result; + result = settings_get_int(settings, "general", "max_fan_speed"); + if (result != 0) max_fan_speed = result; + result = settings_get_int(settings, "general", "low_temp"); + if (result != 0) low_temp = result; + result = settings_get_int(settings, "general", "high_temp"); + if (result != 0) high_temp = result; + result = settings_get_int(settings, "general", "max_temp"); + if (result != 0) max_temp = result; + result = settings_get_int(settings, "general", "polling_interval"); + if (result != 0) polling_interval = result; + + /* Destroy the settings object */ + settings_delete(settings); + } + } + if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); + syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } sleep(polling_interval); @@ -330,7 +386,7 @@ void mbpfan() printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); if(daemonize) { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); } } @@ -341,7 +397,7 @@ void mbpfan() printf("Sleeping for %d seconds\n", polling_interval); if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds\n", polling_interval); + syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } sleep(polling_interval); From 542d06e5e087e122d20c8d69fdec616544860556 Mon Sep 17 00:00:00 2001 From: Magnus Stubman Date: Tue, 31 Jul 2012 18:54:18 +0200 Subject: [PATCH 022/235] Added Gentoo init file --- mbpfan.init.gentoo | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 mbpfan.init.gentoo diff --git a/mbpfan.init.gentoo b/mbpfan.init.gentoo new file mode 100755 index 0000000..30a35ea --- /dev/null +++ b/mbpfan.init.gentoo @@ -0,0 +1,18 @@ +#!/sbin/runscript + +depend() { + use logger +} + +start() { + ebegin "Starting mbpfan" + start-stop-daemon --start --quiet --exec /usr/sbin/mbpfan + eend $? +} + +stop() { + ebegin "Stopping mbpfan" + start-stop-daemon --stop --quiet --exec /usr/sbin/mbpfan + eend $? +} + From 94d7b35c0ac0594317773fefd553dd2386b2c534 Mon Sep 17 00:00:00 2001 From: Magnus Stubman Date: Tue, 31 Jul 2012 19:04:57 +0200 Subject: [PATCH 023/235] added gentoo init hint in Makefile --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 073f4fb..841b1f0 100644 --- a/Makefile +++ b/Makefile @@ -79,6 +79,10 @@ install: @echo "systemctl start mbpfan.service" @echo "To start the service automatically at boot, also execute the following:" @echo "systemctl enable mbpfan.service" + @echo "" + @echo "An init file is available for gentoo users: mbpfan.init.gentoo" + @echo "To install, run # cp mbpfan.init.gentoo /etc/init.d/mbpfan" + @echo "To automatically run mbpfan at boot, run # rc-update add mbpfan default" @echo "-----------------------------------------------------------------------------" rebuild: clean all From a895bff912439caf375e55c90c0cb6ad32525db1 Mon Sep 17 00:00:00 2001 From: Magnus Stubman Date: Tue, 31 Jul 2012 19:09:20 +0200 Subject: [PATCH 024/235] Altered README.md for Gentoo init usage --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c6df88..f0d75ff 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Fan-Control-Daemon Introduction --------------------- -This is an enhanced version of [rvega's Fan-Control-Daemon](https://github.com/rvega/Fan-Control-Daemon), +This is an enhanced version of [CeRiAl's Fan-Control-Daemon](https://github.com/CeRiAl/Fan-Control-Daemon), which itself is an enhanced version of [rvega's Fan-Control-Daemon](https://github.com/rvega/Fan-Control-Daemon), which itself is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. @@ -49,6 +49,10 @@ Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults (Ubu An init file suitable for Fedora (and probably RedHat) can be found in the file mbpfan.init.fedora +An init file is available for gentoo users: mbpfan.init.gentoo +To install, run # cp mbpfan.init.gentoo /etc/init.d/mbpfan +To automatically run mbpfan at boot, run # rc-update add mbpfan default + Run Instructions --------------------- From 398e65ad5fc8131bd61286b1a3bf6ee72d772755 Mon Sep 17 00:00:00 2001 From: "Trevor Joynson (trevorj)" Date: Sun, 19 Aug 2012 08:33:35 -0400 Subject: [PATCH 025/235] Add Upstart (ubuntu) job --- Makefile | 17 +++++++++++++++-- mbpfan.upstart | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 mbpfan.upstart diff --git a/Makefile b/Makefile index a67d557..4171f3e 100644 --- a/Makefile +++ b/Makefile @@ -56,11 +56,24 @@ clean: install: cp $(EXE) /usr/sbin @echo "-----------------------------------------------------------------------------" - @echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure)" - @echo "Is located in the main folder of the source files. It is called mbpfan.init." + @echo "An init file suitable for /lib/lsb/init-functions (Debian) is located in" + @echo "in the main folder of the source files. It is called mbpfan.init." @echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)" @echo "and move it to /etc/init.d" @echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults" + @echo "" + @echo "Additionally, an init file suitable for /etc/rc.d/init.d/functions" + @echo "(RHEL/CentOS & Fedora) is also located at the same place, this file is called" + @echo "mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions" + @echo "and move it to /etc/init.d" + @echo "To add the script to the default runlevels, run the following as root:" + @echo "chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off" + @echo "" + @echo "For upstart based init systems (Ubuntu), an example upstart job has been" + @echo "provided for use in place of the LSB-style init script. To use, execute" + @echo "as root:" + @echo "cp mbpfan.upstart /etc/init/mbpfan.conf" + @echo "start mbpfan" @echo "-----------------------------------------------------------------------------" rebuild: clean all diff --git a/mbpfan.upstart b/mbpfan.upstart new file mode 100644 index 0000000..ff2957f --- /dev/null +++ b/mbpfan.upstart @@ -0,0 +1,26 @@ +# mbpfan - A simple daemon to control fan speed on all Macbook/Macbook Pros \ +# (probably all Apple computers) for Linux 3.x.x + +description "mbpfan" + +start on filesystem or runlevel [2345] +stop on runlevel [!2345] + +respawn +respawn limit 10 5 +umask 022 + +console log + +pre-start script + test -x /usr/sbin/mbpfan || { stop; exit 0; } + # A way to turn pidfile writing off would be nice. That or mbpfan + # could check for staleness before refusing to control your fans. + rm -f /var/run/mbpfan.pid +end script + +# Starting mbpfan in the foreground without being in an interactive tty currently +# seems to segfault, so expect forkage. +expect fork +exec /usr/sbin/mbpfan + From 43f370576f0e5b569c08c7bb308720241167de93 Mon Sep 17 00:00:00 2001 From: "Trevor Joynson (trevorj)" Date: Sun, 19 Aug 2012 08:35:56 -0400 Subject: [PATCH 026/235] Fix typo in Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4171f3e..5f317a3 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ install: cp $(EXE) /usr/sbin @echo "-----------------------------------------------------------------------------" @echo "An init file suitable for /lib/lsb/init-functions (Debian) is located in" - @echo "in the main folder of the source files. It is called mbpfan.init." + @echo "in the main folder of the source files. It is called mbpfan.init.debian" @echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)" @echo "and move it to /etc/init.d" @echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults" From fcf18f52cf9339460de6e3ea6c29c02b38a86593 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 22 Aug 2012 15:05:52 +0200 Subject: [PATCH 027/235] Cleanup, reformatting, better Makefile and better README --- AUTHORS | 2 +- Makefile | 41 +- README.md | 61 ++- src/daemon.c | 202 +++++----- src/main.c | 59 ++- src/mbpfan.c | 461 +++++++++++------------ src/mbpfan.h | 7 + src/minunit.c | 158 ++++---- src/minunit.h | 1 + src/settings.c | 990 ++++++++++++++++++++++++------------------------- src/settings.h | 574 ++++++++++++++-------------- src/strmap.c | 499 +++++++++++++------------ src/strmap.h | 270 +++++++------- 13 files changed, 1647 insertions(+), 1678 deletions(-) diff --git a/AUTHORS b/AUTHORS index fe4287c..9ff236c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,7 +3,7 @@ MAINTAINERS AND CONTRIBUTORS Daniel Graziotin Ismail Khatib - +Trevor Joynson ORIGINARY AUTHORS ----------------- diff --git a/Makefile b/Makefile index 1813d44..cb26497 100644 --- a/Makefile +++ b/Makefile @@ -54,37 +54,24 @@ all: $(OBJS) clean: rm -rf $(SOURCE_PATH)*.$(OBJ) $(EXE) +tests: + make install + sudo /usr/sbin/mbpfan -f -v -t + install: + make cp $(EXE) /usr/sbin cp -n $(CONF) /etc - @echo "-----------------------------------------------------------------------------" - @echo "An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu for sure)" - @echo "is located in the main folder of the source files, called mbpfan.init.debian" - @echo "Rename it to mbpfan, give it execution permissions (chmod +x mbpfan)" - @echo "and move it to /etc/init.d" - @echo "Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults" @echo "" - @echo "Additionally, an init file suitable for /etc/rc.d/init.d/functions" - @echo "(RHEL/CentOS & Fedora) is also located at the same place, this file is called" - @echo "mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions" - @echo "and move it to /etc/init.d" - @echo "To add the script to the default runlevels, run the following as root:" - @echo "chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off" + @echo "******************" + @echo "INSTALL COMPLETED" + @echo "******************" + @echo "" + @echo "A configuration file has been copied to /etc/mbpfan.conf" + @echo "See README.md file to have mbpfan automatically started at system boot." + @echo "" + @echo "Please run the tests now with the command" + @echo " sudo make tests" @echo "" - @echo "For upstart based init systems (Ubuntu), an example upstart job has been" - @echo "provided for use in place of the LSB-style init script. To use, execute" - @echo "as root:" - @echo "cp mbpfan.upstart /etc/init/mbpfan.conf" - @echo "start mbpfan" - @echo "As a special bonus, a service file for systemd is also included. To use it," - @echo "execute the following as root:" - @echo "cp mbpfan.service /usr/lib/systemd/system" - @echo "ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service" - @echo "systemctl daemon-reload" - @echo "systemctl start mbpfan.service" - @echo "To start the service automatically at boot, also execute the following:" - @echo "systemctl enable mbpfan.service" - @echo "-----------------------------------------------------------------------------" - rebuild: clean all #rebuild is not entirely correct diff --git a/README.md b/README.md index 9c6df88..06212d1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ Fan-Control-Daemon Introduction --------------------- - This is an enhanced version of [rvega's Fan-Control-Daemon](https://github.com/rvega/Fan-Control-Daemon), which itself is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) @@ -15,43 +14,36 @@ This enhanced version assumes any number of processors and fans (max. 10). * It requires root use * It daemonizes or stays in foreground * Verbose mode for both syslog and stdout +* Users can configure it using the file /etc/mbpfan.conf Compile Instructions --------------------- - Compile with make Manually compile with gcc -o bin/mbpfan src/mbpfan.c -lm -Run The Tests (Recommended) ---------------------------- - -It is recommended to run the tests before installing the program - sudo ./bin/mbpfan -t Install Instructions -------------------- - Install with sudo make install -It actually copies mbpfan to /usr/sbin. +It copies mbpfan to /usr/sbin and mbpfan.conf to /etc -An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu fur sure) -is located in the main folder of the source files. It is called mbpfan.init.debian -Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) -and move it to /etc/init.d -Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults (Ubuntu example) -An init file suitable for Fedora (and probably RedHat) can be found -in the file mbpfan.init.fedora +Run The Tests (Recommended) +--------------------------- +It is recommended to run the tests after installing the program + sudo ./bin/mbpfan -t +or + sudo make tests + Run Instructions ---------------------- - +---------------- If not installed, run with sudo bin/mbpfan @@ -61,9 +53,39 @@ sudo mbpfan If installed and using the init file, run with (Ubuntu example) sudo service mbpfan start + +Starting at boot +---------------- +An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu for sure) +is located in the main folder of the source files, called mbpfan.init.debian +Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) +and move it to /etc/init.d +Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults + +Additionally, an init file suitable for /etc/rc.d/init.d/functions +(RHEL/CentOS & Fedora) is also located at the same place, this file is called +mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions +and move it to /etc/init.d +To add the script to the default runlevels, run the following as root: +chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off + +For upstart based init systems (Ubuntu), an example upstart job has been +provided for use in place of the LSB-style init script. To use, execute +as root: +cp mbpfan.upstart /etc/init/mbpfan.conf +start mbpfan +As a special bonus, a service file for systemd is also included. To use it, +execute the following as root: +cp mbpfan.service /usr/lib/systemd/system +ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service +systemctl daemon-reload +systemctl start mbpfan.service +To start the service automatically at boot, also execute the following: +systemctl enable mbpfan.service + + Usage ------- - Usage: ./mbpfan OPTION(S) -h Show the help screen -f Run in foreground @@ -78,7 +100,6 @@ GNU General Public License version 3 Based On --------------------- - * http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ * http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ * https://launchpad.net/macfanctld diff --git a/src/daemon.c b/src/daemon.c index ba05549..e1bc98e 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -32,175 +32,151 @@ int write_pid(int pid) { - FILE *file = NULL; - file = fopen(program_pid, "w"); - if(file != NULL) - { - fprintf(file, "%d", pid); - fclose(file); - return 1; - } - else - { - return 0; + FILE *file = NULL; + file = fopen(program_pid, "w"); + if(file != NULL) { + fprintf(file, "%d", pid); + fclose(file); + return 1; + } else { + return 0; } } int read_pid() { - FILE *file = NULL; - int pid = -1; - file = fopen(program_pid, "r"); - if(file != NULL) - { - fscanf(file, "%d", &pid); - fclose(file); - return pid; + FILE *file = NULL; + int pid = -1; + file = fopen(program_pid, "r"); + if(file != NULL) { + fscanf(file, "%d", &pid); + fclose(file); + return pid; } - return -1; + return -1; } int delete_pid() { - return remove(program_pid); + return remove(program_pid); } void signal_handler(int signal) { - switch(signal) - { + switch(signal) { case SIGHUP: - //TODO: restart myself - syslog(LOG_WARNING, "Received SIGHUP signal."); - delete_pid(); - exit(0); - break; + //TODO: restart myself + syslog(LOG_WARNING, "Received SIGHUP signal."); + delete_pid(); + exit(0); + break; case SIGTERM: - syslog(LOG_WARNING, "Received SIGTERM signal."); - delete_pid(); - //TODO: free resources - exit(0); - break; + syslog(LOG_WARNING, "Received SIGTERM signal."); + delete_pid(); + //TODO: free resources + exit(0); + break; case SIGINT: - syslog(LOG_WARNING, "Received SIGINT signal."); - delete_pid(); - //TODO: free resources - exit(0); + syslog(LOG_WARNING, "Received SIGINT signal."); + delete_pid(); + //TODO: free resources + exit(0); default: - syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); - break; + syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); + break; } } void go_daemon(void (*fan_control)()) { - // Setup signal handling before we start - signal(SIGHUP, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGINT, signal_handler); + // Setup signal handling before we start + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); - syslog(LOG_INFO, "%s starting up", program_name); + syslog(LOG_INFO, "%s starting up", program_name); - // Setup syslog logging - see SETLOGMASK(3) - if(verbose) - { - setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); - } - else - { - setlogmask(LOG_UPTO(LOG_INFO)); - openlog(program_name, LOG_CONS, LOG_USER); + // Setup syslog logging - see SETLOGMASK(3) + if(verbose) { + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + } else { + setlogmask(LOG_UPTO(LOG_INFO)); + openlog(program_name, LOG_CONS, LOG_USER); } - pid_t pid_slave; - pid_t sid_slave; + pid_t pid_slave; + pid_t sid_slave; - if (daemonize) - { + if (daemonize) { - pid_slave = fork(); - if (pid_slave < 0) - { - exit(EXIT_FAILURE); + pid_slave = fork(); + if (pid_slave < 0) { + exit(EXIT_FAILURE); } - if (pid_slave > 0) - { - // kill the father - exit(EXIT_SUCCESS); + if (pid_slave > 0) { + // kill the father + exit(EXIT_SUCCESS); } - umask(0022); + umask(0022); - // new sid_slave for the child process - sid_slave = setsid(); - if (sid_slave < 0) - { - exit(EXIT_FAILURE); + // new sid_slave for the child process + sid_slave = setsid(); + if (sid_slave < 0) { + exit(EXIT_FAILURE); } - if ((chdir("/")) < 0) - { - exit(EXIT_FAILURE); + if ((chdir("/")) < 0) { + exit(EXIT_FAILURE); } - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); } - int current_pid = getpid(); + int current_pid = getpid(); - if (read_pid() == -1) - { - if (verbose) - { - printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid); - syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); + if (read_pid() == -1) { + if (verbose) { + printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid); + syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); } - if (write_pid(current_pid) == 0) - { - syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); - if (verbose) - { - printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid); + if (write_pid(current_pid) == 0) { + syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); + if (verbose) { + printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid); } - exit(EXIT_FAILURE); - } - else - { - if (verbose) - { - printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid); - syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + exit(EXIT_FAILURE); + } else { + if (verbose) { + printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid); + syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); } } - } - else - { - syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); - if (verbose) - { - printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); + } else { + syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); + if (verbose) { + printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); } - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } - fan_control(); + fan_control(); - if(daemonize) - { - syslog(LOG_INFO, "%s daemon exiting", program_name); + if(daemonize) { + syslog(LOG_INFO, "%s daemon exiting", program_name); } - return; + return; } diff --git a/src/main.c b/src/main.c index 2c2358a..30253b8 100644 --- a/src/main.c +++ b/src/main.c @@ -29,15 +29,14 @@ const char *program_pid = "/var/run/mbpfan.pid"; void print_usage(int argc, char *argv[]) { - if (argc >=1) - { - printf("Usage: %s OPTION(S) \n", argv[0]); - printf("Options:\n"); - printf("\t-h Show this help screen\n"); - printf("\t-f Run in foreground\n"); - printf("\t-t Run the tests\n"); - printf("\t-v Be (a lot) verbose\n"); - printf("\n"); + if (argc >=1) { + printf("Usage: %s OPTION(S) \n", argv[0]); + printf("Options:\n"); + printf("\t-h Show this help screen\n"); + printf("\t-f Run in foreground\n"); + printf("\t-t Run the tests\n"); + printf("\t-v Be (a lot) verbose\n"); + printf("\n"); } } @@ -46,34 +45,32 @@ void print_usage(int argc, char *argv[]) int main(int argc, char *argv[]) { - int c; - while( (c = getopt(argc, argv, "hftv|help")) != -1) - { - switch(c) - { + int c; + while( (c = getopt(argc, argv, "hftv|help")) != -1) { + switch(c) { case 'h': - print_usage(argc, argv); - exit(0); - break; + print_usage(argc, argv); + exit(0); + break; case 'f': - daemonize = 0; - break; + daemonize = 0; + break; case 't': - tests(); - exit(0); - break; + tests(); + exit(0); + break; case 'v': - verbose = 1; - break; + verbose = 1; + break; default: - print_usage(argc, argv); - exit(0); - break; + print_usage(argc, argv); + exit(0); + break; } } - // pointer to mbpfan() function in mbpfan.c - void (*fan_control)() = mbpfan; - go_daemon(fan_control); - exit(0); + // pointer to mbpfan() function in mbpfan.c + void (*fan_control)() = mbpfan; + go_daemon(fan_control); + exit(0); } \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index d586c1d..5cc30de 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -58,348 +58,313 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; -struct s_sensors -{ - char* path; - char* fan_output_path; - char* fan_manual_path; - unsigned int temperature; - struct s_sensors *next; +struct s_sensors { + char* path; + char* fan_output_path; + char* fan_manual_path; + unsigned int temperature; + struct s_sensors *next; }; t_sensors *retrieve_sensors() { - t_sensors *sensors_head = NULL; - t_sensors *s = NULL; - - char *path = NULL; - const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; - const char *path_end = "_input"; - - int path_size = strlen(path_begin) + strlen(path_end) + 2; - char number[1]; - sprintf(number,"%d",0); - - int i = 0; - for(i = 0; i<10; i++) - { - path = (char*) malloc(sizeof( char ) * path_size); - - sprintf(number,"%d",i); - path[0] = '\0'; - strncat( path, path_begin, strlen(path_begin) ); - strncat( path, number, strlen(number) ); - strncat( path, path_end, strlen(path_begin) ); - - FILE *file = fopen(path, "r"); - - if(file != NULL) - { - s = (t_sensors *) malloc( sizeof( t_sensors ) ); - s->path = (char *) malloc(sizeof( char ) * path_size); - strcpy(s->path, path); - fscanf(file, "%d", &s->temperature); - if (sensors_head == NULL) - { - sensors_head = s; - sensors_head->next = NULL; - } - else - { - t_sensors *tmp = sensors_head; - while (tmp->next != NULL) - { - tmp = tmp->next; + t_sensors *sensors_head = NULL; + t_sensors *s = NULL; + + char *path = NULL; + const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; + const char *path_end = "_input"; + + int path_size = strlen(path_begin) + strlen(path_end) + 2; + char number[1]; + sprintf(number,"%d",0); + + int i = 0; + for(i = 0; i<10; i++) { + path = (char*) malloc(sizeof( char ) * path_size); + + sprintf(number,"%d",i); + path[0] = '\0'; + strncat( path, path_begin, strlen(path_begin) ); + strncat( path, number, strlen(number) ); + strncat( path, path_end, strlen(path_begin) ); + + FILE *file = fopen(path, "r"); + + if(file != NULL) { + s = (t_sensors *) malloc( sizeof( t_sensors ) ); + s->path = (char *) malloc(sizeof( char ) * path_size); + strcpy(s->path, path); + fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) { + sensors_head = s; + sensors_head->next = NULL; + } else { + t_sensors *tmp = sensors_head; + while (tmp->next != NULL) { + tmp = tmp->next; } - tmp->next = s; - tmp->next->next = NULL; + tmp->next = s; + tmp->next->next = NULL; } - fclose(file); + fclose(file); } - free(path); - path = NULL; + free(path); + path = NULL; } - if(sensors_head != NULL) - find_fans(sensors_head); - return sensors_head; + if(sensors_head != NULL) + find_fans(sensors_head); + return sensors_head; } void find_fans(t_sensors* sensors) { - t_sensors *tmp = sensors; + t_sensors *tmp = sensors; - char *path_output = NULL; - char *path_manual = NULL; + char *path_output = NULL; + char *path_manual = NULL; - const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; - const char *path_output_end = "_output"; - const char *path_man_end = "_manual"; + const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_output_end = "_output"; + const char *path_man_end = "_manual"; - int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; - int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; - char number[1]; - sprintf(number,"%d",0); + int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; + int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; + char number[1]; + sprintf(number,"%d",0); - int n_sensors = 0; - int n_fans = 0; + int n_sensors = 0; + int n_fans = 0; - for(n_sensors = 0; n_sensors<10; n_sensors++) - { - path_output = (char*) malloc(sizeof( char ) * path_min_size); - path_output[0] = '\0'; - path_manual = (char*) malloc(sizeof( char ) * path_man_size); - path_manual[0] = '\0'; - sprintf(number,"%d",n_sensors); + for(n_sensors = 0; n_sensors<10; n_sensors++) { + path_output = (char*) malloc(sizeof( char ) * path_min_size); + path_output[0] = '\0'; + path_manual = (char*) malloc(sizeof( char ) * path_man_size); + path_manual[0] = '\0'; + sprintf(number,"%d",n_sensors); - strncat( path_output, path_begin, strlen(path_begin) ); - strncat( path_output, number, strlen(number) ); - strncat( path_output, path_output_end, strlen(path_begin) ); + strncat( path_output, path_begin, strlen(path_begin) ); + strncat( path_output, number, strlen(number) ); + strncat( path_output, path_output_end, strlen(path_begin) ); - strncat( path_manual, path_begin, strlen(path_begin) ); - strncat( path_manual, number, strlen(number) ); - strncat( path_manual, path_man_end, strlen(path_begin) ); + strncat( path_manual, path_begin, strlen(path_begin) ); + strncat( path_manual, number, strlen(number) ); + strncat( path_manual, path_man_end, strlen(path_begin) ); - FILE *file = fopen(path_output, "r"); + FILE *file = fopen(path_output, "r"); - if(file != NULL) - { - if (tmp->path != NULL) - { - tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); - tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); + if(file != NULL) { + if (tmp->path != NULL) { + tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); + tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); } - strcpy(tmp->fan_output_path, path_output); - strcpy(tmp->fan_manual_path, path_manual); - tmp = tmp->next; - n_fans++; - fclose(file); + strcpy(tmp->fan_output_path, path_output); + strcpy(tmp->fan_manual_path, path_manual); + tmp = tmp->next; + n_fans++; + fclose(file); } } - if(verbose) - { - printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); - if(daemonize) - { - syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); + if(verbose) { + printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); + if(daemonize) { + syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); } } - free(path_output); - path_output = NULL; - free(path_manual); - path_manual = NULL; + free(path_output); + path_output = NULL; + free(path_manual); + path_manual = NULL; } void set_fans_man(t_sensors *sensors) { - t_sensors *tmp = sensors; - FILE *file; - while(tmp != NULL) - { - file = fopen(tmp->fan_manual_path, "rw+"); - if(file != NULL) - { - fprintf(file, "%d", 1); - fclose(file); + t_sensors *tmp = sensors; + FILE *file; + while(tmp != NULL) { + file = fopen(tmp->fan_manual_path, "rw+"); + if(file != NULL) { + fprintf(file, "%d", 1); + fclose(file); } - tmp = tmp->next; + tmp = tmp->next; } } t_sensors *refresh_sensors(t_sensors *sensors) { - t_sensors *tmp = sensors; + t_sensors *tmp = sensors; - while(tmp != NULL) - { - FILE *file = fopen(tmp->path, "r"); + while(tmp != NULL) { + FILE *file = fopen(tmp->path, "r"); - if(file != NULL) - { - fscanf(file, "%d", &tmp->temperature); - fclose(file); + if(file != NULL) { + fscanf(file, "%d", &tmp->temperature); + fclose(file); } - tmp = tmp->next; + tmp = tmp->next; } - return sensors; + return sensors; } - /* Controls the speed of the fan */ void set_fan_speed(t_sensors* sensors, int speed) { - t_sensors *tmp = sensors; - FILE *file; - while(tmp != NULL) - { - file = fopen(tmp->fan_output_path, "rw+"); - if(file != NULL) - { - fprintf(file, "%d", speed); - fclose(file); + t_sensors *tmp = sensors; + FILE *file; + while(tmp != NULL) { + file = fopen(tmp->fan_output_path, "rw+"); + if(file != NULL) { + fprintf(file, "%d", speed); + fclose(file); } - tmp = tmp->next; + tmp = tmp->next; } } - unsigned short get_temp(t_sensors* sensors) { - sensors = refresh_sensors(sensors); - int sum_temp = 0; - unsigned short temp = 0; - - t_sensors* tmp = sensors; - while(tmp != NULL) - { - sum_temp += tmp->temperature; - tmp = tmp->next; + sensors = refresh_sensors(sensors); + int sum_temp = 0; + unsigned short temp = 0; + + t_sensors* tmp = sensors; + while(tmp != NULL) { + sum_temp += tmp->temperature; + tmp = tmp->next; } - temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); - return temp; + temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); + return temp; } - -void mbpfan() +void retrieve_settings() { - int old_temp, new_temp, fan_speed, steps; - int temp_change; - int step_up, step_down; - FILE *f = NULL; - Settings *settings = NULL; - int result = 0; - - t_sensors* sensors = retrieve_sensors(); - set_fans_man(sensors); - new_temp = get_temp(sensors); - fan_speed = 2000; - set_fan_speed(sensors, fan_speed); - - f = fopen("/etc/mbpfan.conf", "r"); - if (f == NULL) - { - /* Could not open configfile */ - if(verbose) - { - printf("Couldn't open configfile, using defaults\n"); - if(daemonize) - { - syslog(LOG_INFO, "Couldn't open configfile, using defaults"); + Settings *settings = NULL; + int result = 0; + FILE *f = NULL; + f = fopen("/etc/mbpfan.conf", "r"); + + if (f == NULL) { + /* Could not open configfile */ + if(verbose) { + printf("Couldn't open configfile, using defaults\n"); + if(daemonize) { + syslog(LOG_INFO, "Couldn't open configfile, using defaults"); } } - } - else - { - settings = settings_open(f); - fclose(f); - if (settings == NULL) - { - /* Could not read configfile */ - if(verbose) - { - printf("Couldn't read configfile\n"); - if(daemonize) - { - syslog(LOG_INFO, "Couldn't read configfile"); + } else { + settings = settings_open(f); + fclose(f); + if (settings == NULL) { + /* Could not read configfile */ + if(verbose) { + printf("Couldn't read configfile\n"); + if(daemonize) { + syslog(LOG_INFO, "Couldn't read configfile"); } } - } - else - { - /* Read configfile values */ - result = settings_get_int(settings, "general", "min_fan_speed"); - if (result != 0) min_fan_speed = result; - result = settings_get_int(settings, "general", "max_fan_speed"); - if (result != 0) max_fan_speed = result; - result = settings_get_int(settings, "general", "low_temp"); - if (result != 0) low_temp = result; - result = settings_get_int(settings, "general", "high_temp"); - if (result != 0) high_temp = result; - result = settings_get_int(settings, "general", "max_temp"); - if (result != 0) max_temp = result; - result = settings_get_int(settings, "general", "polling_interval"); - if (result != 0) polling_interval = result; - - /* Destroy the settings object */ - settings_delete(settings); + } else { + /* Read configfile values */ + result = settings_get_int(settings, "general", "min_fan_speed"); + if (result != 0) min_fan_speed = result; + result = settings_get_int(settings, "general", "max_fan_speed"); + if (result != 0) max_fan_speed = result; + result = settings_get_int(settings, "general", "low_temp"); + if (result != 0) low_temp = result; + result = settings_get_int(settings, "general", "high_temp"); + if (result != 0) high_temp = result; + result = settings_get_int(settings, "general", "max_temp"); + if (result != 0) max_temp = result; + result = settings_get_int(settings, "general", "polling_interval"); + if (result != 0) polling_interval = result; + + /* Destroy the settings object */ + settings_delete(settings); } } +} + + +void mbpfan() +{ + int old_temp, new_temp, fan_speed, steps; + int temp_change; + int step_up, step_down; + + retrieve_settings(); + + t_sensors* sensors = retrieve_sensors(); + set_fans_man(sensors); + new_temp = get_temp(sensors); + fan_speed = 2000; + set_fan_speed(sensors, fan_speed); + - if(verbose) - { - printf("Sleeping for %d seconds\n", polling_interval); - if(daemonize) - { - syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); + if(verbose) { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { + syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } - sleep(polling_interval); + sleep(polling_interval); - step_up = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); + step_up = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); - step_down = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); + step_down = (float)( max_fan_speed - min_fan_speed ) / + (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); - while(1) - { - old_temp = new_temp; - new_temp = get_temp(sensors); + while(1) { + old_temp = new_temp; + new_temp = get_temp(sensors); - if(new_temp >= max_temp && fan_speed != max_fan_speed) - { - fan_speed = max_fan_speed; + if(new_temp >= max_temp && fan_speed != max_fan_speed) { + fan_speed = max_fan_speed; } - if(new_temp <= low_temp && fan_speed != min_fan_speed) - { - fan_speed = min_fan_speed; + if(new_temp <= low_temp && fan_speed != min_fan_speed) { + fan_speed = min_fan_speed; } - temp_change = new_temp - old_temp; + temp_change = new_temp - old_temp; - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) - { - steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; - fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); + if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { + steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; + fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); } - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) - { - steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; - fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); + if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { + steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; + fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); } - if(verbose) - { - printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); - if(daemonize) - { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); + if(verbose) { + printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(daemonize) { + syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); } } - set_fan_speed(sensors, fan_speed); + set_fan_speed(sensors, fan_speed); - if(verbose) - { - printf("Sleeping for %d seconds\n", polling_interval); - if(daemonize) - { - syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); + if(verbose) { + printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { + syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } - sleep(polling_interval); + sleep(polling_interval); } } diff --git a/src/mbpfan.h b/src/mbpfan.h index 97ba7c2..a51fcd2 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -37,6 +37,13 @@ extern int polling_interval; struct s_sensors; typedef struct s_sensors t_sensors; +/** + * Tries to use the settings located in + * /etc/mbpfan.conf + * If it fails, the default hardcoded settings are used + */ +void retrieve_settings(); + /** * Detect the sensors in /sys/devices/platform/coretemp.0/temp * Return a linked list of t_sensors (first temperature detected) diff --git a/src/minunit.c b/src/minunit.c index fadaf97..0603047 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -4,117 +4,139 @@ #include #include #include "mbpfan.h" +#include "settings.h" #include "minunit.h" int tests_run = 0; -struct s_sensors -{ - char* path; - char* fan_output_path; - char* fan_manual_path; - unsigned int temperature; - struct s_sensors *next; +struct s_sensors { + char* path; + char* fan_output_path; + char* fan_manual_path; + unsigned int temperature; + struct s_sensors *next; }; typedef s_sensors t_sensors; static char *test_sensor_paths() { - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - t_sensors* tmp = sensors; - while(tmp != NULL) - { - mu_assert("Sensor does not have a valid path", tmp->path != NULL); - if(tmp->path != NULL) - mu_assert("Sensor does not have valid temperature", tmp->temperature > 0); - tmp = tmp->next; + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + t_sensors* tmp = sensors; + while(tmp != NULL) { + mu_assert("Sensor does not have a valid path", tmp->path != NULL); + if(tmp->path != NULL) + mu_assert("Sensor does not have valid temperature", tmp->temperature > 0); + tmp = tmp->next; } - return 0; + return 0; } static char *test_fan_paths() { - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - t_sensors* tmp = sensors; - int found_fan_path = 0; - while(tmp != NULL) - { - if(tmp->fan_output_path != NULL) - found_fan_path++; - tmp = tmp->next; + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + t_sensors* tmp = sensors; + int found_fan_path = 0; + while(tmp != NULL) { + if(tmp->fan_output_path != NULL) + found_fan_path++; + tmp = tmp->next; } - mu_assert("No fans found", found_fan_path != 0); - return 0; + mu_assert("No fans found", found_fan_path != 0); + return 0; } unsigned time_seed() { - time_t now = time ( 0 ); - unsigned char *p = (unsigned char *)&now; - unsigned seed = 0; - size_t i; - for ( i = 0; i < sizeof now; i++ ) - seed = seed * ( UCHAR_MAX + 2U ) + p[i]; - return seed; + time_t now = time ( 0 ); + unsigned char *p = (unsigned char *)&now; + unsigned seed = 0; + size_t i; + for ( i = 0; i < sizeof now; i++ ) + seed = seed * ( UCHAR_MAX + 2U ) + p[i]; + return seed; } // nothing better than a horrible piece of code to // stress a little bit the CPU int stress(int n) { - int f = n; - while (f > 0) - { - while(n > 0) - { - srand ( time_seed() ); - n--; + int f = n; + while (f > 0) { + while(n > 0) { + srand ( time_seed() ); + n--; } - f--; - n = f; + f--; + n = f; } } static char *test_get_temp() { - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - unsigned short temp_1 = get_temp(sensors); - mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150); - stress(2000); - unsigned short temp_2 = get_temp(sensors); - mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2); - return 0; + t_sensors* sensors = retrieve_sensors(); + mu_assert("No sensors found", sensors != NULL); + unsigned short temp_1 = get_temp(sensors); + mu_assert("Invalid Global Temperature Found", temp_1 > 1 && temp_1 < 150); + stress(2000); + unsigned short temp_2 = get_temp(sensors); + mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2); + return 0; +} + +static char *test_config_file() +{ + FILE *f = NULL; + Settings *settings = NULL; + f = fopen("/etc/mbpfan.conf", "r"); + mu_assert("No config file found", f != NULL); + + if (f == NULL) + return 0; + + settings = settings_open(f); + fclose(f); + mu_assert("Could not read settings from config file", settings != NULL); + if (settings == NULL) + return 0; + + mu_assert("Could not read min_fan_speed from config file",settings_get_int(settings, "general", "min_fan_speed") != 0); + mu_assert("Could not read max_fan_speed from config file",settings_get_int(settings, "general", "max_fan_speed") != 0); + mu_assert("Could not read low_temp from config file",settings_get_int(settings, "general", "low_temp") != 0); + mu_assert("Could not read high_temp from config file",settings_get_int(settings, "general", "high_temp") != 0); + mu_assert("Could not read max_temp from config file",settings_get_int(settings, "general", "max_temp") != 0); + mu_assert("Could not read polling_interval from config file",settings_get_int(settings, "general", "polling_interval") != 0); + /* Destroy the settings object */ + settings_delete(settings); + + return 0; } static char *all_tests() { - mu_run_test(test_sensor_paths); - mu_run_test(test_fan_paths); - mu_run_test(test_get_temp); - return 0; + mu_run_test(test_sensor_paths); + mu_run_test(test_fan_paths); + mu_run_test(test_get_temp); + mu_run_test(test_config_file); + return 0; } int tests() { - printf("Starting the tests..\n"); - printf("It is normal for them to take a bit to finish.\n"); - char *result = all_tests(); - if (result != 0) - { - printf("%s \n", result); - } - else - { - printf("ALL TESTS PASSED\n"); + printf("Starting the tests..\n"); + printf("It is normal for them to take a bit to finish.\n"); + char *result = all_tests(); + if (result != 0) { + printf("%s \n", result); + } else { + printf("ALL TESTS PASSED\n"); } - printf("Tests run: %d\n", tests_run); + printf("Tests run: %d\n", tests_run); - return result != 0; + return result != 0; } \ No newline at end of file diff --git a/src/minunit.h b/src/minunit.h index d9dc93d..0565cdf 100644 --- a/src/minunit.h +++ b/src/minunit.h @@ -10,6 +10,7 @@ extern int tests_run; static char *test_sensor_paths(); static char *test_fan_paths(); static char *test_get_temp(); +static char *test_config_file(); static char *all_tests(); diff --git a/src/settings.c b/src/settings.c index c386167..5af8932 100644 --- a/src/settings.c +++ b/src/settings.c @@ -13,7 +13,7 @@ * * Copyright (c) 2009-2011 Per Ola Kristensson. * - * Per Ola Kristensson + * Per Ola Kristensson * Inference Group, Department of Physics * University of Cambridge * Cavendish Laboratory @@ -52,25 +52,25 @@ typedef struct Section Section; typedef struct ParseState ParseState; struct Settings { - Section *sections; - unsigned int section_count; + Section *sections; + unsigned int section_count; }; struct Section { - char *name; - StrMap *map; + char *name; + StrMap *map; }; struct ParseState { - char *current_section; - unsigned int current_section_n; - int has_section; + char *current_section; + unsigned int current_section_n; + int has_section; }; enum ConvertMode { - CONVERT_MODE_INT, - CONVERT_MODE_LONG, - CONVERT_MODE_DOUBLE, + CONVERT_MODE_INT, + CONVERT_MODE_LONG, + CONVERT_MODE_DOUBLE, }; typedef enum ConvertMode ConvertMode; @@ -94,216 +94,216 @@ static void enum_map(const char *key, const char *value, const void *obj); Settings * settings_new() { - Settings *settings; - - settings = (Settings*)malloc(sizeof(Settings)); - if (settings == NULL) { - return NULL; - } - settings->section_count = 0; - settings->sections = NULL; - return settings; + Settings *settings; + + settings = (Settings*)malloc(sizeof(Settings)); + if (settings == NULL) { + return NULL; + } + settings->section_count = 0; + settings->sections = NULL; + return settings; } void settings_delete(Settings *settings) { - unsigned int i, n; - Section *section; - - if (settings == NULL) { - return; - } - section = settings->sections; - n = settings->section_count; - i = 0; - while (i < n) { - sm_delete(section->map); - if (section->name != NULL) { - free(section->name); - } - section++; - i++; - } - free(settings->sections); - free(settings); + unsigned int i, n; + Section *section; + + if (settings == NULL) { + return; + } + section = settings->sections; + n = settings->section_count; + i = 0; + while (i < n) { + sm_delete(section->map); + if (section->name != NULL) { + free(section->name); + } + section++; + i++; + } + free(settings->sections); + free(settings); } Settings * settings_open(FILE *stream) { - Settings *settings; - char buf[MAX_LINECHARS]; - char trimmed_buf[MAX_LINECHARS]; - char section_buf[MAX_LINECHARS]; - ParseState parse_state; - - if (stream == NULL) { - return NULL; - } - settings = settings_new(); - if (settings == NULL) { - return NULL; - } - parse_state.current_section = section_buf; - parse_state.current_section_n = sizeof(section_buf); - parse_state.has_section = 0; - trim_str("", trimmed_buf); - while (fgets(buf, MAX_LINECHARS, stream) != NULL) { - trim_str(buf, trimmed_buf); - if (!parse_str(settings, trimmed_buf, &parse_state)) { - return NULL; - } - } - return settings; + Settings *settings; + char buf[MAX_LINECHARS]; + char trimmed_buf[MAX_LINECHARS]; + char section_buf[MAX_LINECHARS]; + ParseState parse_state; + + if (stream == NULL) { + return NULL; + } + settings = settings_new(); + if (settings == NULL) { + return NULL; + } + parse_state.current_section = section_buf; + parse_state.current_section_n = sizeof(section_buf); + parse_state.has_section = 0; + trim_str("", trimmed_buf); + while (fgets(buf, MAX_LINECHARS, stream) != NULL) { + trim_str(buf, trimmed_buf); + if (!parse_str(settings, trimmed_buf, &parse_state)) { + return NULL; + } + } + return settings; } int settings_save(const Settings *settings, FILE *stream) { - unsigned int i, n; - Section *section; - char buf[MAX_LINECHARS]; - - if (settings == NULL) { - return 0; - } - if (stream == NULL) { - return 0; - } - section = settings->sections; - n = settings->section_count; - i = 0; - while (i < n) { - sprintf(buf, "[%s]\n", section->name); - fputs(buf, stream); - sm_enum(section->map, enum_map, stream); - section++; - i++; - fputs("\n", stream); - } - return 0; + unsigned int i, n; + Section *section; + char buf[MAX_LINECHARS]; + + if (settings == NULL) { + return 0; + } + if (stream == NULL) { + return 0; + } + section = settings->sections; + n = settings->section_count; + i = 0; + while (i < n) { + sprintf(buf, "[%s]\n", section->name); + fputs(buf, stream); + sm_enum(section->map, enum_map, stream); + section++; + i++; + fputs("\n", stream); + } + return 0; } int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf) { - Section *s; - - if (settings == NULL) { - return 0; - } - s = get_section(settings->sections, settings->section_count, section); - if (s == NULL) { - return 0; - } - return sm_get(s->map, key, out_buf, n_out_buf); + Section *s; + + if (settings == NULL) { + return 0; + } + s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { + return 0; + } + return sm_get(s->map, key, out_buf, n_out_buf); } int settings_get_int(const Settings *settings, const char *section, const char *key) { - int i; + int i; - if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { - return i; - } - return 0; + if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { + return i; + } + return 0; } long settings_get_long(const Settings *settings, const char *section, const char *key) { - long l; + long l; - if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { - return l; - } - return 0; + if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { + return l; + } + return 0; } double settings_get_double(const Settings *settings, const char *section, const char *key) { - double d; + double d; - if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { - return d; - } - return 0; + if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { + return d; + } + return 0; } int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out) { - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, out, n_out); + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_INT, out, n_out); } long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out) { - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, out, n_out); + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_LONG, out, n_out); } double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out) { - return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, out, n_out); + return get_converted_tuple(settings, section, key, ',', CONVERT_MODE_DOUBLE, out, n_out); } int settings_set(Settings *settings, const char *section, const char *key, const char *value) { - Section *s; - - if (settings == NULL) { - return 0; - } - if (section == NULL || key == NULL || value == NULL) { - return 0; - } - if (strlen(section) == 0) { - return 0; - } - /* Get a pointer to the section */ - s = get_section(settings->sections, settings->section_count, section); - if (s == NULL) { - /* The section is not created---create it */ - s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); - if (s == NULL) { - return 0; - } - settings->sections = s; - settings->section_count++; - s = &(settings->sections[settings->section_count - 1]); - s->map = sm_new(DEFAULT_STRMAP_CAPACITY); - if (s->map == NULL) { - free(s); - return 0; - } - s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); - if (s->name == NULL) { - sm_delete(s->map); - free(s); - return 0; - } - strcpy(s->name, section); - } - return sm_put(s->map, key, value); + Section *s; + + if (settings == NULL) { + return 0; + } + if (section == NULL || key == NULL || value == NULL) { + return 0; + } + if (strlen(section) == 0) { + return 0; + } + /* Get a pointer to the section */ + s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { + /* The section is not created---create it */ + s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + if (s == NULL) { + return 0; + } + settings->sections = s; + settings->section_count++; + s = &(settings->sections[settings->section_count - 1]); + s->map = sm_new(DEFAULT_STRMAP_CAPACITY); + if (s->map == NULL) { + free(s); + return 0; + } + s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); + if (s->name == NULL) { + sm_delete(s->map); + free(s); + return 0; + } + strcpy(s->name, section); + } + return sm_put(s->map, key, value); } int settings_section_get_count(const Settings *settings, const char *section) { - Section *sect; - - if (settings == NULL) { - return 0; - } - sect = get_section(settings->sections, settings->section_count, section); - if (sect == NULL) { - return 0; - } - return sm_get_count(sect->map); + Section *sect; + + if (settings == NULL) { + return 0; + } + sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { + return 0; + } + return sm_get_count(sect->map); } int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj) { - Section *sect; + Section *sect; - sect = get_section(settings->sections, settings->section_count, section); - if (sect == NULL) { - return 0; - } - return sm_enum(sect->map, enum_func, obj); + sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { + return 0; + } + return sm_enum(sect->map, enum_func, obj); } /* Copies a trimmed variant without leading and trailing blank characters @@ -312,27 +312,27 @@ int settings_section_enum(const Settings *settings, const char *section, setting */ static void trim_str(const char *str, char *out_buf) { - unsigned int len; - const char *s0; - - while (*str != '\0' && is_blank_char(*str)) { - str++; - } - s0 = str; - len = 0; - while (*str != '\0') { - len++; - str++; - } - if (len > 0) { - str--; - } - while (is_blank_char(*str)) { - str--; - len--; - } - memcpy(out_buf, s0, len); - out_buf[len] = '\0'; + unsigned int len; + const char *s0; + + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + s0 = str; + len = 0; + while (*str != '\0') { + len++; + str++; + } + if (len > 0) { + str--; + } + while (is_blank_char(*str)) { + str--; + len--; + } + memcpy(out_buf, s0, len); + out_buf[len] = '\0'; } /* Parses a single input string and updates the provided settings object. @@ -348,55 +348,49 @@ static void trim_str(const char *str, char *out_buf) */ static int parse_str(Settings *settings, char *str, ParseState *parse_state) { - char buf[MAX_LINECHARS]; - char buf1[MAX_LINECHARS]; - char buf2[MAX_LINECHARS]; - int result; - - if (*str == '\0') { - return 1; - } - else if (is_blank_str(str)) { - return 1; - } - else if (is_comment_str(str)) { - return 1; - } - else if (is_section_str(str)) { - result = get_section_from_str(str, buf, sizeof(buf)); - if (!result) { - return 0; - } - if (strlen(buf) + 1 > parse_state->current_section_n) { - return 0; - } - strcpy(parse_state->current_section, buf); - parse_state->has_section = 1; - return 1; - } - else if (is_key_value_str(str)) { - result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2)); - if (!result) { - return 0; - } - if (!parse_state->has_section) { - return 0; - } - return settings_set(settings, parse_state->current_section, buf1, buf2); - } - else if (is_key_without_value_str(str)) { - result = get_key_without_value_from_str(str, buf, sizeof(buf)); - if (!result) { - return 0; - } - if (!parse_state->has_section) { - return 0; - } - return settings_set(settings, parse_state->current_section, buf, ""); - } - else { - return 0; - } + char buf[MAX_LINECHARS]; + char buf1[MAX_LINECHARS]; + char buf2[MAX_LINECHARS]; + int result; + + if (*str == '\0') { + return 1; + } else if (is_blank_str(str)) { + return 1; + } else if (is_comment_str(str)) { + return 1; + } else if (is_section_str(str)) { + result = get_section_from_str(str, buf, sizeof(buf)); + if (!result) { + return 0; + } + if (strlen(buf) + 1 > parse_state->current_section_n) { + return 0; + } + strcpy(parse_state->current_section, buf); + parse_state->has_section = 1; + return 1; + } else if (is_key_value_str(str)) { + result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2)); + if (!result) { + return 0; + } + if (!parse_state->has_section) { + return 0; + } + return settings_set(settings, parse_state->current_section, buf1, buf2); + } else if (is_key_without_value_str(str)) { + result = get_key_without_value_from_str(str, buf, sizeof(buf)); + if (!result) { + return 0; + } + if (!parse_state->has_section) { + return 0; + } + return settings_set(settings, parse_state->current_section, buf, ""); + } else { + return 0; + } } /* Returns true if the input character is blank, @@ -404,7 +398,7 @@ static int parse_str(Settings *settings, char *str, ParseState *parse_state) */ static int is_blank_char(char c) { - return c == ' ' || c == '\t' || c == '\r' || c == '\n'; + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; } /* Returns true if the input string is blank, @@ -412,13 +406,13 @@ static int is_blank_char(char c) */ static int is_blank_str(const char *str) { - while (*str != '\0') { - if (!is_blank_char(*str)) { - return 0; - } - str++; - } - return 1; + while (*str != '\0') { + if (!is_blank_char(*str)) { + return 0; + } + str++; + } + return 1; } /* Returns true if the input string denotes a comment, @@ -426,13 +420,13 @@ static int is_blank_str(const char *str) */ static int is_comment_str(const char *str) { - if (*str == COMMENT_CHAR) { - /* To be a comment the first character must be the - * comment character. - */ - return 1; - } - return 0; + if (*str == COMMENT_CHAR) { + /* To be a comment the first character must be the + * comment character. + */ + return 1; + } + return 0; } /* Returns true if the input string denotes a section name, @@ -440,18 +434,18 @@ static int is_comment_str(const char *str) */ static int is_section_str(const char *str) { - if (*str != SECTION_START_CHAR) { - /* The first character must be the section start character */ - return 0; - } - while (*str != '\0' && *str != SECTION_END_CHAR) { - str++; - } - if (*str != SECTION_END_CHAR) { - /* The section end character must be present somewhere thereafter */ - return 0; - } - return 1; + if (*str != SECTION_START_CHAR) { + /* The first character must be the section start character */ + return 0; + } + while (*str != '\0' && *str != SECTION_END_CHAR) { + str++; + } + if (*str != SECTION_END_CHAR) { + /* The section end character must be present somewhere thereafter */ + return 0; + } + return 1; } /* Returns true if the input string denotes a key-value pair, @@ -459,18 +453,18 @@ static int is_section_str(const char *str) */ static int is_key_value_str(const char *str) { - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* It is illegal to start with the key-value separator */ - return 0; - } - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - str++; - } - if (*str != KEY_VALUE_SEPARATOR_CHAR) { - /* The key-value separator must be present after the key part */ - return 0; - } - return 1; + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + if (*str != KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must be present after the key part */ + return 0; + } + return 1; } /* Returns true if the input string denotes a key without a value, @@ -478,18 +472,18 @@ static int is_key_value_str(const char *str) */ static int is_key_without_value_str(const char *str) { - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* It is illegal to start with the key-value separator */ - return 0; - } - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - str++; - } - if (*str == KEY_VALUE_SEPARATOR_CHAR) { - /* The key-value separator must not be present after the key part */ - return 0; - } - return 1; + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* It is illegal to start with the key-value separator */ + return 0; + } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + str++; + } + if (*str == KEY_VALUE_SEPARATOR_CHAR) { + /* The key-value separator must not be present after the key part */ + return 0; + } + return 1; } /* @@ -498,27 +492,27 @@ static int is_key_without_value_str(const char *str) */ static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n) { - unsigned int count; - - count = 0; - /* Jump past the section begin character */ - str++; - while (*str != '\0' && *str != SECTION_END_CHAR) { - /* Read in the section name into the output buffer */ - if (count == out_buf_n) { - return 0; - } - *out_buf = *str; - out_buf++; - str++; - count++; - } - /* Terminate the output buffer */ - if (count == out_buf_n) { - return 0; - } - *out_buf = '\0'; - return 1; + unsigned int count; + + count = 0; + /* Jump past the section begin character */ + str++; + while (*str != '\0' && *str != SECTION_END_CHAR) { + /* Read in the section name into the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = *str; + out_buf++; + str++; + count++; + } + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = '\0'; + return 1; } /* @@ -527,67 +521,67 @@ static int get_section_from_str(const char *str, char *out_buf, unsigned int out */ static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n) { - unsigned int count1; - unsigned int count2; - - count1 = 0; - count2 = 0; - /* Read the key value from the input string and write it sequentially - * to the first output buffer by walking the input string until we either hit - * the null-terminator or the key-value separator. - */ - while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { - /* Ensure the first output buffer is large enough. */ - if (count1 == out_buf1_n) { - return 0; - } - /* Copy the character to the first output buffer */ - *out_buf1 = *str; - out_buf1++; - str++; - count1++; - } - /* Terminate the first output buffer */ - if (count1 == out_buf1_n) { - return 0; - } - *out_buf1 = '\0'; - /* Now trace the first input buffer backwards until we hit a non-blank character */ - while (is_blank_char(*(out_buf1 - 1))) { - out_buf1--; - } - *out_buf1 = '\0'; - /* Try to proceed one more character, past the last read key-value - * delimiter, in the input string. - */ - if (*str != '\0') { - str++; - } - /* Now find start of the value in the input string by walking the input - * string until we either hit the null-terminator or a blank character. - */ - while (*str != '\0' && is_blank_char(*str)) { - str++; - } - while (*str != '\0') { - /* Fail if there is a possibility that we are overwriting the second - * input buffer. - */ - if (count2 == out_buf2_n) { - return 0; - } - /* Copy the character to the second output buffer */ - *out_buf2 = *str; - out_buf2++; - str++; - count2++; - } - /* Terminate the second output buffer */ - if (count2 == out_buf2_n) { - return 0; - } - *out_buf2 = '\0'; - return 1; + unsigned int count1; + unsigned int count2; + + count1 = 0; + count2 = 0; + /* Read the key value from the input string and write it sequentially + * to the first output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { + /* Ensure the first output buffer is large enough. */ + if (count1 == out_buf1_n) { + return 0; + } + /* Copy the character to the first output buffer */ + *out_buf1 = *str; + out_buf1++; + str++; + count1++; + } + /* Terminate the first output buffer */ + if (count1 == out_buf1_n) { + return 0; + } + *out_buf1 = '\0'; + /* Now trace the first input buffer backwards until we hit a non-blank character */ + while (is_blank_char(*(out_buf1 - 1))) { + out_buf1--; + } + *out_buf1 = '\0'; + /* Try to proceed one more character, past the last read key-value + * delimiter, in the input string. + */ + if (*str != '\0') { + str++; + } + /* Now find start of the value in the input string by walking the input + * string until we either hit the null-terminator or a blank character. + */ + while (*str != '\0' && is_blank_char(*str)) { + str++; + } + while (*str != '\0') { + /* Fail if there is a possibility that we are overwriting the second + * input buffer. + */ + if (count2 == out_buf2_n) { + return 0; + } + /* Copy the character to the second output buffer */ + *out_buf2 = *str; + out_buf2++; + str++; + count2++; + } + /* Terminate the second output buffer */ + if (count2 == out_buf2_n) { + return 0; + } + *out_buf2 = '\0'; + return 1; } /* @@ -596,30 +590,30 @@ static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int */ static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n) { - unsigned int count; - - count = 0; - /* Now read the key value from the input string and write it sequentially - * to the output buffer by walking the input string until we either hit - * the null-terminator or the key-value separator. - */ - while (*str != '\0') { - /* Ensure the output buffer is large enough. */ - if (count == out_buf_n) { - return 0; - } - /* Copy the character to the input buffer */ - *out_buf = *str; - out_buf++; - str++; - count++; - } - /* Terminate the output buffer */ - if (count == out_buf_n) { - return 0; - } - *out_buf = '\0'; - return 1; + unsigned int count; + + count = 0; + /* Now read the key value from the input string and write it sequentially + * to the output buffer by walking the input string until we either hit + * the null-terminator or the key-value separator. + */ + while (*str != '\0') { + /* Ensure the output buffer is large enough. */ + if (count == out_buf_n) { + return 0; + } + /* Copy the character to the input buffer */ + *out_buf = *str; + out_buf++; + str++; + count++; + } + /* Terminate the output buffer */ + if (count == out_buf_n) { + return 0; + } + *out_buf = '\0'; + return 1; } /* Returns a pointer to the next token in the input string delimited @@ -641,29 +635,29 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign static const char * get_token(char *str, char delim, char **last) { - char *s0; - - s0 = str; - /* If we hit the null-terminator the string - * is exhausted and another token does not - * exist. - */ - if (*str == '\0') { - return NULL; - } - /* Walk the string until we encounter a - * null-terminator or the delimiter. - */ - while (*str != '\0' && *str != delim) { - str++; - } - /* Terminate the return token, if necessary */ - if (*str != '\0') { - *str = '\0'; - str++; - } - *last = str; - return s0; + char *s0; + + s0 = str; + /* If we hit the null-terminator the string + * is exhausted and another token does not + * exist. + */ + if (*str == '\0') { + return NULL; + } + /* Walk the string until we encounter a + * null-terminator or the delimiter. + */ + while (*str != '\0' && *str != delim) { + str++; + } + /* Terminate the return token, if necessary */ + if (*str != '\0') { + *str = '\0'; + str++; + } + *last = str; + return s0; } /* Returns a converted value pointed to by the provided key in the given section. @@ -674,23 +668,23 @@ static const char * get_token(char *str, char delim, char **last) */ static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out) { - char value[MAX_VALUECHARS]; - - if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { - return 0; - } - switch (mode) { - case CONVERT_MODE_INT: - *((int *)out) = atoi(value); - return 1; - case CONVERT_MODE_LONG: - *((long *)out) = atol(value); - return 1; - case CONVERT_MODE_DOUBLE: - *((double *)out) = atof(value); - return 1; - } - return 0; + char value[MAX_VALUECHARS]; + + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } + switch (mode) { + case CONVERT_MODE_INT: + *((int *)out) = atoi(value); + return 1; + case CONVERT_MODE_LONG: + *((long *)out) = atol(value); + return 1; + case CONVERT_MODE_DOUBLE: + *((double *)out) = atof(value); + return 1; + } + return 0; } /* Returns a converted tuple pointed to by the provided key in the given section. @@ -702,42 +696,42 @@ static int get_converted_value(const Settings *settings, const char *section, co */ static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out) { - unsigned int count; - const char *token; - static char value[MAX_VALUECHARS]; - char *v; - - if (out == NULL) { - return 0; - } - if (n_out == 0) { - return 0; - } - if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { - return 0; - } - v = value; - count = 0; - /* Walk over all tokens in the value, and convert them and assign them - * to the output array as specified by the mode. - */ - while ((token = get_token(v, delim, &v)) != NULL && count < n_out) { - switch (mode) { - case CONVERT_MODE_INT: - ((int *)out)[count] = atoi(token); - break; - case CONVERT_MODE_LONG: - ((long *)out)[count] = atol(token); - break; - case CONVERT_MODE_DOUBLE: - ((double *)out)[count] = atof(token); - break; - default: - return 0; - } - count++; - } - return 1; + unsigned int count; + const char *token; + static char value[MAX_VALUECHARS]; + char *v; + + if (out == NULL) { + return 0; + } + if (n_out == 0) { + return 0; + } + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { + return 0; + } + v = value; + count = 0; + /* Walk over all tokens in the value, and convert them and assign them + * to the output array as specified by the mode. + */ + while ((token = get_token(v, delim, &v)) != NULL && count < n_out) { + switch (mode) { + case CONVERT_MODE_INT: + ((int *)out)[count] = atoi(token); + break; + case CONVERT_MODE_LONG: + ((long *)out)[count] = atol(token); + break; + case CONVERT_MODE_DOUBLE: + ((double *)out)[count] = atof(token); + break; + default: + return 0; + } + count++; + } + return 1; } /* Returns a pointer to the section or null if the named section does not @@ -745,22 +739,22 @@ static int get_converted_tuple(const Settings *settings, const char *section, co */ static Section * get_section(Section *sections, unsigned int n, const char *name) { - unsigned int i; - Section *section; - - if (name == NULL) { - return NULL; - } - section = sections; - i = 0; - while (i < n) { - if (strcmp(section->name, name) == 0) { - return section; - } - section++; - i++; - } - return NULL; + unsigned int i; + Section *section; + + if (name == NULL) { + return NULL; + } + section = sections; + i = 0; + while (i < n) { + if (strcmp(section->name, name) == 0) { + return section; + } + section++; + i++; + } + return NULL; } /* Callback function that is passed into the enumeration function in the @@ -769,20 +763,20 @@ static Section * get_section(Section *sections, unsigned int n, const char *name */ static void enum_map(const char *key, const char *value, const void *obj) { - FILE *stream; - char buf[MAX_LINECHARS]; - - if (key == NULL || value == NULL) { - return; - } - if (obj == NULL) { - return; - } - stream = (FILE *)obj; - if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { - sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); - fputs(buf, stream); - } + FILE *stream; + char buf[MAX_LINECHARS]; + + if (key == NULL || value == NULL) { + return; + } + if (obj == NULL) { + return; + } + stream = (FILE *)obj; + if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { + sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); + fputs(buf, stream); + } } /* diff --git a/src/settings.h b/src/settings.h index 6ec3ec8..dde66de 100644 --- a/src/settings.h +++ b/src/settings.h @@ -7,7 +7,7 @@ * * Copyright (c) 2009 Per Ola Kristensson. * - * Per Ola Kristensson + * Per Ola Kristensson * Inference Group, Department of Physics * University of Cambridge * Cavendish Laboratory @@ -43,292 +43,292 @@ extern "C" { #endif -typedef struct Settings Settings; - -/* - * This callback function is called once per key-value when enumerating - * all keys inside a section. - * - * Parameters: - * - * key: A pointer to a null-terminated C string. The string must not - * be modified by the client. - * - * value: A pointer to a null-terminated C string. The string must - * not be modified by the client. - * - * obj: A pointer to a client-specific object. This parameter may be - * null. - * - * Return value: None. - */ -typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); - -/* - * Creates a settings object. - * - * Return value: A pointer to a settings object, - * or null if a new settings object could not be allocated. - */ -Settings * settings_new(); - -/* - * Releases all memory held by a settings object. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * If the supplied settings object has been previously released, the - * behaviour of this function is undefined. - * - * Return value: None. - */ -void settings_delete(Settings *settings); - -/* - * Constructs a settings object by loading settings in textual form - * from the given stream. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * stream: A pointer to a stream. This parameter cannot be null. - * - * Return value: A pointer to a settings object, - * or null if an error occurred. - */ -Settings * settings_open(FILE *stream); - -/* - * Saves the current settings object in textual form to the given stream. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * stream: A pointer to a stream. This parameter cannot be null. - * - * Return value: 1 if the operation succeeded, 0 otherwise. - */ -int settings_save(const Settings *settings, FILE *stream); - -/* - * Returns the value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out_buf: A pointer to an output buffer which will contain the value, - * if it exists and fits into the buffer. - * - * n_out_buf: The size of the output buffer in bytes. - * - * Return value: If out_buf is set to null and n_out_buf is set to 0 the return - * value will be the number of bytes required to store the value (if it exists) - * and its null-terminator. For all other parameter configurations the return value - * is 1 if an associated value was found and completely copied into the output buffer, - * 0 otherwise. - */ -int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); - -/* - * Returns the integer value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The integer value associated to the provided section and - * key, or 0 if no such value exists. - */ -int settings_get_int(const Settings *settings, const char *section, const char *key); - -/* - * Returns the long integer value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The long integer value associated to the provided section and - * key, or 0 if no such value exists. - */ -long settings_get_long(const Settings *settings, const char *section, const char *key); - -/* - * Returns the double value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The double value associated to the provided section and - * key, or 0 if no such value exists. - */ -double settings_get_double(const Settings *settings, const char *section, const char *key); - -/* - * Returns the integer tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ -int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); - -/* - * Returns the long tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ -long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); - -/* - * Returns the double tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ -double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); - -/* - * Associates a value with the supplied key in the provided section. - * If the key is already associated with a value, the previous value - * is replaced. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. The string must have a string length > 0. The string will - * be copied. - * - * key: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * value: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * Return value: 1 if the association succeeded, 0 otherwise. - */ -int settings_set(Settings *setting, const char *section, const char *key, const char *value); - -/* - * Returns the number of associations between keys and values that exist - * in the provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The number of associations between keys and values in - * the provided section. - */ -int settings_section_get_count(const Settings *settings, const char *section); - -/* - * Enumerates all associations between keys and values in the provided - * section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * enum_func: A pointer to a callback function that will be - * called by this procedure once for every key associated - * with a value. This parameter cannot be null. - * - * obj: A pointer to a client-specific object. This parameter will be - * passed back to the client's callback function. This parameter can - * be null. - * - * Return value: 1 if enumeration completed, 0 otherwise. - */ -int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); + typedef struct Settings Settings; + + /* + * This callback function is called once per key-value when enumerating + * all keys inside a section. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ + typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); + + /* + * Creates a settings object. + * + * Return value: A pointer to a settings object, + * or null if a new settings object could not be allocated. + */ + Settings * settings_new(); + + /* + * Releases all memory held by a settings object. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * If the supplied settings object has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ + void settings_delete(Settings *settings); + + /* + * Constructs a settings object by loading settings in textual form + * from the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: A pointer to a settings object, + * or null if an error occurred. + */ + Settings * settings_open(FILE *stream); + + /* + * Saves the current settings object in textual form to the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: 1 if the operation succeeded, 0 otherwise. + */ + int settings_save(const Settings *settings, FILE *stream); + + /* + * Returns the value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ + int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); + + /* + * Returns the integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The integer value associated to the provided section and + * key, or 0 if no such value exists. + */ + int settings_get_int(const Settings *settings, const char *section, const char *key); + + /* + * Returns the long integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The long integer value associated to the provided section and + * key, or 0 if no such value exists. + */ + long settings_get_long(const Settings *settings, const char *section, const char *key); + + /* + * Returns the double value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The double value associated to the provided section and + * key, or 0 if no such value exists. + */ + double settings_get_double(const Settings *settings, const char *section, const char *key); + + /* + * Returns the integer tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ + int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); + + /* + * Returns the long tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ + long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); + + /* + * Returns the double tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ + double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); + + /* + * Associates a value with the supplied key in the provided section. + * If the key is already associated with a value, the previous value + * is replaced. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. The string must have a string length > 0. The string will + * be copied. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ + int settings_set(Settings *setting, const char *section, const char *key, const char *value); + + /* + * Returns the number of associations between keys and values that exist + * in the provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The number of associations between keys and values in + * the provided section. + */ + int settings_section_get_count(const Settings *settings, const char *section); + + /* + * Enumerates all associations between keys and values in the provided + * section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ + int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); #ifdef __cplusplus } diff --git a/src/strmap.c b/src/strmap.c index 5daa1ba..a08165b 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -6,13 +6,13 @@ * Version history: * 1.0.0 - initial release * 2.0.0 - changed function prefix from strmap to sm to ensure - * ANSI C compatibility + * ANSI C compatibility * * strmap.c * * Copyright (c) 2009, 2011 Per Ola Kristensson. * - * Per Ola Kristensson + * Per Ola Kristensson * Inference Group, Department of Physics * University of Cambridge * Cavendish Laboratory @@ -40,18 +40,18 @@ typedef struct Pair Pair; typedef struct Bucket Bucket; struct Pair { - char *key; - char *value; + char *key; + char *value; }; struct Bucket { - unsigned int count; - Pair *pairs; + unsigned int count; + Pair *pairs; }; struct StrMap { - unsigned int count; - Bucket *buckets; + unsigned int count; + Bucket *buckets; }; static Pair * get_pair(Bucket *bucket, const char *key); @@ -59,247 +59,246 @@ static unsigned long hash(const char *str); StrMap * sm_new(unsigned int capacity) { - StrMap *map; - - map = (StrMap*)malloc(sizeof(StrMap)); - if (map == NULL) { - return NULL; - } - map->count = capacity; - map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); - if (map->buckets == NULL) { - free(map); - return NULL; - } - memset(map->buckets, 0, map->count * sizeof(Bucket)); - return map; + StrMap *map; + + map = (StrMap*)malloc(sizeof(StrMap)); + if (map == NULL) { + return NULL; + } + map->count = capacity; + map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); + if (map->buckets == NULL) { + free(map); + return NULL; + } + memset(map->buckets, 0, map->count * sizeof(Bucket)); + return map; } void sm_delete(StrMap *map) { - unsigned int i, j, n, m; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return; - } - n = map->count; - bucket = map->buckets; - i = 0; - while (i < n) { - m = bucket->count; - pair = bucket->pairs; - j = 0; - while(j < m) { - free(pair->key); - free(pair->value); - pair++; - j++; - } - free(bucket->pairs); - bucket++; - i++; - } - free(map->buckets); - free(map); + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return; + } + n = map->count; + bucket = map->buckets; + i = 0; + while (i < n) { + m = bucket->count; + pair = bucket->pairs; + j = 0; + while(j < m) { + free(pair->key); + free(pair->value); + pair++; + j++; + } + free(bucket->pairs); + bucket++; + i++; + } + free(map->buckets); + free(map); } int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf) { - unsigned int index; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return 0; - } - if (key == NULL) { - return 0; - } - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - pair = get_pair(bucket, key); - if (pair == NULL) { - return 0; - } - if (out_buf == NULL && n_out_buf == 0) { - return strlen(pair->value) + 1; - } - if (out_buf == NULL) { - return 0; - } - if (strlen(pair->value) >= n_out_buf) { - return 0; - } - strcpy(out_buf, pair->value); - return 1; + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + if (out_buf == NULL && n_out_buf == 0) { + return strlen(pair->value) + 1; + } + if (out_buf == NULL) { + return 0; + } + if (strlen(pair->value) >= n_out_buf) { + return 0; + } + strcpy(out_buf, pair->value); + return 1; } int sm_exists(const StrMap *map, const char *key) { - unsigned int index; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return 0; - } - if (key == NULL) { - return 0; - } - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - pair = get_pair(bucket, key); - if (pair == NULL) { - return 0; - } - return 1; + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + return 1; } int sm_put(StrMap *map, const char *key, const char *value) { - unsigned int key_len, value_len, index; - Bucket *bucket; - Pair *tmp_pairs, *pair; - char *tmp_value; - char *new_key, *new_value; - - if (map == NULL) { - return 0; - } - if (key == NULL || value == NULL) { - return 0; - } - key_len = strlen(key); - value_len = strlen(value); - /* Get a pointer to the bucket the key string hashes to */ - index = hash(key) % map->count; - bucket = &(map->buckets[index]); - /* Check if we can handle insertion by simply replacing - * an existing value in a key-value pair in the bucket. - */ - if ((pair = get_pair(bucket, key)) != NULL) { - /* The bucket contains a pair that matches the provided key, - * change the value for that pair to the new value. - */ - if (strlen(pair->value) < value_len) { - /* If the new value is larger than the old value, re-allocate - * space for the new larger value. - */ - tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); - if (tmp_value == NULL) { - return 0; - } - pair->value = tmp_value; - } - /* Copy the new value into the pair that matches the key */ - strcpy(pair->value, value); - return 1; - } - /* Allocate space for a new key and value */ - new_key = (char*)malloc((key_len + 1) * sizeof(char)); - if (new_key == NULL) { - return 0; - } - new_value = (char*)malloc((value_len + 1) * sizeof(char)); - if (new_value == NULL) { - free(new_key); - return 0; - } - /* Create a key-value pair */ - if (bucket->count == 0) { - /* The bucket is empty, lazily allocate space for a single - * key-value pair. - */ - bucket->pairs = (Pair*)malloc(sizeof(Pair)); - if (bucket->pairs == NULL) { - free(new_key); - free(new_value); - return 0; - } - bucket->count = 1; - } - else { - /* The bucket wasn't empty but no pair existed that matches the provided - * key, so create a new key-value pair. - */ - tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); - if (tmp_pairs == NULL) { - free(new_key); - free(new_value); - return 0; - } - bucket->pairs = tmp_pairs; - bucket->count++; - } - /* Get the last pair in the chain for the bucket */ - pair = &(bucket->pairs[bucket->count - 1]); - pair->key = new_key; - pair->value = new_value; - /* Copy the key and its value into the key-value pair */ - strcpy(pair->key, key); - strcpy(pair->value, value); - return 1; + unsigned int key_len, value_len, index; + Bucket *bucket; + Pair *tmp_pairs, *pair; + char *tmp_value; + char *new_key, *new_value; + + if (map == NULL) { + return 0; + } + if (key == NULL || value == NULL) { + return 0; + } + key_len = strlen(key); + value_len = strlen(value); + /* Get a pointer to the bucket the key string hashes to */ + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + /* Check if we can handle insertion by simply replacing + * an existing value in a key-value pair in the bucket. + */ + if ((pair = get_pair(bucket, key)) != NULL) { + /* The bucket contains a pair that matches the provided key, + * change the value for that pair to the new value. + */ + if (strlen(pair->value) < value_len) { + /* If the new value is larger than the old value, re-allocate + * space for the new larger value. + */ + tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); + if (tmp_value == NULL) { + return 0; + } + pair->value = tmp_value; + } + /* Copy the new value into the pair that matches the key */ + strcpy(pair->value, value); + return 1; + } + /* Allocate space for a new key and value */ + new_key = (char*)malloc((key_len + 1) * sizeof(char)); + if (new_key == NULL) { + return 0; + } + new_value = (char*)malloc((value_len + 1) * sizeof(char)); + if (new_value == NULL) { + free(new_key); + return 0; + } + /* Create a key-value pair */ + if (bucket->count == 0) { + /* The bucket is empty, lazily allocate space for a single + * key-value pair. + */ + bucket->pairs = (Pair*)malloc(sizeof(Pair)); + if (bucket->pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->count = 1; + } else { + /* The bucket wasn't empty but no pair existed that matches the provided + * key, so create a new key-value pair. + */ + tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + if (tmp_pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->pairs = tmp_pairs; + bucket->count++; + } + /* Get the last pair in the chain for the bucket */ + pair = &(bucket->pairs[bucket->count - 1]); + pair->key = new_key; + pair->value = new_value; + /* Copy the key and its value into the key-value pair */ + strcpy(pair->key, key); + strcpy(pair->value, value); + return 1; } int sm_get_count(const StrMap *map) { - unsigned int i, j, n, m; - unsigned int count; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return 0; - } - bucket = map->buckets; - n = map->count; - i = 0; - count = 0; - while (i < n) { - pair = bucket->pairs; - m = bucket->count; - j = 0; - while (j < m) { - count++; - pair++; - j++; - } - bucket++; - i++; - } - return count; + unsigned int i, j, n, m; + unsigned int count; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + count = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + count++; + pair++; + j++; + } + bucket++; + i++; + } + return count; } int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) { - unsigned int i, j, n, m; - Bucket *bucket; - Pair *pair; - - if (map == NULL) { - return 0; - } - if (enum_func == NULL) { - return 0; - } - bucket = map->buckets; - n = map->count; - i = 0; - while (i < n) { - pair = bucket->pairs; - m = bucket->count; - j = 0; - while (j < m) { - enum_func(pair->key, pair->value, obj); - pair++; - j++; - } - bucket++; - i++; - } - return 1; + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (enum_func == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + enum_func(pair->key, pair->value, obj); + pair++; + j++; + } + bucket++; + i++; + } + return 1; } /* @@ -308,25 +307,25 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) */ static Pair * get_pair(Bucket *bucket, const char *key) { - unsigned int i, n; - Pair *pair; - - n = bucket->count; - if (n == 0) { - return NULL; - } - pair = bucket->pairs; - i = 0; - while (i < n) { - if (pair->key != NULL && pair->value != NULL) { - if (strcmp(pair->key, key) == 0) { - return pair; - } - } - pair++; - i++; - } - return NULL; + unsigned int i, n; + Pair *pair; + + n = bucket->count; + if (n == 0) { + return NULL; + } + pair = bucket->pairs; + i = 0; + while (i < n) { + if (pair->key != NULL && pair->value != NULL) { + if (strcmp(pair->key, key) == 0) { + return pair; + } + } + pair++; + i++; + } + return NULL; } /* @@ -334,13 +333,13 @@ static Pair * get_pair(Bucket *bucket, const char *key) */ static unsigned long hash(const char *str) { - unsigned long hash = 5381; - int c; + unsigned long hash = 5381; + int c; - while (c = *str++) { - hash = ((hash << 5) + hash) + c; - } - return hash; + while (c = *str++) { + hash = ((hash << 5) + hash) + c; + } + return hash; } /* diff --git a/src/strmap.h b/src/strmap.h index bd97b98..367f43c 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -6,13 +6,13 @@ * Version history: * 1.0.0 - initial release * 2.0.0 - changed function prefix from strmap to sm to ensure - * ANSI C compatibility + * ANSI C compatibility * * strmap.h * * Copyright (c) 2009, 2011 Per Ola Kristensson. * - * Per Ola Kristensson + * Per Ola Kristensson * Inference Group, Department of Physics * University of Cambridge * Cavendish Laboratory @@ -44,139 +44,139 @@ extern "C" #include #include -typedef struct StrMap StrMap; - -/* - * This callback function is called once per key-value when enumerating - * all keys associated to values. - * - * Parameters: - * - * key: A pointer to a null-terminated C string. The string must not - * be modified by the client. - * - * value: A pointer to a null-terminated C string. The string must - * not be modified by the client. - * - * obj: A pointer to a client-specific object. This parameter may be - * null. - * - * Return value: None. - */ -typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); - -/* - * Creates a string map. - * - * Parameters: - * - * capacity: The number of top-level slots this string map - * should allocate. This parameter must be > 0. - * - * Return value: A pointer to a string map object, - * or null if a new string map could not be allocated. - */ -StrMap * sm_new(unsigned int capacity); - -/* - * Releases all memory held by a string map object. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * If the supplied string map has been previously released, the - * behaviour of this function is undefined. - * - * Return value: None. - */ -void sm_delete(StrMap *map); - -/* - * Returns the value associated with the supplied key. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out_buf: A pointer to an output buffer which will contain the value, - * if it exists and fits into the buffer. - * - * n_out_buf: The size of the output buffer in bytes. - * - * Return value: If out_buf is set to null and n_out_buf is set to 0 the return - * value will be the number of bytes required to store the value (if it exists) - * and its null-terminator. For all other parameter configurations the return value - * is 1 if an associated value was found and completely copied into the output buffer, - * 0 otherwise. - */ -int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); - -/* - * Queries the existence of a key. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: 1 if the key exists, 0 otherwise. - */ -int sm_exists(const StrMap *map, const char *key); - -/* - * Associates a value with the supplied key. If the key is already - * associated with a value, the previous value is replaced. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * value: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * Return value: 1 if the association succeeded, 0 otherwise. - */ -int sm_put(StrMap *map, const char *key, const char *value); - -/* - * Returns the number of associations between keys and values. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * Return value: The number of associations between keys and values. - */ -int sm_get_count(const StrMap *map); - -/* - * Enumerates all associations between keys and values. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * enum_func: A pointer to a callback function that will be - * called by this procedure once for every key associated - * with a value. This parameter cannot be null. - * - * obj: A pointer to a client-specific object. This parameter will be - * passed back to the client's callback function. This parameter can - * be null. - * - * Return value: 1 if enumeration completed, 0 otherwise. - */ -int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); + typedef struct StrMap StrMap; + + /* + * This callback function is called once per key-value when enumerating + * all keys associated to values. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ + typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); + + /* + * Creates a string map. + * + * Parameters: + * + * capacity: The number of top-level slots this string map + * should allocate. This parameter must be > 0. + * + * Return value: A pointer to a string map object, + * or null if a new string map could not be allocated. + */ + StrMap * sm_new(unsigned int capacity); + + /* + * Releases all memory held by a string map object. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * If the supplied string map has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ + void sm_delete(StrMap *map); + + /* + * Returns the value associated with the supplied key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ + int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); + + /* + * Queries the existence of a key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: 1 if the key exists, 0 otherwise. + */ + int sm_exists(const StrMap *map, const char *key); + + /* + * Associates a value with the supplied key. If the key is already + * associated with a value, the previous value is replaced. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ + int sm_put(StrMap *map, const char *key, const char *value); + + /* + * Returns the number of associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * Return value: The number of associations between keys and values. + */ + int sm_get_count(const StrMap *map); + + /* + * Enumerates all associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ + int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); #ifdef __cplusplus } From 8ed60e90247037f5a60afbce1c0860a152831d57 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 22 Aug 2012 15:53:07 +0200 Subject: [PATCH 028/235] Fixes #14 and enhances Readme.md --- Makefile | 4 ++++ README.md | 30 ++++++++++++++++++------------ src/mbpfan.c | 4 ++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index cb26497..7e40233 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,10 @@ tests: make install sudo /usr/sbin/mbpfan -f -v -t +uninstall: + rm /usr/sbin/mbpfan + rm /etc/mbpfan.conf + install: make cp $(EXE) /usr/sbin diff --git a/README.md b/README.md index 06212d1..29ea2df 100644 --- a/README.md +++ b/README.md @@ -56,32 +56,38 @@ sudo service mbpfan start Starting at boot ---------------- -An init file suitable for /lib/lsb/init-functions (Debian & Ubuntu for sure) +**Ubuntu** +For upstart based init systems (Ubuntu), an example upstart job has been +provided for use in place of the LSB-style init script. +To use, execute: + sudo cp mbpfan.upstart /etc/init/mbpfan.conf + sudo start mbpfan + +**Debian** +An init file suitable for /lib/lsb/init-functions (Debian) is located in the main folder of the source files, called mbpfan.init.debian Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) and move it to /etc/init.d Then, add it to the default runlevels with sudo update-rc.d mbpfan defaults -Additionally, an init file suitable for /etc/rc.d/init.d/functions +**Redhat, CentOS, Fedora** +An init file suitable for /etc/rc.d/init.d/functions (RHEL/CentOS & Fedora) is also located at the same place, this file is called mbpfan.init.redhat. Also rename it to mbpfan, give it execution permissions and move it to /etc/init.d To add the script to the default runlevels, run the following as root: chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off -For upstart based init systems (Ubuntu), an example upstart job has been -provided for use in place of the LSB-style init script. To use, execute -as root: -cp mbpfan.upstart /etc/init/mbpfan.conf -start mbpfan +**systemd** As a special bonus, a service file for systemd is also included. To use it, execute the following as root: -cp mbpfan.service /usr/lib/systemd/system -ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service -systemctl daemon-reload -systemctl start mbpfan.service +sudo cp mbpfan.service /usr/lib/systemd/system +sudo ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service +sudo systemctl daemon-reload +sudo systemctl start mbpfan.service + To start the service automatically at boot, also execute the following: -systemctl enable mbpfan.service +sudo systemctl enable mbpfan.service Usage diff --git a/src/mbpfan.c b/src/mbpfan.c index 5cc30de..1953659 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -78,7 +78,7 @@ t_sensors *retrieve_sensors() const char *path_end = "_input"; int path_size = strlen(path_begin) + strlen(path_end) + 2; - char number[1]; + char number[2]; sprintf(number,"%d",0); int i = 0; @@ -132,7 +132,7 @@ void find_fans(t_sensors* sensors) int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; - char number[1]; + char number[2]; sprintf(number,"%d",0); int n_sensors = 0; From 37847f39d412357a83ff300221914a86060486f9 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 22 Aug 2012 16:02:03 +0200 Subject: [PATCH 029/235] Fixes #15 --- src/mbpfan.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 1953659..405a5d3 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -135,15 +135,16 @@ void find_fans(t_sensors* sensors) char number[2]; sprintf(number,"%d",0); + int counter = 0; int n_sensors = 0; int n_fans = 0; - for(n_sensors = 0; n_sensors<10; n_sensors++) { + for(counter = 0; counter<10; counter++) { path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output[0] = '\0'; path_manual = (char*) malloc(sizeof( char ) * path_man_size); path_manual[0] = '\0'; - sprintf(number,"%d",n_sensors); + sprintf(number,"%d",counter); strncat( path_output, path_begin, strlen(path_begin) ); strncat( path_output, number, strlen(number) ); @@ -165,14 +166,15 @@ void find_fans(t_sensors* sensors) strcpy(tmp->fan_manual_path, path_manual); tmp = tmp->next; n_fans++; + n_sensors++; fclose(file); } } if(verbose) { - printf("Found %d: sensors and %d fans\n", n_sensors, n_fans); + printf("Found %d sensors and %d fans\n", n_sensors, n_fans); if(daemonize) { - syslog(LOG_INFO, "Found %d: sensors and %d fans", n_sensors, n_fans); + syslog(LOG_INFO, "Found %d sensors and %d fans", n_sensors, n_fans); } } From 1f8131bd9def66e6a08e1e5908815d3f38235324 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 23 Aug 2012 22:32:49 +0200 Subject: [PATCH 030/235] Fixes #18. Seems ready for v1.4.0 --- README.md | 7 +- mbpfan.init.debian | 0 mbpfan.init.gentoo | 0 mbpfan.init.redhat | 0 ...ained => mbpfan.rc.archlinux-notmaintained | 5 +- src/daemon.c | 15 +++ src/daemon.h | 6 +- src/global.h | 16 ++- src/main.c | 5 + src/mbpfan.c | 72 ++++++++++-- src/mbpfan.h | 7 +- src/minunit.c | 43 +++++-- src/minunit.h | 30 +++-- src/settings.c | 110 ++++++++++++++++++ src/strmap.c | 54 +++++++++ 15 files changed, 323 insertions(+), 47 deletions(-) mode change 100755 => 100644 mbpfan.init.debian mode change 100755 => 100644 mbpfan.init.gentoo mode change 100755 => 100644 mbpfan.init.redhat rename mbpfan.rc-not-maintained => mbpfan.rc.archlinux-notmaintained (91%) diff --git a/README.md b/README.md index 2137929..f55cd19 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,7 @@ Fan-Control-Daemon Introduction --------------------- -This is an enhanced version of [rvega's Fan-Control-Daemon](https://github.com/rvega/Fan-Control-Daemon), -which itself is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) +This is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). @@ -50,10 +49,6 @@ Compile with make -Manually compile with - - gcc -o bin/mbpfan src/mbpfan.c -lm - Install Instructions -------------------- diff --git a/mbpfan.init.debian b/mbpfan.init.debian old mode 100755 new mode 100644 diff --git a/mbpfan.init.gentoo b/mbpfan.init.gentoo old mode 100755 new mode 100644 diff --git a/mbpfan.init.redhat b/mbpfan.init.redhat old mode 100755 new mode 100644 diff --git a/mbpfan.rc-not-maintained b/mbpfan.rc.archlinux-notmaintained similarity index 91% rename from mbpfan.rc-not-maintained rename to mbpfan.rc.archlinux-notmaintained index 412f154..799e3d4 100644 --- a/mbpfan.rc-not-maintained +++ b/mbpfan.rc.archlinux-notmaintained @@ -1,6 +1,7 @@ #!/bin/bash - -# not maintained in this proejct +# Archlinux rc script +# not maintained in this project +# please, fix it and contribute back to the project daemon_name=mbpfan diff --git a/src/daemon.c b/src/daemon.c index e1bc98e..56d563c 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -34,10 +34,12 @@ int write_pid(int pid) { FILE *file = NULL; file = fopen(program_pid, "w"); + if(file != NULL) { fprintf(file, "%d", pid); fclose(file); return 1; + } else { return 0; } @@ -48,11 +50,13 @@ int read_pid() FILE *file = NULL; int pid = -1; file = fopen(program_pid, "r"); + if(file != NULL) { fscanf(file, "%d", &pid); fclose(file); return pid; } + return -1; } @@ -103,6 +107,7 @@ void go_daemon(void (*fan_control)()) if(verbose) { setlogmask(LOG_UPTO(LOG_DEBUG)); openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + } else { setlogmask(LOG_UPTO(LOG_INFO)); openlog(program_name, LOG_CONS, LOG_USER); @@ -115,9 +120,11 @@ void go_daemon(void (*fan_control)()) if (daemonize) { pid_slave = fork(); + if (pid_slave < 0) { exit(EXIT_FAILURE); } + if (pid_slave > 0) { // kill the father exit(EXIT_SUCCESS); @@ -127,6 +134,7 @@ void go_daemon(void (*fan_control)()) // new sid_slave for the child process sid_slave = setsid(); + if (sid_slave < 0) { exit(EXIT_FAILURE); } @@ -151,23 +159,30 @@ void go_daemon(void (*fan_control)()) printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid); syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); } + if (write_pid(current_pid) == 0) { syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); + if (verbose) { printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid); } + exit(EXIT_FAILURE); + } else { if (verbose) { printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid); syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); } } + } else { syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); + if (verbose) { printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); } + exit(EXIT_FAILURE); } diff --git a/src/daemon.h b/src/daemon.h index 0f76787..b20a185 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -13,6 +13,8 @@ * */ +#ifndef _DAEMON_H_ +#define _DAEMON_H_ /** * Write the PID of the forked daemon to the @@ -47,4 +49,6 @@ void signal_handler(int signal); /** * Daemonizes */ -int go_daemon(void (*function)()); \ No newline at end of file +int go_daemon(void (*function)()); + +#endif \ No newline at end of file diff --git a/src/global.h b/src/global.h index e1dd842..d4bea44 100644 --- a/src/global.h +++ b/src/global.h @@ -1,5 +1,19 @@ +#ifndef _GLOBAL_H_ +#define _GLOBAL_H_ + extern int daemonize; extern int verbose; extern const char* program_name; -extern const char* program_pid; \ No newline at end of file +extern const char* program_pid; + +struct s_sensors { + char* path; + char* fan_output_path; + char* fan_manual_path; + unsigned int temperature; + struct s_sensors *next; +}; + +typedef s_sensors t_sensors; +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 30253b8..ff50a26 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,10 @@ * */ +/** + * Code formatted with astyle -A3 -s --break-blocks=all --add-brackets *.c *.h + */ + #include #include #include @@ -46,6 +50,7 @@ int main(int argc, char *argv[]) { int c; + while( (c = getopt(argc, argv, "hftv|help")) != -1) { switch(c) { case 'h': diff --git a/src/mbpfan.c b/src/mbpfan.c index 405a5d3..d454ccd 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -57,7 +57,7 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; - +/* struct s_sensors { char* path; char* fan_output_path; @@ -65,6 +65,9 @@ struct s_sensors { unsigned int temperature; struct s_sensors *next; }; + */ + +typedef struct s_sensors t_sensors; t_sensors *retrieve_sensors() @@ -82,6 +85,7 @@ t_sensors *retrieve_sensors() sprintf(number,"%d",0); int i = 0; + for(i = 0; i<10; i++) { path = (char*) malloc(sizeof( char ) * path_size); @@ -98,24 +102,33 @@ t_sensors *retrieve_sensors() s->path = (char *) malloc(sizeof( char ) * path_size); strcpy(s->path, path); fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) { sensors_head = s; sensors_head->next = NULL; + } else { t_sensors *tmp = sensors_head; + while (tmp->next != NULL) { tmp = tmp->next; } + tmp->next = s; tmp->next->next = NULL; } + fclose(file); } + free(path); path = NULL; } - if(sensors_head != NULL) + + if(sensors_head != NULL) { find_fans(sensors_head); + } + return sensors_head; } @@ -162,6 +175,7 @@ void find_fans(t_sensors* sensors) tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); } + strcpy(tmp->fan_output_path, path_output); strcpy(tmp->fan_manual_path, path_manual); tmp = tmp->next; @@ -173,6 +187,7 @@ void find_fans(t_sensors* sensors) if(verbose) { printf("Found %d sensors and %d fans\n", n_sensors, n_fans); + if(daemonize) { syslog(LOG_INFO, "Found %d sensors and %d fans", n_sensors, n_fans); } @@ -189,12 +204,15 @@ void set_fans_man(t_sensors *sensors) t_sensors *tmp = sensors; FILE *file; + while(tmp != NULL) { file = fopen(tmp->fan_manual_path, "rw+"); + if(file != NULL) { fprintf(file, "%d", 1); fclose(file); } + tmp = tmp->next; } } @@ -214,6 +232,7 @@ t_sensors *refresh_sensors(t_sensors *sensors) tmp = tmp->next; } + return sensors; } @@ -223,8 +242,10 @@ void set_fan_speed(t_sensors* sensors, int speed) { t_sensors *tmp = sensors; FILE *file; + while(tmp != NULL) { file = fopen(tmp->fan_output_path, "rw+"); + if(file != NULL) { fprintf(file, "%d", speed); fclose(file); @@ -243,10 +264,12 @@ unsigned short get_temp(t_sensors* sensors) unsigned short temp = 0; t_sensors* tmp = sensors; + while(tmp != NULL) { sum_temp += tmp->temperature; tmp = tmp->next; } + temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); return temp; } @@ -262,35 +285,63 @@ void retrieve_settings() /* Could not open configfile */ if(verbose) { printf("Couldn't open configfile, using defaults\n"); + if(daemonize) { syslog(LOG_INFO, "Couldn't open configfile, using defaults"); } } + } else { settings = settings_open(f); fclose(f); + if (settings == NULL) { /* Could not read configfile */ if(verbose) { printf("Couldn't read configfile\n"); + if(daemonize) { syslog(LOG_INFO, "Couldn't read configfile"); } } + } else { /* Read configfile values */ result = settings_get_int(settings, "general", "min_fan_speed"); - if (result != 0) min_fan_speed = result; + + if (result != 0) { + min_fan_speed = result; + } + result = settings_get_int(settings, "general", "max_fan_speed"); - if (result != 0) max_fan_speed = result; + + if (result != 0) { + max_fan_speed = result; + } + result = settings_get_int(settings, "general", "low_temp"); - if (result != 0) low_temp = result; + + if (result != 0) { + low_temp = result; + } + result = settings_get_int(settings, "general", "high_temp"); - if (result != 0) high_temp = result; + + if (result != 0) { + high_temp = result; + } + result = settings_get_int(settings, "general", "max_temp"); - if (result != 0) max_temp = result; + + if (result != 0) { + max_temp = result; + } + result = settings_get_int(settings, "general", "polling_interval"); - if (result != 0) polling_interval = result; + + if (result != 0) { + polling_interval = result; + } /* Destroy the settings object */ settings_delete(settings); @@ -316,10 +367,12 @@ void mbpfan() if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } + sleep(polling_interval); step_up = (float)( max_fan_speed - min_fan_speed ) / @@ -354,6 +407,7 @@ void mbpfan() if(verbose) { printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(daemonize) { syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); } @@ -363,10 +417,12 @@ void mbpfan() if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); + if(daemonize) { syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } + sleep(polling_interval); } } diff --git a/src/mbpfan.h b/src/mbpfan.h index a51fcd2..c456b5e 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -14,6 +14,9 @@ * */ +#ifndef _MBPFAN_H_ +#define _MBPFAN_H_ + /** Basic fan speed parameters */ extern int min_fan_speed; @@ -82,4 +85,6 @@ unsigned short get_temp(t_sensors* sensors); /** * Main Program */ -void mbpfan(); \ No newline at end of file +void mbpfan(); + +#endif \ No newline at end of file diff --git a/src/minunit.c b/src/minunit.c index 9b43ce9..ef22695 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -3,6 +3,7 @@ #include #include #include +#include "global.h" #include "mbpfan.h" #include "settings.h" #include "minunit.h" @@ -10,32 +11,41 @@ int tests_run = 0; -static char *test_sensor_paths() +static const char *test_sensor_paths() { t_sensors* sensors = retrieve_sensors(); mu_assert("No sensors found", sensors != NULL); t_sensors* tmp = sensors; + while(tmp != NULL) { mu_assert("Sensor does not have a valid path", tmp->path != NULL); - if(tmp->path != NULL) + + if(tmp->path != NULL) { mu_assert("Sensor does not have valid temperature", tmp->temperature > 0); + } + tmp = tmp->next; } + return 0; } -static char *test_fan_paths() +static const char *test_fan_paths() { t_sensors* sensors = retrieve_sensors(); mu_assert("No sensors found", sensors != NULL); t_sensors* tmp = sensors; int found_fan_path = 0; + while(tmp != NULL) { - if(tmp->fan_output_path != NULL) + if(tmp->fan_output_path != NULL) { found_fan_path++; + } + tmp = tmp->next; } + mu_assert("No fans found", found_fan_path != 0); return 0; } @@ -46,8 +56,11 @@ unsigned time_seed() unsigned char *p = (unsigned char *)&now; unsigned seed = 0; size_t i; - for ( i = 0; i < sizeof now; i++ ) + + for ( i = 0; i < sizeof now; i++ ) { seed = seed * ( UCHAR_MAX + 2U ) + p[i]; + } + return seed; } @@ -56,17 +69,19 @@ unsigned time_seed() int stress(int n) { int f = n; + while (f > 0) { while(n > 0) { srand ( time_seed() ); n--; } + f--; n = f; } } -static char *test_get_temp() +static const char *test_get_temp() { t_sensors* sensors = retrieve_sensors(); mu_assert("No sensors found", sensors != NULL); @@ -78,21 +93,24 @@ static char *test_get_temp() return 0; } -static char *test_config_file() +static const char *test_config_file() { FILE *f = NULL; Settings *settings = NULL; f = fopen("/etc/mbpfan.conf", "r"); mu_assert("No config file found", f != NULL); - if (f == NULL) + if (f == NULL) { return 0; + } settings = settings_open(f); fclose(f); mu_assert("Could not read settings from config file", settings != NULL); - if (settings == NULL) + + if (settings == NULL) { return 0; + } mu_assert("Could not read min_fan_speed from config file",settings_get_int(settings, "general", "min_fan_speed") != 0); mu_assert("Could not read max_fan_speed from config file",settings_get_int(settings, "general", "max_fan_speed") != 0); @@ -107,7 +125,7 @@ static char *test_config_file() } -static char *all_tests() +static const char *all_tests() { mu_run_test(test_sensor_paths); mu_run_test(test_fan_paths); @@ -120,12 +138,15 @@ int tests() { printf("Starting the tests..\n"); printf("It is normal for them to take a bit to finish.\n"); - char *result = all_tests(); + const char *result = all_tests(); + if (result != 0) { printf("%s \n", result); + } else { printf("ALL TESTS PASSED\n"); } + printf("Tests run: %d\n", tests_run); return result != 0; diff --git a/src/minunit.h b/src/minunit.h index a83e6de..5fbff28 100644 --- a/src/minunit.h +++ b/src/minunit.h @@ -1,27 +1,23 @@ /** * This is the MinUnit testing framework - http://www.jera.com/techinfo/jtns/jtn002.html */ -#define mu_assert(message, test) do { if (!(test)) return message; } while (0) -#define mu_run_test(test) do { char *message = test(); tests_run++; \ - if (message) return message; } while (0) -struct s_sensors { - char* path; - char* fan_output_path; - char* fan_manual_path; - unsigned int temperature; - struct s_sensors *next; -}; -typedef s_sensors t_sensors; +#ifndef _MINUNIT_H_ +#define _MINUNIT_H_ +#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_run_test(test) do { const char *message = test(); tests_run++; \ + if (message) return message; } while (0) extern int tests_run; -static char *test_sensor_paths(); -static char *test_fan_paths(); -static char *test_get_temp(); -static char *test_config_file(); +static const char *test_sensor_paths(); +static const char *test_fan_paths(); +static const char *test_get_temp(); +static const char *test_config_file(); + +static const char *all_tests(); -static char *all_tests(); +int tests(); -int tests(); \ No newline at end of file +#endif \ No newline at end of file diff --git a/src/settings.c b/src/settings.c index 5af8932..ae108f2 100644 --- a/src/settings.c +++ b/src/settings.c @@ -97,9 +97,11 @@ Settings * settings_new() Settings *settings; settings = (Settings*)malloc(sizeof(Settings)); + if (settings == NULL) { return NULL; } + settings->section_count = 0; settings->sections = NULL; return settings; @@ -113,17 +115,22 @@ void settings_delete(Settings *settings) if (settings == NULL) { return; } + section = settings->sections; n = settings->section_count; i = 0; + while (i < n) { sm_delete(section->map); + if (section->name != NULL) { free(section->name); } + section++; i++; } + free(settings->sections); free(settings); } @@ -139,20 +146,26 @@ Settings * settings_open(FILE *stream) if (stream == NULL) { return NULL; } + settings = settings_new(); + if (settings == NULL) { return NULL; } + parse_state.current_section = section_buf; parse_state.current_section_n = sizeof(section_buf); parse_state.has_section = 0; trim_str("", trimmed_buf); + while (fgets(buf, MAX_LINECHARS, stream) != NULL) { trim_str(buf, trimmed_buf); + if (!parse_str(settings, trimmed_buf, &parse_state)) { return NULL; } } + return settings; } @@ -165,12 +178,15 @@ int settings_save(const Settings *settings, FILE *stream) if (settings == NULL) { return 0; } + if (stream == NULL) { return 0; } + section = settings->sections; n = settings->section_count; i = 0; + while (i < n) { sprintf(buf, "[%s]\n", section->name); fputs(buf, stream); @@ -179,6 +195,7 @@ int settings_save(const Settings *settings, FILE *stream) i++; fputs("\n", stream); } + return 0; } @@ -189,10 +206,13 @@ int settings_get(const Settings *settings, const char *section, const char *key, if (settings == NULL) { return 0; } + s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { return 0; } + return sm_get(s->map, key, out_buf, n_out_buf); } @@ -203,6 +223,7 @@ int settings_get_int(const Settings *settings, const char *section, const char * if (get_converted_value(settings, section, key, CONVERT_MODE_INT, &i)) { return i; } + return 0; } @@ -213,6 +234,7 @@ long settings_get_long(const Settings *settings, const char *section, const char if (get_converted_value(settings, section, key, CONVERT_MODE_LONG, &l)) { return l; } + return 0; } @@ -223,6 +245,7 @@ double settings_get_double(const Settings *settings, const char *section, const if (get_converted_value(settings, section, key, CONVERT_MODE_DOUBLE, &d)) { return d; } + return 0; } @@ -248,36 +271,47 @@ int settings_set(Settings *settings, const char *section, const char *key, const if (settings == NULL) { return 0; } + if (section == NULL || key == NULL || value == NULL) { return 0; } + if (strlen(section) == 0) { return 0; } + /* Get a pointer to the section */ s = get_section(settings->sections, settings->section_count, section); + if (s == NULL) { /* The section is not created---create it */ s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + if (s == NULL) { return 0; } + settings->sections = s; settings->section_count++; s = &(settings->sections[settings->section_count - 1]); s->map = sm_new(DEFAULT_STRMAP_CAPACITY); + if (s->map == NULL) { free(s); return 0; } + s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); + if (s->name == NULL) { sm_delete(s->map); free(s); return 0; } + strcpy(s->name, section); } + return sm_put(s->map, key, value); } @@ -288,10 +322,13 @@ int settings_section_get_count(const Settings *settings, const char *section) if (settings == NULL) { return 0; } + sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { return 0; } + return sm_get_count(sect->map); } @@ -300,9 +337,11 @@ int settings_section_enum(const Settings *settings, const char *section, setting Section *sect; sect = get_section(settings->sections, settings->section_count, section); + if (sect == NULL) { return 0; } + return sm_enum(sect->map, enum_func, obj); } @@ -318,19 +357,24 @@ static void trim_str(const char *str, char *out_buf) while (*str != '\0' && is_blank_char(*str)) { str++; } + s0 = str; len = 0; + while (*str != '\0') { len++; str++; } + if (len > 0) { str--; } + while (is_blank_char(*str)) { str--; len--; } + memcpy(out_buf, s0, len); out_buf[len] = '\0'; } @@ -355,39 +399,54 @@ static int parse_str(Settings *settings, char *str, ParseState *parse_state) if (*str == '\0') { return 1; + } else if (is_blank_str(str)) { return 1; + } else if (is_comment_str(str)) { return 1; + } else if (is_section_str(str)) { result = get_section_from_str(str, buf, sizeof(buf)); + if (!result) { return 0; } + if (strlen(buf) + 1 > parse_state->current_section_n) { return 0; } + strcpy(parse_state->current_section, buf); parse_state->has_section = 1; return 1; + } else if (is_key_value_str(str)) { result = get_key_value_from_str(str, buf1, sizeof(buf1), buf2, sizeof(buf2)); + if (!result) { return 0; } + if (!parse_state->has_section) { return 0; } + return settings_set(settings, parse_state->current_section, buf1, buf2); + } else if (is_key_without_value_str(str)) { result = get_key_without_value_from_str(str, buf, sizeof(buf)); + if (!result) { return 0; } + if (!parse_state->has_section) { return 0; } + return settings_set(settings, parse_state->current_section, buf, ""); + } else { return 0; } @@ -410,8 +469,10 @@ static int is_blank_str(const char *str) if (!is_blank_char(*str)) { return 0; } + str++; } + return 1; } @@ -426,6 +487,7 @@ static int is_comment_str(const char *str) */ return 1; } + return 0; } @@ -438,13 +500,16 @@ static int is_section_str(const char *str) /* The first character must be the section start character */ return 0; } + while (*str != '\0' && *str != SECTION_END_CHAR) { str++; } + if (*str != SECTION_END_CHAR) { /* The section end character must be present somewhere thereafter */ return 0; } + return 1; } @@ -457,13 +522,16 @@ static int is_key_value_str(const char *str) /* It is illegal to start with the key-value separator */ return 0; } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { str++; } + if (*str != KEY_VALUE_SEPARATOR_CHAR) { /* The key-value separator must be present after the key part */ return 0; } + return 1; } @@ -476,13 +544,16 @@ static int is_key_without_value_str(const char *str) /* It is illegal to start with the key-value separator */ return 0; } + while (*str != '\0' && *str != KEY_VALUE_SEPARATOR_CHAR) { str++; } + if (*str == KEY_VALUE_SEPARATOR_CHAR) { /* The key-value separator must not be present after the key part */ return 0; } + return 1; } @@ -497,20 +568,24 @@ static int get_section_from_str(const char *str, char *out_buf, unsigned int out count = 0; /* Jump past the section begin character */ str++; + while (*str != '\0' && *str != SECTION_END_CHAR) { /* Read in the section name into the output buffer */ if (count == out_buf_n) { return 0; } + *out_buf = *str; out_buf++; str++; count++; } + /* Terminate the output buffer */ if (count == out_buf_n) { return 0; } + *out_buf = '\0'; return 1; } @@ -526,6 +601,7 @@ static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int count1 = 0; count2 = 0; + /* Read the key value from the input string and write it sequentially * to the first output buffer by walking the input string until we either hit * the null-terminator or the key-value separator. @@ -535,34 +611,42 @@ static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int if (count1 == out_buf1_n) { return 0; } + /* Copy the character to the first output buffer */ *out_buf1 = *str; out_buf1++; str++; count1++; } + /* Terminate the first output buffer */ if (count1 == out_buf1_n) { return 0; } + *out_buf1 = '\0'; + /* Now trace the first input buffer backwards until we hit a non-blank character */ while (is_blank_char(*(out_buf1 - 1))) { out_buf1--; } + *out_buf1 = '\0'; + /* Try to proceed one more character, past the last read key-value * delimiter, in the input string. */ if (*str != '\0') { str++; } + /* Now find start of the value in the input string by walking the input * string until we either hit the null-terminator or a blank character. */ while (*str != '\0' && is_blank_char(*str)) { str++; } + while (*str != '\0') { /* Fail if there is a possibility that we are overwriting the second * input buffer. @@ -570,16 +654,19 @@ static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int if (count2 == out_buf2_n) { return 0; } + /* Copy the character to the second output buffer */ *out_buf2 = *str; out_buf2++; str++; count2++; } + /* Terminate the second output buffer */ if (count2 == out_buf2_n) { return 0; } + *out_buf2 = '\0'; return 1; } @@ -593,6 +680,7 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign unsigned int count; count = 0; + /* Now read the key value from the input string and write it sequentially * to the output buffer by walking the input string until we either hit * the null-terminator or the key-value separator. @@ -602,16 +690,19 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign if (count == out_buf_n) { return 0; } + /* Copy the character to the input buffer */ *out_buf = *str; out_buf++; str++; count++; } + /* Terminate the output buffer */ if (count == out_buf_n) { return 0; } + *out_buf = '\0'; return 1; } @@ -638,6 +729,7 @@ static const char * get_token(char *str, char delim, char **last) char *s0; s0 = str; + /* If we hit the null-terminator the string * is exhausted and another token does not * exist. @@ -645,17 +737,20 @@ static const char * get_token(char *str, char delim, char **last) if (*str == '\0') { return NULL; } + /* Walk the string until we encounter a * null-terminator or the delimiter. */ while (*str != '\0' && *str != delim) { str++; } + /* Terminate the return token, if necessary */ if (*str != '\0') { *str = '\0'; str++; } + *last = str; return s0; } @@ -673,6 +768,7 @@ static int get_converted_value(const Settings *settings, const char *section, co if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { return 0; } + switch (mode) { case CONVERT_MODE_INT: *((int *)out) = atoi(value); @@ -684,6 +780,7 @@ static int get_converted_value(const Settings *settings, const char *section, co *((double *)out) = atof(value); return 1; } + return 0; } @@ -704,14 +801,18 @@ static int get_converted_tuple(const Settings *settings, const char *section, co if (out == NULL) { return 0; } + if (n_out == 0) { return 0; } + if (!settings_get(settings, section, key, value, MAX_VALUECHARS)) { return 0; } + v = value; count = 0; + /* Walk over all tokens in the value, and convert them and assign them * to the output array as specified by the mode. */ @@ -729,8 +830,10 @@ static int get_converted_tuple(const Settings *settings, const char *section, co default: return 0; } + count++; } + return 1; } @@ -745,15 +848,19 @@ static Section * get_section(Section *sections, unsigned int n, const char *name if (name == NULL) { return NULL; } + section = sections; i = 0; + while (i < n) { if (strcmp(section->name, name) == 0) { return section; } + section++; i++; } + return NULL; } @@ -769,10 +876,13 @@ static void enum_map(const char *key, const char *value, const void *obj) if (key == NULL || value == NULL) { return; } + if (obj == NULL) { return; } + stream = (FILE *)obj; + if (strlen(key) < MAX_KEYCHARS && strlen(value) < MAX_VALUECHARS) { sprintf(buf, "%s%c%s\n", key, KEY_VALUE_SEPARATOR_CHAR, value); fputs(buf, stream); diff --git a/src/strmap.c b/src/strmap.c index a08165b..d553b44 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -62,15 +62,19 @@ StrMap * sm_new(unsigned int capacity) StrMap *map; map = (StrMap*)malloc(sizeof(StrMap)); + if (map == NULL) { return NULL; } + map->count = capacity; map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); + if (map->buckets == NULL) { free(map); return NULL; } + memset(map->buckets, 0, map->count * sizeof(Bucket)); return map; } @@ -84,23 +88,28 @@ void sm_delete(StrMap *map) if (map == NULL) { return; } + n = map->count; bucket = map->buckets; i = 0; + while (i < n) { m = bucket->count; pair = bucket->pairs; j = 0; + while(j < m) { free(pair->key); free(pair->value); pair++; j++; } + free(bucket->pairs); bucket++; i++; } + free(map->buckets); free(map); } @@ -114,24 +123,31 @@ int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out if (map == NULL) { return 0; } + if (key == NULL) { return 0; } + index = hash(key) % map->count; bucket = &(map->buckets[index]); pair = get_pair(bucket, key); + if (pair == NULL) { return 0; } + if (out_buf == NULL && n_out_buf == 0) { return strlen(pair->value) + 1; } + if (out_buf == NULL) { return 0; } + if (strlen(pair->value) >= n_out_buf) { return 0; } + strcpy(out_buf, pair->value); return 1; } @@ -145,15 +161,19 @@ int sm_exists(const StrMap *map, const char *key) if (map == NULL) { return 0; } + if (key == NULL) { return 0; } + index = hash(key) % map->count; bucket = &(map->buckets[index]); pair = get_pair(bucket, key); + if (pair == NULL) { return 0; } + return 1; } @@ -168,14 +188,17 @@ int sm_put(StrMap *map, const char *key, const char *value) if (map == NULL) { return 0; } + if (key == NULL || value == NULL) { return 0; } + key_len = strlen(key); value_len = strlen(value); /* Get a pointer to the bucket the key string hashes to */ index = hash(key) % map->count; bucket = &(map->buckets[index]); + /* Check if we can handle insertion by simply replacing * an existing value in a key-value pair in the bucket. */ @@ -188,50 +211,64 @@ int sm_put(StrMap *map, const char *key, const char *value) * space for the new larger value. */ tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); + if (tmp_value == NULL) { return 0; } + pair->value = tmp_value; } + /* Copy the new value into the pair that matches the key */ strcpy(pair->value, value); return 1; } + /* Allocate space for a new key and value */ new_key = (char*)malloc((key_len + 1) * sizeof(char)); + if (new_key == NULL) { return 0; } + new_value = (char*)malloc((value_len + 1) * sizeof(char)); + if (new_value == NULL) { free(new_key); return 0; } + /* Create a key-value pair */ if (bucket->count == 0) { /* The bucket is empty, lazily allocate space for a single * key-value pair. */ bucket->pairs = (Pair*)malloc(sizeof(Pair)); + if (bucket->pairs == NULL) { free(new_key); free(new_value); return 0; } + bucket->count = 1; + } else { /* The bucket wasn't empty but no pair existed that matches the provided * key, so create a new key-value pair. */ tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + if (tmp_pairs == NULL) { free(new_key); free(new_value); return 0; } + bucket->pairs = tmp_pairs; bucket->count++; } + /* Get the last pair in the chain for the bucket */ pair = &(bucket->pairs[bucket->count - 1]); pair->key = new_key; @@ -252,22 +289,27 @@ int sm_get_count(const StrMap *map) if (map == NULL) { return 0; } + bucket = map->buckets; n = map->count; i = 0; count = 0; + while (i < n) { pair = bucket->pairs; m = bucket->count; j = 0; + while (j < m) { count++; pair++; j++; } + bucket++; i++; } + return count; } @@ -280,24 +322,30 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) if (map == NULL) { return 0; } + if (enum_func == NULL) { return 0; } + bucket = map->buckets; n = map->count; i = 0; + while (i < n) { pair = bucket->pairs; m = bucket->count; j = 0; + while (j < m) { enum_func(pair->key, pair->value, obj); pair++; j++; } + bucket++; i++; } + return 1; } @@ -311,20 +359,25 @@ static Pair * get_pair(Bucket *bucket, const char *key) Pair *pair; n = bucket->count; + if (n == 0) { return NULL; } + pair = bucket->pairs; i = 0; + while (i < n) { if (pair->key != NULL && pair->value != NULL) { if (strcmp(pair->key, key) == 0) { return pair; } } + pair++; i++; } + return NULL; } @@ -339,6 +392,7 @@ static unsigned long hash(const char *str) while (c = *str++) { hash = ((hash << 5) + hash) + c; } + return hash; } From e60498a8a6df0fed961962e8736ea5ae8fd2160a Mon Sep 17 00:00:00 2001 From: CeRiAl Date: Mon, 27 Aug 2012 12:12:17 +0200 Subject: [PATCH 031/235] Formatting fix for AUTHORS --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 941e824..3f50d62 100644 --- a/AUTHORS +++ b/AUTHORS @@ -4,7 +4,7 @@ AUTHORS, CONTRIBUTORS, MAINTAINERS Daniel Graziotin Ismail Khatib Trevor Joynson -Magnus Stubman +Magnus Stubman ORIGINARY AUTHORS From 4dbce6bcb353940bfcc77f13fa0bcb14713f3112 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 19 Oct 2012 18:25:10 +0200 Subject: [PATCH 032/235] Fixes #23 --- src/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.c b/src/main.c index ff50a26..31378d7 100644 --- a/src/main.c +++ b/src/main.c @@ -24,6 +24,7 @@ #include "daemon.h" #include "global.h" #include "minunit.h" +#include int daemonize = 1; int verbose = 0; @@ -74,6 +75,13 @@ int main(int argc, char *argv[]) } } + uid_t uid=getuid(), euid=geteuid(); + if (!(uid<0 || uid!=euid)) { + syslog(LOG_INFO, "Mbpfan not started with root privileges. Exiting."); + printf("Mbpfan not started with root privileges. Exiting.\n"); + exit(0); + } + // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; go_daemon(fan_control); From d9aee40c707ee046c4848036dbd3ad51e2b7fbeb Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 19 Oct 2012 20:13:07 +0200 Subject: [PATCH 033/235] Fixes #22. Also, really fixes #23, sorry --- src/main.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 31378d7..0d76b30 100644 --- a/src/main.c +++ b/src/main.c @@ -75,13 +75,39 @@ int main(int argc, char *argv[]) } } + /** + * Check for root + */ + uid_t uid=getuid(), euid=geteuid(); - if (!(uid<0 || uid!=euid)) { - syslog(LOG_INFO, "Mbpfan not started with root privileges. Exiting."); - printf("Mbpfan not started with root privileges. Exiting.\n"); + + if (uid != 0 || euid != 0) { + syslog(LOG_INFO, "Mbpfan needs root privileges. Please run mbpfan as root. Exiting."); + printf("Mbpfan not started with root privileges. Please run mbpfan as root. Exiting.\n"); exit(0); } + /** + * Check for coretemp and applesmc modules + * Credits: http://stackoverflow.com/questions/12978794 + */ + + FILE *fd = popen("lsmod | grep coretemp", "r"); + + char buf[16]; + + if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { + syslog(LOG_INFO, "Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting."); + printf("Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting.\n"); + } + + fd = popen("lsmod | grep applesmc", "r"); + + if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { + syslog(LOG_INFO, "Mbpfan needs applesmc module. Please load it and run mbpfan again. Exiting."); + printf("Mbpfan needs applescm module. Please load it and run mbpfan again. Exiting.\n"); + } + // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; go_daemon(fan_control); From ac4178b36e5f3122e6f029036a5958ad91f4dd8a Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 19 Oct 2012 20:37:08 +0200 Subject: [PATCH 034/235] I must be drunk or something. Fixes #22 --- src/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.c b/src/main.c index 0d76b30..c3783eb 100644 --- a/src/main.c +++ b/src/main.c @@ -99,6 +99,7 @@ int main(int argc, char *argv[]) if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { syslog(LOG_INFO, "Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting."); printf("Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting.\n"); + exit(0); } fd = popen("lsmod | grep applesmc", "r"); @@ -106,6 +107,7 @@ int main(int argc, char *argv[]) if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { syslog(LOG_INFO, "Mbpfan needs applesmc module. Please load it and run mbpfan again. Exiting."); printf("Mbpfan needs applescm module. Please load it and run mbpfan again. Exiting.\n"); + exit(0); } // pointer to mbpfan() function in mbpfan.c From eda84439061d02654843bab891acb2798af02771 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 20 Oct 2012 17:18:30 +0200 Subject: [PATCH 035/235] Fixes #26 --- README.md | 2 +- mbpfan.upstart | 15 ++------------- src/daemon.c | 5 +++++ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f55cd19..a2acf77 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This enhanced version assumes any number of processors and fans (max. 10). Supported GNU/Linux Distributions --------------------------------- We provide scripts to to load mbpfan daemon at system boot for many distros. -Please note that the support is provided by volunteers. mbpfan needs test and bug reports. +Please note that the support is provided by volunteers. mbpfan needs tests and bug reports. Supported distributions: diff --git a/mbpfan.upstart b/mbpfan.upstart index ff2957f..32fc389 100644 --- a/mbpfan.upstart +++ b/mbpfan.upstart @@ -1,7 +1,7 @@ # mbpfan - A simple daemon to control fan speed on all Macbook/Macbook Pros \ # (probably all Apple computers) for Linux 3.x.x -description "mbpfan" +description "mbpfan" start on filesystem or runlevel [2345] stop on runlevel [!2345] @@ -12,15 +12,4 @@ umask 022 console log -pre-start script - test -x /usr/sbin/mbpfan || { stop; exit 0; } - # A way to turn pidfile writing off would be nice. That or mbpfan - # could check for staleness before refusing to control your fans. - rm -f /var/run/mbpfan.pid -end script - -# Starting mbpfan in the foreground without being in an interactive tty currently -# seems to segfault, so expect forkage. -expect fork -exec /usr/sbin/mbpfan - +exec /usr/sbin/mbpfan -f diff --git a/src/daemon.c b/src/daemon.c index 56d563c..44a2b90 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -87,6 +87,11 @@ void signal_handler(int signal) delete_pid(); //TODO: free resources exit(0); + case SIGSTOP: + syslog(LOG_WARNING, "Received SIGSTOP signal."); + delete_pid(); + //TODO: free resources + exit(0); default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); break; From 45755d6228f06ceebfdf12edd2783ab909a1bb35 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 24 Oct 2012 11:13:25 +0200 Subject: [PATCH 036/235] First attempt to fix #24 --- src/global.h | 13 +++++-- src/mbpfan.c | 104 ++++++++++++++++++++++++++++---------------------- src/mbpfan.h | 9 +++-- src/minunit.c | 6 +-- 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/global.h b/src/global.h index d4bea44..194b24f 100644 --- a/src/global.h +++ b/src/global.h @@ -9,11 +9,18 @@ extern const char* program_pid; struct s_sensors { char* path; - char* fan_output_path; - char* fan_manual_path; unsigned int temperature; struct s_sensors *next; }; -typedef s_sensors t_sensors; +struct s_fans { + char* path; + char* fan_output_path; + char* fan_manual_path; + struct s_fans *next; +}; + +typedef struct s_sensors t_sensors; +typedef struct s_fans t_fans; + #endif \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index d454ccd..ea8d9c0 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -57,18 +57,8 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; -/* -struct s_sensors { - char* path; - char* fan_output_path; - char* fan_manual_path; - unsigned int temperature; - struct s_sensors *next; -}; - */ - typedef struct s_sensors t_sensors; - +typedef struct s_fans t_fans; t_sensors *retrieve_sensors() { @@ -84,12 +74,12 @@ t_sensors *retrieve_sensors() char number[2]; sprintf(number,"%d",0); - int i = 0; - - for(i = 0; i<10; i++) { + int counter = 0; + int sensors_found = 0; + for(counter = 0; counter<10; counter++) { path = (char*) malloc(sizeof( char ) * path_size); - sprintf(number,"%d",i); + sprintf(number,"%d",counter); path[0] = '\0'; strncat( path, path_begin, strlen(path_begin) ); strncat( path, number, strlen(number) ); @@ -119,22 +109,29 @@ t_sensors *retrieve_sensors() } fclose(file); + sensors_found++; } free(path); path = NULL; } - if(sensors_head != NULL) { - find_fans(sensors_head); + if(verbose) { + printf("Found %d sensors\n", sensors_found); + + if(daemonize) { + syslog(LOG_INFO, "Found %d sensors", sensors_found); + } } return sensors_head; } -void find_fans(t_sensors* sensors) +t_fans *retrieve_fans() { - t_sensors *tmp = sensors; + + t_fans *fans_head = NULL; + t_fans *fan = NULL; char *path_output = NULL; char *path_manual = NULL; @@ -142,17 +139,17 @@ void find_fans(t_sensors* sensors) const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; const char *path_output_end = "_output"; const char *path_man_end = "_manual"; - + int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; char number[2]; sprintf(number,"%d",0); int counter = 0; - int n_sensors = 0; - int n_fans = 0; + int fans_found = 0; - for(counter = 0; counter<10; counter++) { + for(int counter = 0; counter<10; counter++) { + path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output[0] = '\0'; path_manual = (char*) malloc(sizeof( char ) * path_man_size); @@ -171,38 +168,53 @@ void find_fans(t_sensors* sensors) FILE *file = fopen(path_output, "r"); if(file != NULL) { - if (tmp->path != NULL) { - tmp->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); - tmp->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); + fan = (t_fans *) malloc( sizeof( t_fans ) ); + fan->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); + fan->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); + strcpy(fan->fan_output_path, path_output); + strcpy(fan->fan_manual_path, path_manual); + + if (fans_head == NULL) { + fans_head = fan; + fans_head->next = NULL; + + } else { + t_fans *tmp = fans_head; + + while (tmp->next != NULL) { + tmp = tmp->next; + } + + tmp->next = fan; + tmp->next->next = NULL; } - strcpy(tmp->fan_output_path, path_output); - strcpy(tmp->fan_manual_path, path_manual); - tmp = tmp->next; - n_fans++; - n_sensors++; fclose(file); + fans_found++; } + + free(path_output); + path_output = NULL; + free(path_manual); + path_manual = NULL; } if(verbose) { - printf("Found %d sensors and %d fans\n", n_sensors, n_fans); - + printf("Found %d fans\n", fans_found); if(daemonize) { - syslog(LOG_INFO, "Found %d sensors and %d fans", n_sensors, n_fans); + syslog(LOG_INFO, "Found %d fans", fans_found); } } - free(path_output); - path_output = NULL; - free(path_manual); - path_manual = NULL; + + return fans_head; } -void set_fans_man(t_sensors *sensors) + +void set_fans_man(t_fans *fans) { - t_sensors *tmp = sensors; + t_fans *tmp = fans; FILE *file; while(tmp != NULL) { @@ -238,9 +250,9 @@ t_sensors *refresh_sensors(t_sensors *sensors) /* Controls the speed of the fan */ -void set_fan_speed(t_sensors* sensors, int speed) +void set_fan_speed(t_fans* fans, int speed) { - t_sensors *tmp = sensors; + t_fans *tmp = fans; FILE *file; while(tmp != NULL) { @@ -359,10 +371,12 @@ void mbpfan() retrieve_settings(); t_sensors* sensors = retrieve_sensors(); - set_fans_man(sensors); + t_fans* fans = retrieve_fans(); + + set_fans_man(fans); new_temp = get_temp(sensors); fan_speed = 2000; - set_fan_speed(sensors, fan_speed); + set_fan_speed(fans, fan_speed); if(verbose) { @@ -413,7 +427,7 @@ void mbpfan() } } - set_fan_speed(sensors, fan_speed); + set_fan_speed(fans, fan_speed); if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); diff --git a/src/mbpfan.h b/src/mbpfan.h index c456b5e..70f6297 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -40,6 +40,9 @@ extern int polling_interval; struct s_sensors; typedef struct s_sensors t_sensors; +struct s_fans; +typedef struct s_fans t_fans; + /** * Tries to use the settings located in * /etc/mbpfan.conf @@ -63,19 +66,19 @@ t_sensors *refresh_sensors(t_sensors *sensors); * Detect the fans in /sys/devices/platform/applesmc.768/ * Associate each fan to a sensor */ -void find_fans(t_sensors *sensors); +t_fans* retrieve_fans(); /** * Given a list of sensors with associated fans * Set them to manual control */ -void set_fans_man(t_sensors *sensors); +void set_fans_man(t_fans *fans); /** * Given a list of sensors with associated fans * Change their speed */ -void set_fan_speed(t_sensors* sensors, int speed); +void set_fan_speed(t_fans* fans, int speed); /** * Return average CPU temp in degrees (ceiling) diff --git a/src/minunit.c b/src/minunit.c index ef22695..622ed95 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -33,9 +33,9 @@ static const char *test_sensor_paths() static const char *test_fan_paths() { - t_sensors* sensors = retrieve_sensors(); - mu_assert("No sensors found", sensors != NULL); - t_sensors* tmp = sensors; + t_fans* fans = retrieve_fans(); + mu_assert("No fans found", fans != NULL); + t_fans* tmp = fans; int found_fan_path = 0; while(tmp != NULL) { From 3ff1aad95f63d8802fcfb58be3a6e204ef5a2a48 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 24 Oct 2012 14:18:11 +0200 Subject: [PATCH 037/235] Taking into account the number of sensors in averaging temp #24 --- src/mbpfan.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index ea8d9c0..e9478e8 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -277,12 +277,18 @@ unsigned short get_temp(t_sensors* sensors) t_sensors* tmp = sensors; + int number_sensors = 0; while(tmp != NULL) { sum_temp += tmp->temperature; tmp = tmp->next; + number_sensors++; } - temp = (unsigned short)( ceil( (float)( sum_temp ) / 2000. ) ); + // just to be safe + if (number_sensors == 0) + number_sensors++; + + temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) ); return temp; } From 8d51412bc9067dfc639019fd24f3d0b2b3423699 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 24 Oct 2012 22:37:53 +0200 Subject: [PATCH 038/235] Tested on new kernels and new Macbook models :-) --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a2acf77..d93f2fa 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,10 @@ Supported distributions: Tested Macbook Models --------------------- -- MacBook Pro 8.1 13" (Intel i7 - Linux 3.2) -- MacBook Pro 6,2 15" (Intel i7 - Linux 3.2) +- Macbook Pro 8,2 15" (Intel i7 - Linux 3.6.2) +- MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) +- Macbook Pro 6,2 15" (Intel i7 - Linux 3.5.0) +- MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) From 33a3710995cced01639532e70eb5c82dc120942a Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 12 Nov 2012 11:48:05 +0100 Subject: [PATCH 039/235] Fixes #27 --- src/daemon.c | 30 ++++++++-------- src/global.h | 4 +-- src/main.c | 93 +++++++++++++++++++++++++++++++------------------- src/mbpfan.c | 10 ++++-- src/minunit.c | 0 src/settings.c | 0 src/settings.h | 0 src/strmap.c | 0 src/strmap.h | 0 9 files changed, 82 insertions(+), 55 deletions(-) mode change 100644 => 100755 src/daemon.c mode change 100644 => 100755 src/minunit.c mode change 100644 => 100755 src/settings.c mode change 100644 => 100755 src/settings.h mode change 100644 => 100755 src/strmap.c mode change 100644 => 100755 src/strmap.h diff --git a/src/daemon.c b/src/daemon.c old mode 100644 new mode 100755 index 44a2b90..a714af3 --- a/src/daemon.c +++ b/src/daemon.c @@ -33,7 +33,7 @@ int write_pid(int pid) { FILE *file = NULL; - file = fopen(program_pid, "w"); + file = fopen(PROGRAM_PID, "w"); if(file != NULL) { fprintf(file, "%d", pid); @@ -49,7 +49,7 @@ int read_pid() { FILE *file = NULL; int pid = -1; - file = fopen(program_pid, "r"); + file = fopen(PROGRAM_PID, "r"); if(file != NULL) { fscanf(file, "%d", &pid); @@ -62,7 +62,7 @@ int read_pid() int delete_pid() { - return remove(program_pid); + return remove(PROGRAM_PID); } @@ -106,16 +106,16 @@ void go_daemon(void (*fan_control)()) signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - syslog(LOG_INFO, "%s starting up", program_name); + syslog(LOG_INFO, "%s starting up", PROGRAM_NAME); // Setup syslog logging - see SETLOGMASK(3) if(verbose) { setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog(program_name, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); } else { setlogmask(LOG_UPTO(LOG_INFO)); - openlog(program_name, LOG_CONS, LOG_USER); + openlog(PROGRAM_NAME, LOG_CONS, LOG_USER); } @@ -161,31 +161,31 @@ void go_daemon(void (*fan_control)()) if (read_pid() == -1) { if (verbose) { - printf("Writing a new .pid file with value %d at: %s\n", current_pid, program_pid); - syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, program_pid); + printf("Writing a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID); + syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); } if (write_pid(current_pid) == 0) { - syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", program_pid); + syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID); if (verbose) { - printf("ERROR: Can not create a .pid file at: %s. Aborting\n", program_pid); + printf("ERROR: Can not create a .pid file at: %s. Aborting\n", PROGRAM_PID); } exit(EXIT_FAILURE); } else { if (verbose) { - printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, program_pid); - syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, program_pid); + printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID); + syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); } } } else { - syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", program_pid); + syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID); if (verbose) { - printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", program_pid); + printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", PROGRAM_PID); } exit(EXIT_FAILURE); @@ -195,7 +195,7 @@ void go_daemon(void (*fan_control)()) fan_control(); if(daemonize) { - syslog(LOG_INFO, "%s daemon exiting", program_name); + syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); } return; diff --git a/src/global.h b/src/global.h index 194b24f..093f4c9 100644 --- a/src/global.h +++ b/src/global.h @@ -4,8 +4,8 @@ extern int daemonize; extern int verbose; -extern const char* program_name; -extern const char* program_pid; +extern const char* PROGRAM_NAME; +extern const char* PROGRAM_PID; struct s_sensors { char* path; diff --git a/src/main.c b/src/main.c index c3783eb..d275879 100644 --- a/src/main.c +++ b/src/main.c @@ -20,17 +20,23 @@ #include #include #include +#include +#include +#include +#include #include "mbpfan.h" #include "daemon.h" #include "global.h" #include "minunit.h" -#include int daemonize = 1; int verbose = 0; -const char *program_name = "mbpfan"; -const char *program_pid = "/var/run/mbpfan.pid"; +const char *PROGRAM_NAME = "mbpfan"; +const char *PROGRAM_PID = "/var/run/mbpfan.pid"; + +const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0"; +const char *APPLESMC_PATH = "/sys/devices/platform/applesmc.768"; void print_usage(int argc, char *argv[]) { @@ -46,6 +52,54 @@ void print_usage(int argc, char *argv[]) } +void check_requirements() +{ + + /** + * Check for root + */ + + uid_t uid=getuid(), euid=geteuid(); + + if (uid != 0 || euid != 0) { + syslog(LOG_INFO, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); + printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME); + exit(0); + } + + /** + * Check for coretemp and applesmc modules + * Credits: -http://stackoverflow.com/questions/12978794 + */ + FILE *fd = popen("lsmod | grep coretemp", "r"); + + char buf[16]; + + if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { + DIR* dir = opendir(CORETEMP_PATH); + + if (ENOENT == errno) { + syslog(LOG_INFO, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + exit(0); + } + } + + fd = popen("lsmod | grep applesmc", "r"); + + if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { + DIR* dir = opendir(APPLESMC_PATH); + + if (ENOENT == errno) { + syslog(LOG_INFO, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + exit(0); + } + + } + +} + int main(int argc, char *argv[]) { @@ -75,40 +129,9 @@ int main(int argc, char *argv[]) } } - /** - * Check for root - */ - uid_t uid=getuid(), euid=geteuid(); - if (uid != 0 || euid != 0) { - syslog(LOG_INFO, "Mbpfan needs root privileges. Please run mbpfan as root. Exiting."); - printf("Mbpfan not started with root privileges. Please run mbpfan as root. Exiting.\n"); - exit(0); - } - - /** - * Check for coretemp and applesmc modules - * Credits: http://stackoverflow.com/questions/12978794 - */ - - FILE *fd = popen("lsmod | grep coretemp", "r"); - - char buf[16]; - - if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { - syslog(LOG_INFO, "Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting."); - printf("Mbpfan needs coretemp module. Please load it and run mbpfan again. Exiting.\n"); - exit(0); - } - - fd = popen("lsmod | grep applesmc", "r"); - - if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { - syslog(LOG_INFO, "Mbpfan needs applesmc module. Please load it and run mbpfan again. Exiting."); - printf("Mbpfan needs applescm module. Please load it and run mbpfan again. Exiting.\n"); - exit(0); - } + check_requirements(); // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; diff --git a/src/mbpfan.c b/src/mbpfan.c index e9478e8..73a3655 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -76,6 +76,7 @@ t_sensors *retrieve_sensors() int counter = 0; int sensors_found = 0; + for(counter = 0; counter<10; counter++) { path = (char*) malloc(sizeof( char ) * path_size); @@ -139,7 +140,7 @@ t_fans *retrieve_fans() const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; const char *path_output_end = "_output"; const char *path_man_end = "_manual"; - + int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; char number[2]; @@ -149,7 +150,7 @@ t_fans *retrieve_fans() int fans_found = 0; for(int counter = 0; counter<10; counter++) { - + path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output[0] = '\0'; path_manual = (char*) malloc(sizeof( char ) * path_man_size); @@ -201,6 +202,7 @@ t_fans *retrieve_fans() if(verbose) { printf("Found %d fans\n", fans_found); + if(daemonize) { syslog(LOG_INFO, "Found %d fans", fans_found); } @@ -278,6 +280,7 @@ unsigned short get_temp(t_sensors* sensors) t_sensors* tmp = sensors; int number_sensors = 0; + while(tmp != NULL) { sum_temp += tmp->temperature; tmp = tmp->next; @@ -285,8 +288,9 @@ unsigned short get_temp(t_sensors* sensors) } // just to be safe - if (number_sensors == 0) + if (number_sensors == 0) { number_sensors++; + } temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) ); return temp; diff --git a/src/minunit.c b/src/minunit.c old mode 100644 new mode 100755 diff --git a/src/settings.c b/src/settings.c old mode 100644 new mode 100755 diff --git a/src/settings.h b/src/settings.h old mode 100644 new mode 100755 diff --git a/src/strmap.c b/src/strmap.c old mode 100644 new mode 100755 diff --git a/src/strmap.h b/src/strmap.h old mode 100644 new mode 100755 From a2dacfde05704b8c7857edacfa8a2026a0b13b71 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 18 Nov 2012 22:16:49 +0100 Subject: [PATCH 040/235] Fixes #4. Adds some tests for #28 --- mbpfan.conf.test1 | 7 +++++++ src/daemon.c | 8 ++++---- src/daemon.h | 4 ++-- src/global.h | 1 + src/main.c | 10 ++++----- src/mbpfan.c | 14 +++++++++---- src/mbpfan.h | 2 +- src/minunit.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ src/minunit.h | 5 ++++- src/settings.c | 0 src/settings.h | 0 src/strmap.c | 0 src/strmap.h | 0 13 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 mbpfan.conf.test1 mode change 100755 => 100644 src/daemon.c mode change 100755 => 100644 src/minunit.c mode change 100755 => 100644 src/settings.c mode change 100755 => 100644 src/settings.h mode change 100755 => 100644 src/strmap.c mode change 100755 => 100644 src/strmap.h diff --git a/mbpfan.conf.test1 b/mbpfan.conf.test1 new file mode 100644 index 0000000..e17bdbd --- /dev/null +++ b/mbpfan.conf.test1 @@ -0,0 +1,7 @@ +[general] +min_fan_speed = 6200 # default is 2000 +max_fan_speed = 6200 # default is 6200 +low_temp = 63 # try ranges 55-63, default is 63 +high_temp = 66 # try ranges 58-66, default is 66 +max_temp = 86 # do not set it > 90, default is 86 +polling_interval = 1 # default is 7 diff --git a/src/daemon.c b/src/daemon.c old mode 100755 new mode 100644 index a714af3..9b33b61 --- a/src/daemon.c +++ b/src/daemon.c @@ -29,6 +29,7 @@ #include #include "mbpfan.h" #include "global.h" +#include "daemon.h" int write_pid(int pid) { @@ -65,16 +66,13 @@ int delete_pid() return remove(PROGRAM_PID); } - void signal_handler(int signal) { switch(signal) { case SIGHUP: - //TODO: restart myself syslog(LOG_WARNING, "Received SIGHUP signal."); - delete_pid(); - exit(0); + retrieve_settings(NULL); break; case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); @@ -105,6 +103,7 @@ void go_daemon(void (*fan_control)()) signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); + signal(SIGSTOP, signal_handler); syslog(LOG_INFO, "%s starting up", PROGRAM_NAME); @@ -131,6 +130,7 @@ void go_daemon(void (*fan_control)()) } if (pid_slave > 0) { + signal(SIGCHLD, SIG_IGN); // kill the father exit(EXIT_SUCCESS); } diff --git a/src/daemon.h b/src/daemon.h index b20a185..67fa775 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -45,10 +45,10 @@ int delete_pid(); */ void signal_handler(int signal); - /** * Daemonizes */ -int go_daemon(void (*function)()); +void go_daemon(void (*function)()); + #endif \ No newline at end of file diff --git a/src/global.h b/src/global.h index 093f4c9..472bad3 100644 --- a/src/global.h +++ b/src/global.h @@ -23,4 +23,5 @@ struct s_fans { typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index d275879..12947b8 100644 --- a/src/main.c +++ b/src/main.c @@ -68,11 +68,10 @@ void check_requirements() } /** - * Check for coretemp and applesmc modules - * Credits: -http://stackoverflow.com/questions/12978794 - */ + * Check for coretemp and applesmc modules + * Credits: -http://stackoverflow.com/questions/12978794 + */ FILE *fd = popen("lsmod | grep coretemp", "r"); - char buf[16]; if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { @@ -83,6 +82,7 @@ void check_requirements() printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); exit(0); } + } fd = popen("lsmod | grep applesmc", "r"); @@ -92,7 +92,7 @@ void check_requirements() if (ENOENT == errno) { syslog(LOG_INFO, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); exit(0); } diff --git a/src/mbpfan.c b/src/mbpfan.c index 73a3655..86cdbe5 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -296,12 +296,19 @@ unsigned short get_temp(t_sensors* sensors) return temp; } -void retrieve_settings() +void retrieve_settings(const char* settings_path) { Settings *settings = NULL; int result = 0; FILE *f = NULL; - f = fopen("/etc/mbpfan.conf", "r"); + + if (settings_path == NULL) { + f = fopen("/etc/mbpfan.conf", "r"); + + } else { + f = fopen(settings_path, "r"); + } + if (f == NULL) { /* Could not open configfile */ @@ -378,7 +385,7 @@ void mbpfan() int temp_change; int step_up, step_down; - retrieve_settings(); + retrieve_settings(NULL); t_sensors* sensors = retrieve_sensors(); t_fans* fans = retrieve_fans(); @@ -388,7 +395,6 @@ void mbpfan() fan_speed = 2000; set_fan_speed(fans, fan_speed); - if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); diff --git a/src/mbpfan.h b/src/mbpfan.h index 70f6297..27cba96 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -48,7 +48,7 @@ typedef struct s_fans t_fans; * /etc/mbpfan.conf * If it fails, the default hardcoded settings are used */ -void retrieve_settings(); +void retrieve_settings(const char* settings_path); /** * Detect the sensors in /sys/devices/platform/coretemp.0/temp diff --git a/src/minunit.c b/src/minunit.c old mode 100755 new mode 100644 index 622ed95..01ba082 --- a/src/minunit.c +++ b/src/minunit.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "global.h" #include "mbpfan.h" #include "settings.h" @@ -124,6 +125,54 @@ static const char *test_config_file() return 0; } +static const char *test_settings() +{ + retrieve_settings("./mbpfan.conf.test1"); + mu_assert("min_fan_speed value is not 6200", min_fan_speed == 6200); + mu_assert("polling_interval is not 1", polling_interval == 1); + retrieve_settings("./mbpfan.conf"); + mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000); + mu_assert("polling_interval is not 7", polling_interval == 7); + return 0; +} + +int received = 0; + +static void handler(int signal) +{ + + switch(signal) { + case SIGHUP: + received = 1; + retrieve_settings("./mbpfan.conf.test1"); + break; + default: + received = 0; + break; + } +} + +static const char *test_sighup_receive() +{ + signal(SIGHUP, handler); + raise(SIGHUP); + mu_assert("did not receive SIGHUP signal", received == 1); + return 0; +} + +static const char *test_settings_reload() +{ + signal(SIGHUP, handler); + retrieve_settings("./mbpfan.conf"); + mu_assert("min_fan_speed value is not 2000 before SIGHUP", min_fan_speed == 2000); + mu_assert("polling_interval is not 7 before SIHUP", polling_interval == 7); + raise(SIGHUP); + mu_assert("min_fan_speed value is not 6200 after SIGHUP", min_fan_speed == 6200); + mu_assert("polling_interval is not 1 after SIHUP", polling_interval == 1); + retrieve_settings("./mbpfan.conf"); + return 0; +} + static const char *all_tests() { @@ -131,6 +180,9 @@ static const char *all_tests() mu_run_test(test_fan_paths); mu_run_test(test_get_temp); mu_run_test(test_config_file); + mu_run_test(test_settings); + mu_run_test(test_sighup_receive); + mu_run_test(test_settings_reload); return 0; } diff --git a/src/minunit.h b/src/minunit.h index 5fbff28..603f046 100644 --- a/src/minunit.h +++ b/src/minunit.h @@ -15,7 +15,10 @@ static const char *test_sensor_paths(); static const char *test_fan_paths(); static const char *test_get_temp(); static const char *test_config_file(); - +static const char *test_settings(); +static void handler(int signal); +static const char *test_sighup_receive(); +static const char *test_settings_reload(); static const char *all_tests(); int tests(); diff --git a/src/settings.c b/src/settings.c old mode 100755 new mode 100644 diff --git a/src/settings.h b/src/settings.h old mode 100755 new mode 100644 diff --git a/src/strmap.c b/src/strmap.c old mode 100755 new mode 100644 diff --git a/src/strmap.h b/src/strmap.h old mode 100755 new mode 100644 From 601c1fa09f1de2c7c3f9c77c8dbe87d6d387325e Mon Sep 17 00:00:00 2001 From: Pieter Joost van de Sande Date: Sun, 13 Oct 2013 12:05:00 +0200 Subject: [PATCH 041/235] Change upstart to respawn forever The deamon should never stop respawning. Way to important! --- mbpfan.upstart | 1 - 1 file changed, 1 deletion(-) diff --git a/mbpfan.upstart b/mbpfan.upstart index 32fc389..7299b4f 100644 --- a/mbpfan.upstart +++ b/mbpfan.upstart @@ -7,7 +7,6 @@ start on filesystem or runlevel [2345] stop on runlevel [!2345] respawn -respawn limit 10 5 umask 022 console log From 3f59ffac636282a162354423bac0a7b92f0e67d6 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 13 Oct 2013 18:18:17 +0200 Subject: [PATCH 042/235] Added compatibility with 9,2 in README [fix #30] --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d93f2fa..9da47a9 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Supported distributions: - Ubuntu - Debian +- Archlinux - Fedora - RedHat - CentOS @@ -33,6 +34,7 @@ Supported distributions: Tested Macbook Models --------------------- +- MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - Macbook Pro 8,2 15" (Intel i7 - Linux 3.6.2) - MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) - Macbook Pro 6,2 15" (Intel i7 - Linux 3.5.0) @@ -162,4 +164,4 @@ Based On * http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ * https://launchpad.net/macfanctld * http://paste2.org/p/862259 -* http://www.lobotomo.com/products/FanControl/ \ No newline at end of file +* http://www.lobotomo.com/products/FanControl/ From 423a94733cf2e19fff5d0c2e49c2897938adea8f Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 13 Oct 2013 18:27:52 +0200 Subject: [PATCH 043/235] Added Arch Linux support and BBS link [Fixes #31] --- AUTHORS | 5 +++-- PKGBUILD | 35 +++++++++++++++++++++++++++++++++++ PKGBUILD-not-maintained | 24 ------------------------ README.md | 20 +++++++++++++++----- 4 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 PKGBUILD delete mode 100644 PKGBUILD-not-maintained diff --git a/AUTHORS b/AUTHORS index 3f50d62..d1e8933 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2,12 +2,13 @@ AUTHORS, CONTRIBUTORS, MAINTAINERS ---------------------------------- Daniel Graziotin + Ismail Khatib Trevor Joynson Magnus Stubman - +Yamakaky ORIGINARY AUTHORS ----------------- Allan McRae mbpfan -rvega +rvega \ No newline at end of file diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 0000000..41489e5 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,35 @@ +# Maintainer: Yamakaky +# Contributor: Allan McRae + +pkgname=mbpfan-git +pkgver=v.1.5.0.2.ga2dacfd +pkgrel=2 +pkgdesc="Automatically adjust the fan on a MacBook Pro" +arch=('i686' 'x86_64') +url="https://github.com/dgraziotin/Fan-Control-Daemon" +license=('GPL3') +makedepends=('git') +provides=('mbpfan') +source=("$pkgname"::'git://github.com/dgraziotin/Fan-Control-Daemon.git' + 'mbpfan.install') +md5sums=('SKIP' + 'e9e2ccddee6f7177d9495781f09c8e39') +install='mbpfan.install' + +pkgver(){ + cd "$pkgname" + local ver="$(git describe --long)" + printf "%s" "${ver//-/.}" +} + +build() { + cd "$pkgname" + make +} + +package() { + cd "$pkgname" + install -Dm755 "$srcdir/$pkgname/bin/mbpfan" "$pkgdir/usr/bin/mbpfan" + install -Dm644 "$srcdir/$pkgname/mbpfan.conf" "$pkgdir/etc/mbpfan.conf" + install -Dm644 "$srcdir/$pkgname/mbpfan.service" "$pkgdir/usr/lib/systemd/system/mbpfan.service" +} diff --git a/PKGBUILD-not-maintained b/PKGBUILD-not-maintained deleted file mode 100644 index 5ee9cf6..0000000 --- a/PKGBUILD-not-maintained +++ /dev/null @@ -1,24 +0,0 @@ -# not maintained in this project -# Maintainer: Allan McRae - -pkgname=mbpfan -pkgver=1.1 -pkgrel=1 -pkgdesc="Automatically adjust the fan on a MacBook Pro" -arch=('i686' 'x86_64') -license=('GPL3') -source=(mbpfan.c - mbpfan.rc) - -md5sums=('57be3248ffa6563f06435f0b3b3451f5' - '6b58ce23ade4c2ccb8cadc004bb9e172') - -build() { - cd $srcdir/ - gcc -o mbpfan -lm -Wall $CFLAGS $LDFLAGS mbpfan.c -} - -package() { - install -Dm755 $srcdir/mbpfan $pkgdir/usr/bin/mbpfan - install -Dm755 $srcdir/mbpfan.rc $pkgdir/etc/rc.d/mbpfan -} diff --git a/README.md b/README.md index 9da47a9..3d6b4b1 100644 --- a/README.md +++ b/README.md @@ -47,15 +47,25 @@ Warning Be sure to load the kernel modules **applesmc** and **coretemp**. -Compile Instructions ---------------------- +Arch Linux +--------- +See [mbpfan-git at AUR](https://aur.archlinux.org/packages/mbpfan-git/). +Otherwise, please refer to the Generic Instructions. + + +Ubuntu +------ + +Install the ```build-essential``` package. +Then, refer to the Generic Install Instructions. + + +Generic Install Instructions +------------------------- Compile with make - -Install Instructions --------------------- Install with sudo make install From 460e0ab7c7e74e79084d2ae41fc3568b74a55d01 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 14 May 2014 09:14:26 +0200 Subject: [PATCH 044/235] Added Macbook Air 5,2 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 3d6b4b1..7c222ce 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,15 @@ Supported distributions: Tested Macbook Models --------------------- +This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. + - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - Macbook Pro 8,2 15" (Intel i7 - Linux 3.6.2) - MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) - Macbook Pro 6,2 15" (Intel i7 - Linux 3.5.0) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) +- Macbook Air 5,2 (unknown) Warning From 2636e193c8570e32b8f265ec24e824c35fae82b6 Mon Sep 17 00:00:00 2001 From: John Ferlito Date: Sat, 24 May 2014 19:51:01 +1000 Subject: [PATCH 045/235] pclose the popened processes This prevents two defunct sub processes hanging around. --- src/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.c b/src/main.c index 12947b8..067e933 100644 --- a/src/main.c +++ b/src/main.c @@ -85,6 +85,8 @@ void check_requirements() } + pclose(fd); + fd = popen("lsmod | grep applesmc", "r"); if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { @@ -98,6 +100,8 @@ void check_requirements() } + pclose(fd); + } From f3bbf97eef2ceb76a7cff98cc53fad8b62386927 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 25 May 2014 12:32:16 +0200 Subject: [PATCH 046/235] Update AUTHORS Added John Ferlito contribution --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index d1e8933..66ec6fb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,8 +7,9 @@ Ismail Khatib Trevor Joynson Magnus Stubman Yamakaky +John Ferlito ORIGINARY AUTHORS ----------------- Allan McRae mbpfan -rvega \ No newline at end of file +rvega From 6bfe9325c37fb5f1f84437af0eeb592202bd5264 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 21 Jun 2014 19:35:56 +0200 Subject: [PATCH 047/235] Support for kernel >= 3.15 --- README.md | 1 + src/daemon.c | 2 +- src/daemon.h | 2 +- src/main.c | 2 +- src/mbpfan.c | 32 ++++++++++++++++++++++++++++++-- src/mbpfan.h | 2 +- 6 files changed, 35 insertions(+), 6 deletions(-) mode change 100644 => 100755 src/daemon.c mode change 100644 => 100755 src/mbpfan.c diff --git a/README.md b/README.md index 7c222ce..c38d754 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Tested Macbook Models --------------------- This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. +- MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - Macbook Pro 8,2 15" (Intel i7 - Linux 3.6.2) - MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) diff --git a/src/daemon.c b/src/daemon.c old mode 100644 new mode 100755 index 9b33b61..215558f --- a/src/daemon.c +++ b/src/daemon.c @@ -1,6 +1,6 @@ /** * Copyright (C) 2012 Peter Lombardo - * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Daniel Graziotin * Modifications (2012) by Ismail Khatib * * This program is free software: you can redistribute it and/or modify diff --git a/src/daemon.h b/src/daemon.h index 67fa775..35b7592 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -1,5 +1,5 @@ /** - * Copyright (C) (2012) Daniel Graziotin + * Copyright (C) (2012) Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/main.c b/src/main.c index 067e933..e766954 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ /** - * Copyright (C) (2012) Daniel Graziotin + * Copyright (C) (2012-2014) Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/mbpfan.c b/src/mbpfan.c old mode 100644 new mode 100755 index 86cdbe5..0650113 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -2,7 +2,7 @@ * mbpfan.c - automatically control fan for MacBook Pro * Copyright (C) 2010 Allan McRae * Modifications by Rafael Vega - * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Daniel Graziotin * Modifications (2012) by Ismail Khatib * * This program is free software: you can redistribute it and/or modify @@ -15,7 +15,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * 2012-06-09 - v1.2 + * 2012-06-09 - v1.5.x * * Notes: * Assumes any number of processors and fans (max. 10) @@ -36,6 +36,7 @@ #include #include #include +#include #include "mbpfan.h" #include "global.h" #include "settings.h" @@ -67,7 +68,34 @@ t_sensors *retrieve_sensors() t_sensors *s = NULL; char *path = NULL; + + + #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0) + + if(verbose) { + printf("Using legacy sensor path for kernel < 3.15.0\n"); + + if(daemonize) { + syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0"); + } + } + const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; + + #else + + if(verbose) { + printf("Using new sensor path for kernel >= 3.0.15\n"); + + if(daemonize) { + syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15"); + } + } + + const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon0/temp"; + + #endif + const char *path_end = "_input"; int path_size = strlen(path_begin) + strlen(path_end) + 2; diff --git a/src/mbpfan.h b/src/mbpfan.h index 27cba96..5b6ed21 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -1,6 +1,6 @@ /** * Copyright (C) 2010 Allan McRae - * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From d14c7107e871a01a5ad565a16b39677ce04fff5c Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 22 Jun 2014 16:48:44 +0200 Subject: [PATCH 048/235] Almost done with #38. --- src/daemon.c | 4 + src/main.c | 4 + src/mbpfan.c | 84 +++++++- src/mbpfan.h | 5 + src/minunit.c | 1 + src/settings.c | 5 + src/settings.h | 572 ++++++++++++++++++++++++------------------------- src/strmap.h | 266 +++++++++++------------ 8 files changed, 520 insertions(+), 421 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 9b33b61..18b01fa 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -74,22 +74,26 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGHUP signal."); retrieve_settings(NULL); break; + case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); delete_pid(); //TODO: free resources exit(0); break; + case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); delete_pid(); //TODO: free resources exit(0); + case SIGSTOP: syslog(LOG_WARNING, "Received SIGSTOP signal."); delete_pid(); //TODO: free resources exit(0); + default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); break; diff --git a/src/main.c b/src/main.c index 067e933..2c690d8 100644 --- a/src/main.c +++ b/src/main.c @@ -116,16 +116,20 @@ int main(int argc, char *argv[]) print_usage(argc, argv); exit(0); break; + case 'f': daemonize = 0; break; + case 't': tests(); exit(0); break; + case 'v': verbose = 1; break; + default: print_usage(argc, argv); exit(0); diff --git a/src/mbpfan.c b/src/mbpfan.c index 86cdbe5..9f09aad 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -2,7 +2,7 @@ * mbpfan.c - automatically control fan for MacBook Pro * Copyright (C) 2010 Allan McRae * Modifications by Rafael Vega - * Modifications (2012) by Daniel Graziotin + * Modifications (2012) by Daniel Graziotin * Modifications (2012) by Ismail Khatib * * This program is free software: you can redistribute it and/or modify @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include "mbpfan.h" #include "global.h" #include "settings.h" @@ -60,6 +62,29 @@ int polling_interval = 7; typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; +bool is_legacy_kernel() +{ + struct utsname kernel; + uname(&kernel); + + char *str_kernel_version; + str_kernel_version = strndup(kernel.release + 2, 2); + + if(verbose) { + printf("Detected kernel version: %s\n", kernel.release); + printf("Detected kernel minor revision: %s\n", str_kernel_version); + + if(daemonize) { + syslog(LOG_INFO, "Kernel version: %s", kernel.release); + syslog(LOG_INFO, "Detected kernel minor revision: %sn", str_kernel_version); + } + } + + int kernel_version = atoi(str_kernel_version); + + return (kernel_version < 15); +} + t_sensors *retrieve_sensors() { @@ -67,7 +92,62 @@ t_sensors *retrieve_sensors() t_sensors *s = NULL; char *path = NULL; - const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; + char *path_begin = NULL; + + if (is_legacy_kernel()) { + if(verbose) { + printf("Using legacy sensor path for kernel < 3.15.0\n"); + + if(daemonize) { + syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0"); + } + } + + path_begin = "/sys/devices/platform/coretemp.0/temp"; + + } else { + + if(verbose) { + printf("Using new sensor path for kernel >= 3.0.15\n"); + + if(daemonize) { + syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15"); + } + } + + path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon"; + + int counter; + + for (counter = 0; counter < 10; counter++) { + + char hwmon_path[strlen(path_begin)+1]; + + sprintf(hwmon_path, "%s%d", path_begin, counter); + + // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen + fopen(hwmon_path, "wb"); + + if (errno == EISDIR) { + + path_begin = (char*) malloc(sizeof( char ) * (strlen(hwmon_path) + strlen("/temp") + 1)); + strcpy(path_begin, hwmon_path); + strcat(path_begin, "/temp"); + + if(verbose) { + printf("Found hwmon path at %s\n", path_begin); + + if(daemonize) { + syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin); + } + + } + + break; + } + } + } + const char *path_end = "_input"; int path_size = strlen(path_begin) + strlen(path_end) + 2; diff --git a/src/mbpfan.h b/src/mbpfan.h index 27cba96..a7aef3d 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -43,6 +43,11 @@ typedef struct s_sensors t_sensors; struct s_fans; typedef struct s_fans t_fans; +/** + * Return true if the kernel is < 3.15.0 + */ +bool is_legacy_kernel(); + /** * Tries to use the settings located in * /etc/mbpfan.conf diff --git a/src/minunit.c b/src/minunit.c index 01ba082..3ada62e 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -146,6 +146,7 @@ static void handler(int signal) received = 1; retrieve_settings("./mbpfan.conf.test1"); break; + default: received = 0; break; diff --git a/src/settings.c b/src/settings.c index ae108f2..20d18f3 100644 --- a/src/settings.c +++ b/src/settings.c @@ -773,9 +773,11 @@ static int get_converted_value(const Settings *settings, const char *section, co case CONVERT_MODE_INT: *((int *)out) = atoi(value); return 1; + case CONVERT_MODE_LONG: *((long *)out) = atol(value); return 1; + case CONVERT_MODE_DOUBLE: *((double *)out) = atof(value); return 1; @@ -821,12 +823,15 @@ static int get_converted_tuple(const Settings *settings, const char *section, co case CONVERT_MODE_INT: ((int *)out)[count] = atoi(token); break; + case CONVERT_MODE_LONG: ((long *)out)[count] = atol(token); break; + case CONVERT_MODE_DOUBLE: ((double *)out)[count] = atof(token); break; + default: return 0; } diff --git a/src/settings.h b/src/settings.h index dde66de..4b3ada8 100644 --- a/src/settings.h +++ b/src/settings.h @@ -43,292 +43,292 @@ extern "C" { #endif - typedef struct Settings Settings; - - /* - * This callback function is called once per key-value when enumerating - * all keys inside a section. - * - * Parameters: - * - * key: A pointer to a null-terminated C string. The string must not - * be modified by the client. - * - * value: A pointer to a null-terminated C string. The string must - * not be modified by the client. - * - * obj: A pointer to a client-specific object. This parameter may be - * null. - * - * Return value: None. - */ - typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); - - /* - * Creates a settings object. - * - * Return value: A pointer to a settings object, - * or null if a new settings object could not be allocated. - */ - Settings * settings_new(); - - /* - * Releases all memory held by a settings object. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * If the supplied settings object has been previously released, the - * behaviour of this function is undefined. - * - * Return value: None. - */ - void settings_delete(Settings *settings); - - /* - * Constructs a settings object by loading settings in textual form - * from the given stream. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * stream: A pointer to a stream. This parameter cannot be null. - * - * Return value: A pointer to a settings object, - * or null if an error occurred. - */ - Settings * settings_open(FILE *stream); - - /* - * Saves the current settings object in textual form to the given stream. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * stream: A pointer to a stream. This parameter cannot be null. - * - * Return value: 1 if the operation succeeded, 0 otherwise. - */ - int settings_save(const Settings *settings, FILE *stream); - - /* - * Returns the value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out_buf: A pointer to an output buffer which will contain the value, - * if it exists and fits into the buffer. - * - * n_out_buf: The size of the output buffer in bytes. - * - * Return value: If out_buf is set to null and n_out_buf is set to 0 the return - * value will be the number of bytes required to store the value (if it exists) - * and its null-terminator. For all other parameter configurations the return value - * is 1 if an associated value was found and completely copied into the output buffer, - * 0 otherwise. - */ - int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); - - /* - * Returns the integer value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The integer value associated to the provided section and - * key, or 0 if no such value exists. - */ - int settings_get_int(const Settings *settings, const char *section, const char *key); - - /* - * Returns the long integer value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The long integer value associated to the provided section and - * key, or 0 if no such value exists. - */ - long settings_get_long(const Settings *settings, const char *section, const char *key); - - /* - * Returns the double value associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The double value associated to the provided section and - * key, or 0 if no such value exists. - */ - double settings_get_double(const Settings *settings, const char *section, const char *key); - - /* - * Returns the integer tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ - int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); - - /* - * Returns the long tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ - long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); - - /* - * Returns the double tuple associated with the supplied key in the - * provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out: A pointer to an output buffer. - * - * n_out: The maximum number of elements the output buffer can hold. - * - * Return value: 1 if the entire tuple was copied into the output buffer, - * 0 otherwise. - */ - double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); - - /* - * Associates a value with the supplied key in the provided section. - * If the key is already associated with a value, the previous value - * is replaced. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. The string must have a string length > 0. The string will - * be copied. - * - * key: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * value: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * Return value: 1 if the association succeeded, 0 otherwise. - */ - int settings_set(Settings *setting, const char *section, const char *key, const char *value); - - /* - * Returns the number of associations between keys and values that exist - * in the provided section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: The number of associations between keys and values in - * the provided section. - */ - int settings_section_get_count(const Settings *settings, const char *section); - - /* - * Enumerates all associations between keys and values in the provided - * section. - * - * Parameters: - * - * settings: A pointer to a settings object. This parameter cannot be null. - * - * section: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * enum_func: A pointer to a callback function that will be - * called by this procedure once for every key associated - * with a value. This parameter cannot be null. - * - * obj: A pointer to a client-specific object. This parameter will be - * passed back to the client's callback function. This parameter can - * be null. - * - * Return value: 1 if enumeration completed, 0 otherwise. - */ - int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); +typedef struct Settings Settings; + +/* + * This callback function is called once per key-value when enumerating + * all keys inside a section. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a settings object. + * + * Return value: A pointer to a settings object, + * or null if a new settings object could not be allocated. + */ +Settings * settings_new(); + +/* + * Releases all memory held by a settings object. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * If the supplied settings object has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void settings_delete(Settings *settings); + +/* + * Constructs a settings object by loading settings in textual form + * from the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: A pointer to a settings object, + * or null if an error occurred. + */ +Settings * settings_open(FILE *stream); + +/* + * Saves the current settings object in textual form to the given stream. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * stream: A pointer to a stream. This parameter cannot be null. + * + * Return value: 1 if the operation succeeded, 0 otherwise. + */ +int settings_save(const Settings *settings, FILE *stream); + +/* + * Returns the value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int settings_get(const Settings *settings, const char *section, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Returns the integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The integer value associated to the provided section and + * key, or 0 if no such value exists. + */ +int settings_get_int(const Settings *settings, const char *section, const char *key); + +/* + * Returns the long integer value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The long integer value associated to the provided section and + * key, or 0 if no such value exists. + */ +long settings_get_long(const Settings *settings, const char *section, const char *key); + +/* + * Returns the double value associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The double value associated to the provided section and + * key, or 0 if no such value exists. + */ +double settings_get_double(const Settings *settings, const char *section, const char *key); + +/* + * Returns the integer tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +int settings_get_int_tuple(const Settings *settings, const char *section, const char *key, int *out, unsigned int n_out); + +/* + * Returns the long tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +long settings_get_long_tuple(const Settings *settings, const char *section, const char *key, long *out, unsigned int n_out); + +/* + * Returns the double tuple associated with the supplied key in the + * provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out: A pointer to an output buffer. + * + * n_out: The maximum number of elements the output buffer can hold. + * + * Return value: 1 if the entire tuple was copied into the output buffer, + * 0 otherwise. + */ +double settings_get_double_tuple(const Settings *settings, const char *section, const char *key, double *out, unsigned int n_out); + +/* + * Associates a value with the supplied key in the provided section. + * If the key is already associated with a value, the previous value + * is replaced. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. The string must have a string length > 0. The string will + * be copied. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int settings_set(Settings *setting, const char *section, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values that exist + * in the provided section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: The number of associations between keys and values in + * the provided section. + */ +int settings_section_get_count(const Settings *settings, const char *section); + +/* + * Enumerates all associations between keys and values in the provided + * section. + * + * Parameters: + * + * settings: A pointer to a settings object. This parameter cannot be null. + * + * section: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int settings_section_enum(const Settings *settings, const char *section, settings_section_enum_func enum_func, const void *obj); #ifdef __cplusplus } diff --git a/src/strmap.h b/src/strmap.h index 367f43c..416a1ce 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -44,139 +44,139 @@ extern "C" #include #include - typedef struct StrMap StrMap; - - /* - * This callback function is called once per key-value when enumerating - * all keys associated to values. - * - * Parameters: - * - * key: A pointer to a null-terminated C string. The string must not - * be modified by the client. - * - * value: A pointer to a null-terminated C string. The string must - * not be modified by the client. - * - * obj: A pointer to a client-specific object. This parameter may be - * null. - * - * Return value: None. - */ - typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); - - /* - * Creates a string map. - * - * Parameters: - * - * capacity: The number of top-level slots this string map - * should allocate. This parameter must be > 0. - * - * Return value: A pointer to a string map object, - * or null if a new string map could not be allocated. - */ - StrMap * sm_new(unsigned int capacity); - - /* - * Releases all memory held by a string map object. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * If the supplied string map has been previously released, the - * behaviour of this function is undefined. - * - * Return value: None. - */ - void sm_delete(StrMap *map); - - /* - * Returns the value associated with the supplied key. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * out_buf: A pointer to an output buffer which will contain the value, - * if it exists and fits into the buffer. - * - * n_out_buf: The size of the output buffer in bytes. - * - * Return value: If out_buf is set to null and n_out_buf is set to 0 the return - * value will be the number of bytes required to store the value (if it exists) - * and its null-terminator. For all other parameter configurations the return value - * is 1 if an associated value was found and completely copied into the output buffer, - * 0 otherwise. - */ - int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); - - /* - * Queries the existence of a key. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter cannot - * be null. - * - * Return value: 1 if the key exists, 0 otherwise. - */ - int sm_exists(const StrMap *map, const char *key); - - /* - * Associates a value with the supplied key. If the key is already - * associated with a value, the previous value is replaced. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * key: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * value: A pointer to a null-terminated C string. This parameter - * cannot be null. The string must have a string length > 0. The - * string will be copied. - * - * Return value: 1 if the association succeeded, 0 otherwise. - */ - int sm_put(StrMap *map, const char *key, const char *value); - - /* - * Returns the number of associations between keys and values. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * Return value: The number of associations between keys and values. - */ - int sm_get_count(const StrMap *map); - - /* - * Enumerates all associations between keys and values. - * - * Parameters: - * - * map: A pointer to a string map. This parameter cannot be null. - * - * enum_func: A pointer to a callback function that will be - * called by this procedure once for every key associated - * with a value. This parameter cannot be null. - * - * obj: A pointer to a client-specific object. This parameter will be - * passed back to the client's callback function. This parameter can - * be null. - * - * Return value: 1 if enumeration completed, 0 otherwise. - */ - int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); +typedef struct StrMap StrMap; + +/* + * This callback function is called once per key-value when enumerating + * all keys associated to values. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a string map. + * + * Parameters: + * + * capacity: The number of top-level slots this string map + * should allocate. This parameter must be > 0. + * + * Return value: A pointer to a string map object, + * or null if a new string map could not be allocated. + */ +StrMap * sm_new(unsigned int capacity); + +/* + * Releases all memory held by a string map object. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * If the supplied string map has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void sm_delete(StrMap *map); + +/* + * Returns the value associated with the supplied key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int sm_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Queries the existence of a key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: 1 if the key exists, 0 otherwise. + */ +int sm_exists(const StrMap *map, const char *key); + +/* + * Associates a value with the supplied key. If the key is already + * associated with a value, the previous value is replaced. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int sm_put(StrMap *map, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * Return value: The number of associations between keys and values. + */ +int sm_get_count(const StrMap *map); + +/* + * Enumerates all associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); #ifdef __cplusplus } From 0526fc5ff93492148c9b15c58fcce168a8c97b78 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 22 Jun 2014 17:07:24 +0200 Subject: [PATCH 049/235] Fixes #38 --- src/mbpfan.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 9f09aad..c75c5db 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -85,6 +85,7 @@ bool is_legacy_kernel() return (kernel_version < 15); } + t_sensors *retrieve_sensors() { @@ -103,7 +104,7 @@ t_sensors *retrieve_sensors() } } - path_begin = "/sys/devices/platform/coretemp.0/temp"; + path_begin = (char *) "/sys/devices/platform/coretemp.0/temp"; } else { @@ -115,10 +116,9 @@ t_sensors *retrieve_sensors() } } - path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon"; + path_begin = (char *) "/sys/devices/platform/coretemp.0/hwmon/hwmon"; int counter; - for (counter = 0; counter < 10; counter++) { char hwmon_path[strlen(path_begin)+1]; @@ -154,9 +154,9 @@ t_sensors *retrieve_sensors() char number[2]; sprintf(number,"%d",0); - int counter = 0; int sensors_found = 0; + int counter = 0; for(counter = 0; counter<10; counter++) { path = (char*) malloc(sizeof( char ) * path_size); @@ -208,6 +208,7 @@ t_sensors *retrieve_sensors() return sensors_head; } + t_fans *retrieve_fans() { @@ -376,6 +377,7 @@ unsigned short get_temp(t_sensors* sensors) return temp; } + void retrieve_settings(const char* settings_path) { Settings *settings = NULL; From 033c8b9de50c3b750174e909b357bda3a4e45247 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 22 Jun 2014 17:14:52 +0200 Subject: [PATCH 050/235] really fixed conflicts... --- src/mbpfan.c | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index b71a61c..69e8826 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -36,12 +36,8 @@ #include #include #include -<<<<<<< HEAD #include #include -======= -#include ->>>>>>> 6bfe9325c37fb5f1f84437af0eeb592202bd5264 #include "mbpfan.h" #include "global.h" #include "settings.h" @@ -97,7 +93,6 @@ t_sensors *retrieve_sensors() t_sensors *s = NULL; char *path = NULL; -<<<<<<< HEAD char *path_begin = NULL; if (is_legacy_kernel()) { @@ -153,36 +148,6 @@ t_sensors *retrieve_sensors() } } -======= - - - #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0) - - if(verbose) { - printf("Using legacy sensor path for kernel < 3.15.0\n"); - - if(daemonize) { - syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0"); - } - } - - const char *path_begin = "/sys/devices/platform/coretemp.0/temp"; - - #else - - if(verbose) { - printf("Using new sensor path for kernel >= 3.0.15\n"); - - if(daemonize) { - syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15"); - } - } - - const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon0/temp"; - - #endif - ->>>>>>> 6bfe9325c37fb5f1f84437af0eeb592202bd5264 const char *path_end = "_input"; int path_size = strlen(path_begin) + strlen(path_end) + 2; From 8b638485314dac3bb40cede8c61b2f65e1775eed Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 22 Jun 2014 18:35:14 +0200 Subject: [PATCH 051/235] Fixes #33 --- mbpfan.rc.archlinux-notmaintained | 70 ------------------------------- src/mbpfan.c | 2 +- 2 files changed, 1 insertion(+), 71 deletions(-) delete mode 100644 mbpfan.rc.archlinux-notmaintained diff --git a/mbpfan.rc.archlinux-notmaintained b/mbpfan.rc.archlinux-notmaintained deleted file mode 100644 index 799e3d4..0000000 --- a/mbpfan.rc.archlinux-notmaintained +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/bash -# Archlinux rc script -# not maintained in this project -# please, fix it and contribute back to the project - -daemon_name=mbpfan - -. /etc/rc.conf -. /etc/rc.d/functions - -get_pid() { - pidof -o %PPID $daemon_name -} - -case "$1" in - start) - stat_busy "Starting $daemon_name daemon" - - PID=$(get_pid) - if [ -z "$PID" ]; then - [ -f /var/run/$daemon_name.pid ] && rm -f /var/run/$daemon_name.pid - # RUN - $daemon_name & - # - if [ $? -gt 0 ]; then - stat_fail - exit 1 - else - echo $(get_pid) > /var/run/$daemon_name.pid - add_daemon $daemon_name - stat_done - fi - else - stat_fail - exit 1 - fi - ;; - - stop) - stat_busy "Stopping $daemon_name daemon" - PID=$(get_pid) - # KILL - [ ! -z "$PID" ] && kill $PID &> /dev/null - # - if [ $? -gt 0 ]; then - stat_fail - exit 1 - else - rm -f /var/run/$daemon_name.pid &> /dev/null - rm_daemon $daemon_name - stat_done - fi - ;; - - restart) - $0 stop - sleep 3 - $0 start - ;; - - status) - stat_busy "Checking $daemon_name status"; - ck_status $daemon_name - ;; - - *) - echo "usage: $0 {start|stop|restart|status}" -esac - -exit 0 diff --git a/src/mbpfan.c b/src/mbpfan.c index 69e8826..b18a78d 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -15,7 +15,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * 2012-06-09 - v1.5.x + * 2014-06-22 - v1.7.0 * * Notes: * Assumes any number of processors and fans (max. 10) From a034b3f03bcbdd8074eedb9cbc1e01fd444f655e Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 23 Jun 2014 10:24:03 +0200 Subject: [PATCH 052/235] Fixes #28 --- src/mbpfan.c | 15 +++++++++++---- src/minunit.c | 23 +++++++++++++++++++++++ src/minunit.h | 2 ++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index b18a78d..f396d37 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -68,7 +68,16 @@ bool is_legacy_kernel() uname(&kernel); char *str_kernel_version; - str_kernel_version = strndup(kernel.release + 2, 2); + str_kernel_version = strtok(kernel.release, "."); + + if (atoi(str_kernel_version) != 3){ + syslog(LOG_INFO, "mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); + printf("mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); + exit(0); + } + + str_kernel_version = strtok(NULL, "."); + int kernel_version = atoi(str_kernel_version); if(verbose) { printf("Detected kernel version: %s\n", kernel.release); @@ -76,12 +85,10 @@ bool is_legacy_kernel() if(daemonize) { syslog(LOG_INFO, "Kernel version: %s", kernel.release); - syslog(LOG_INFO, "Detected kernel minor revision: %sn", str_kernel_version); + syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); } } - int kernel_version = atoi(str_kernel_version); - return (kernel_version < 15); } diff --git a/src/minunit.c b/src/minunit.c index 3ada62e..d618529 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "global.h" #include "mbpfan.h" #include "settings.h" @@ -11,6 +12,27 @@ int tests_run = 0; +static const char *test_is_legacy_kernel() +{ + + struct utsname kernel; + uname(&kernel); + + mu_assert("Kernel major version is not 3.", kernel.release[0] == '3'); + + char *pch; + pch = strtok(kernel.release, "."); + pch = strtok(NULL, "."); + + int minor = atoi(pch); + + if (minor < 15) + mu_assert("Legacy kernel not detected by mbpfan.", is_legacy_kernel() == 1); + else + mu_assert("Non-legacy kernel not detected by mbpfan", is_legacy_kernel() == 0); + + return 0; +} static const char *test_sensor_paths() { @@ -177,6 +199,7 @@ static const char *test_settings_reload() static const char *all_tests() { + mu_run_test(test_is_legacy_kernel); mu_run_test(test_sensor_paths); mu_run_test(test_fan_paths); mu_run_test(test_get_temp); diff --git a/src/minunit.h b/src/minunit.h index 603f046..e60bd4a 100644 --- a/src/minunit.h +++ b/src/minunit.h @@ -11,6 +11,8 @@ extern int tests_run; + +static const char *test_is_legacy_kernel(); static const char *test_sensor_paths(); static const char *test_fan_paths(); static const char *test_get_temp(); From a8c38dfc223551d9d20ab624b0ca9d66928b2a8d Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 23 Jun 2014 11:00:18 +0200 Subject: [PATCH 053/235] Added verified support for mbp 7,1 --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c38d754..433dff5 100644 --- a/README.md +++ b/README.md @@ -38,12 +38,13 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) -- Macbook Pro 8,2 15" (Intel i7 - Linux 3.6.2) +- MacBook Pro 8,2 15" (Intel i7 - Linux 3.6.2) - MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) -- Macbook Pro 6,2 15" (Intel i7 - Linux 3.5.0) +- MacBook Pro 7,1 15" (Intel Core 2 Duo - Linux 3.13) +- MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) -- Macbook Air 5,2 (unknown) +- MacBook Air 5,2 (unknown) Warning From 29faca53b20cf14fc3f1ae528994ac52ebab295a Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 29 Jun 2014 12:34:59 +0200 Subject: [PATCH 054/235] Deleted outdated PKGBUILD fixes #39 --- PKGBUILD | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 PKGBUILD diff --git a/PKGBUILD b/PKGBUILD deleted file mode 100644 index 41489e5..0000000 --- a/PKGBUILD +++ /dev/null @@ -1,35 +0,0 @@ -# Maintainer: Yamakaky -# Contributor: Allan McRae - -pkgname=mbpfan-git -pkgver=v.1.5.0.2.ga2dacfd -pkgrel=2 -pkgdesc="Automatically adjust the fan on a MacBook Pro" -arch=('i686' 'x86_64') -url="https://github.com/dgraziotin/Fan-Control-Daemon" -license=('GPL3') -makedepends=('git') -provides=('mbpfan') -source=("$pkgname"::'git://github.com/dgraziotin/Fan-Control-Daemon.git' - 'mbpfan.install') -md5sums=('SKIP' - 'e9e2ccddee6f7177d9495781f09c8e39') -install='mbpfan.install' - -pkgver(){ - cd "$pkgname" - local ver="$(git describe --long)" - printf "%s" "${ver//-/.}" -} - -build() { - cd "$pkgname" - make -} - -package() { - cd "$pkgname" - install -Dm755 "$srcdir/$pkgname/bin/mbpfan" "$pkgdir/usr/bin/mbpfan" - install -Dm644 "$srcdir/$pkgname/mbpfan.conf" "$pkgdir/etc/mbpfan.conf" - install -Dm644 "$srcdir/$pkgname/mbpfan.service" "$pkgdir/usr/lib/systemd/system/mbpfan.service" -} From 473a12fd2993e96ad5a1dcce371795d299e18ebb Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 29 Jun 2014 12:42:52 +0200 Subject: [PATCH 055/235] correct use of EXIT_SUCCESS and EXIT_FAILURE, fixes #43 --- src/daemon.c | 6 +++--- src/main.c | 14 +++++++------- src/mbpfan.c | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 6f5073f..8f110a9 100755 --- a/src/daemon.c +++ b/src/daemon.c @@ -79,20 +79,20 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGTERM signal."); delete_pid(); //TODO: free resources - exit(0); + exit(EXIT_SUCCESS); break; case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); delete_pid(); //TODO: free resources - exit(0); + exit(EXIT_SUCCESS); case SIGSTOP: syslog(LOG_WARNING, "Received SIGSTOP signal."); delete_pid(); //TODO: free resources - exit(0); + exit(EXIT_SUCCESS); default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); diff --git a/src/main.c b/src/main.c index d3d57a3..f855bae 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,7 @@ void check_requirements() if (uid != 0 || euid != 0) { syslog(LOG_INFO, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME); - exit(0); + exit(EXIT_FAILURE); } /** @@ -80,7 +80,7 @@ void check_requirements() if (ENOENT == errno) { syslog(LOG_INFO, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); - exit(0); + exit(EXIT_FAILURE); } } @@ -95,7 +95,7 @@ void check_requirements() if (ENOENT == errno) { syslog(LOG_INFO, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); - exit(0); + exit(EXIT_FAILURE); } } @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) switch(c) { case 'h': print_usage(argc, argv); - exit(0); + exit(EXIT_SUCCESS); break; case 'f': @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) case 't': tests(); - exit(0); + exit(EXIT_SUCCESS); break; case 'v': @@ -132,7 +132,7 @@ int main(int argc, char *argv[]) default: print_usage(argc, argv); - exit(0); + exit(EXIT_SUCCESS); break; } } @@ -144,5 +144,5 @@ int main(int argc, char *argv[]) // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; go_daemon(fan_control); - exit(0); + exit(EXIT_SUCCESS); } \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index f396d37..3e7cb6e 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -73,7 +73,7 @@ bool is_legacy_kernel() if (atoi(str_kernel_version) != 3){ syslog(LOG_INFO, "mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); printf("mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); - exit(0); + exit(EXIT_FAILURE); } str_kernel_version = strtok(NULL, "."); From b037ec210f18253d49ebbd565e9738bd273f7863 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 29 Jun 2014 12:52:55 +0200 Subject: [PATCH 056/235] Handle corner-case where no sensors and fans are detected [fixes #42] --- src/mbpfan.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index 3e7cb6e..1ea439a 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -212,6 +212,12 @@ t_sensors *retrieve_sensors() } } + if (!sensors_found > 0){ + syslog(LOG_INFO, "mbpfan could not detect any temp sensor. Please contact the developer.\n"); + printf("mbpfan could not detect any temp sensor. Please contact the developer.\n"); + exit(EXIT_FAILURE); + } + return sensors_head; } @@ -296,6 +302,12 @@ t_fans *retrieve_fans() } } + if (!fans_found > 0){ + syslog(LOG_INFO, "mbpfan could not detect any fan. Please contact the developer.\n"); + printf("mbpfan could not detect any fan. Please contact the developer.\n"); + exit(EXIT_FAILURE); + } + return fans_head; } From 42be2d5c76e99c3b9b0bbb743cf4a04027cb33c0 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 29 Jun 2014 12:58:53 +0200 Subject: [PATCH 057/235] Correct syslog use for priority codes [fixes #44] --- src/main.c | 6 +++--- src/mbpfan.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index f855bae..e8af708 100644 --- a/src/main.c +++ b/src/main.c @@ -62,7 +62,7 @@ void check_requirements() uid_t uid=getuid(), euid=geteuid(); if (uid != 0 || euid != 0) { - syslog(LOG_INFO, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); + syslog(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME); exit(EXIT_FAILURE); } @@ -78,7 +78,7 @@ void check_requirements() DIR* dir = opendir(CORETEMP_PATH); if (ENOENT == errno) { - syslog(LOG_INFO, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); exit(EXIT_FAILURE); } @@ -93,7 +93,7 @@ void check_requirements() DIR* dir = opendir(APPLESMC_PATH); if (ENOENT == errno) { - syslog(LOG_INFO, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); exit(EXIT_FAILURE); } diff --git a/src/mbpfan.c b/src/mbpfan.c index 1ea439a..f2706db 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -213,7 +213,7 @@ t_sensors *retrieve_sensors() } if (!sensors_found > 0){ - syslog(LOG_INFO, "mbpfan could not detect any temp sensor. Please contact the developer.\n"); + syslog(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.\n"); printf("mbpfan could not detect any temp sensor. Please contact the developer.\n"); exit(EXIT_FAILURE); } @@ -303,7 +303,7 @@ t_fans *retrieve_fans() } if (!fans_found > 0){ - syslog(LOG_INFO, "mbpfan could not detect any fan. Please contact the developer.\n"); + syslog(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.\n"); printf("mbpfan could not detect any fan. Please contact the developer.\n"); exit(EXIT_FAILURE); } @@ -431,7 +431,7 @@ void retrieve_settings(const char* settings_path) printf("Couldn't read configfile\n"); if(daemonize) { - syslog(LOG_INFO, "Couldn't read configfile"); + syslog(LOG_WARNING, "Couldn't read configfile"); } } From 38c14b7e22e454e08ac44c592f823686c4b9ee8b Mon Sep 17 00:00:00 2001 From: Yamakaky Date: Sun, 29 Jun 2014 17:26:30 +0200 Subject: [PATCH 058/235] Update mbpfan.service --- mbpfan.service | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mbpfan.service b/mbpfan.service index d440e0d..a649f08 100644 --- a/mbpfan.service +++ b/mbpfan.service @@ -1,12 +1,14 @@ [Unit] -Description=Start the mbpfan daemon +Description=A fan manager daemon for MacBook Pro After=syslog.target -After=remote-fs.target +After=sysinit.target [Service] -ExecStart=/usr/sbin/mbpfan -Type=forking +Type=simple +ExecStart=/usr/bin/mbpfan -fv +ExecReload=/usr/bin/kill -HUP $MAINPID +PIDFile=/run/mbpfan.pid Restart=always [Install] -WantedBy=multi-user.target +WantedBy=sysinit.target From aadc05d6fe5af4bd7c2d5cea5e2b85a4eeb5045d Mon Sep 17 00:00:00 2001 From: Yamakaky Date: Sun, 29 Jun 2014 17:28:09 +0200 Subject: [PATCH 059/235] Unneeded link for systemd. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 433dff5..1527a0d 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,6 @@ As a special bonus, a service file for systemd is also included. To use it, execute the following (as root): sudo cp mbpfan.service /usr/lib/systemd/system - sudo ln -s /usr/lib/systemd/system/mbpfan.service /etc/systemd/system/mbpfan.service sudo systemctl daemon-reload sudo systemctl start mbpfan.service From 9c5be5456a9bfe63c13374a57f27e75fa18feffe Mon Sep 17 00:00:00 2001 From: Yamakaky Date: Sun, 29 Jun 2014 17:32:46 +0200 Subject: [PATCH 060/235] More general systemd service file. --- mbpfan.service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mbpfan.service b/mbpfan.service index a649f08..f2983ea 100644 --- a/mbpfan.service +++ b/mbpfan.service @@ -5,9 +5,9 @@ After=sysinit.target [Service] Type=simple -ExecStart=/usr/bin/mbpfan -fv +ExecStart=/usr/sbin/mbpfan -fv ExecReload=/usr/bin/kill -HUP $MAINPID -PIDFile=/run/mbpfan.pid +PIDFile=/var/run/mbpfan.pid Restart=always [Install] From bb264080a58366a007177d74d96442595c1bc319 Mon Sep 17 00:00:00 2001 From: "Yi Yang @ Minthe" Date: Sun, 6 Jul 2014 23:59:06 -0700 Subject: [PATCH 061/235] Fix makefile: no redundant sudo; Fix mbpfan.c: no more sudden silence when starting. --- Makefile | 4 ++-- src/mbpfan.c | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 125897b..6bee6a2 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ clean: tests: make install - sudo /usr/sbin/mbpfan -f -v -t + /usr/sbin/mbpfan -f -v -t uninstall: rm /usr/sbin/mbpfan @@ -78,4 +78,4 @@ install: @echo " sudo make tests" @echo "" rebuild: clean all -#rebuild is not entirely correct \ No newline at end of file +#rebuild is not entirely correct diff --git a/src/mbpfan.c b/src/mbpfan.c index f2706db..705aa2d 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -504,8 +504,6 @@ void mbpfan() } } - sleep(polling_interval); - step_up = (float)( max_fan_speed - min_fan_speed ) / (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); From 451bbb9bc2d64f5e9168176040d0eee82f6cb920 Mon Sep 17 00:00:00 2001 From: "Yi Yang @ Minthe" Date: Mon, 7 Jul 2014 13:24:15 -0700 Subject: [PATCH 062/235] Fix documentation for Gentoo --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1527a0d..e481053 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ To add the script to the default runlevels, run the following as root: An init file is available for gentoo users: mbpfan.init.gentoo To install, run as root: + chmod +x mbpfan.init.gentoo cp mbpfan.init.gentoo /etc/init.d/mbpfan To automatically run mbpfan at boot, run as root: From fff4b50bb272e6825829290ca9814bd82453c46e Mon Sep 17 00:00:00 2001 From: "otilmans@gmail.com" Date: Mon, 21 Jul 2014 23:42:37 +0200 Subject: [PATCH 063/235] Remove redundant typedef's --- src/mbpfan.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index f2706db..2090dda 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -59,8 +59,6 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; -typedef struct s_sensors t_sensors; -typedef struct s_fans t_fans; bool is_legacy_kernel() { From f5f61ccf69dc6ce9d40347bd677f0ec544c124b1 Mon Sep 17 00:00:00 2001 From: "otilmans@gmail.com" Date: Mon, 21 Jul 2014 23:43:02 +0200 Subject: [PATCH 064/235] Fix unused variable --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 2090dda..d31c3ad 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -241,7 +241,7 @@ t_fans *retrieve_fans() int counter = 0; int fans_found = 0; - for(int counter = 0; counter<10; counter++) { + for(counter = 0; counter<10; counter++) { path_output = (char*) malloc(sizeof( char ) * path_min_size); path_output[0] = '\0'; From 44d2ad7b821fb781aa24403bed90ad3acc13510b Mon Sep 17 00:00:00 2001 From: "otilmans@gmail.com" Date: Mon, 21 Jul 2014 23:43:39 +0200 Subject: [PATCH 065/235] Expose fans and sensors globally --- src/global.h | 2 ++ src/mbpfan.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/global.h b/src/global.h index 472bad3..9b64acf 100644 --- a/src/global.h +++ b/src/global.h @@ -23,5 +23,7 @@ struct s_fans { typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; +extern t_sensors* sensors; +extern t_fans* fans; #endif \ No newline at end of file diff --git a/src/mbpfan.c b/src/mbpfan.c index d31c3ad..40524a2 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -59,6 +59,9 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 7; +t_sensors* sensors = NULL; +t_fans* fans = NULL; + bool is_legacy_kernel() { @@ -486,8 +489,8 @@ void mbpfan() retrieve_settings(NULL); - t_sensors* sensors = retrieve_sensors(); - t_fans* fans = retrieve_fans(); + sensors = retrieve_sensors(); + fans = retrieve_fans(); set_fans_man(fans); new_temp = get_temp(sensors); From 8bb61f8633cdab31099fdd1f481597621a517822 Mon Sep 17 00:00:00 2001 From: "otilmans@gmail.com" Date: Mon, 21 Jul 2014 23:44:13 +0200 Subject: [PATCH 066/235] Add function to give back fan control to applesmc --- src/mbpfan.c | 16 ++++++++++++++-- src/mbpfan.h | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 40524a2..5ddffb3 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -314,7 +314,7 @@ t_fans *retrieve_fans() } -void set_fans_man(t_fans *fans) +static void set_fans_mode(t_fans *fans, int mode) { t_fans *tmp = fans; @@ -324,7 +324,7 @@ void set_fans_man(t_fans *fans) file = fopen(tmp->fan_manual_path, "rw+"); if(file != NULL) { - fprintf(file, "%d", 1); + fprintf(file, "%d", mode); fclose(file); } @@ -332,6 +332,18 @@ void set_fans_man(t_fans *fans) } } +void set_fans_man(t_fans *fans) +{ + + set_fans_mode(fans, 1); +} + +void set_fans_auto(t_fans *fans) +{ + + set_fans_mode(fans, 0); +} + t_sensors *refresh_sensors(t_sensors *sensors) { diff --git a/src/mbpfan.h b/src/mbpfan.h index c9ed069..d6250fa 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -79,6 +79,12 @@ t_fans* retrieve_fans(); */ void set_fans_man(t_fans *fans); +/** + * Given a list of sensors with associated fans + * Set them to automatic control + */ +void set_fans_auto(t_fans *fans); + /** * Given a list of sensors with associated fans * Change their speed From 511d348adb9e1cb8ea6f90c7bb9dc1941b8ff2c0 Mon Sep 17 00:00:00 2001 From: "otilmans@gmail.com" Date: Mon, 21 Jul 2014 23:45:08 +0200 Subject: [PATCH 067/235] Handle back control to applesmc on exit --- src/daemon.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/daemon.c b/src/daemon.c index 8f110a9..07e3271 100755 --- a/src/daemon.c +++ b/src/daemon.c @@ -79,6 +79,7 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGTERM signal."); delete_pid(); //TODO: free resources + set_fans_auto(fans); exit(EXIT_SUCCESS); break; @@ -86,12 +87,14 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGINT signal."); delete_pid(); //TODO: free resources + set_fans_auto(fans); exit(EXIT_SUCCESS); case SIGSTOP: syslog(LOG_WARNING, "Received SIGSTOP signal."); delete_pid(); //TODO: free resources + set_fans_auto(fans); exit(EXIT_SUCCESS); default: From 5e55ebfb99ab08db26dcdcff05b98305ff6fe1cd Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 27 Jul 2014 17:44:00 +0000 Subject: [PATCH 068/235] Initial polling of 2 seconds to gather sufficient delta_temp and set the fans high in case --- src/mbpfan.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index fe45e52..42f0178 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -503,19 +503,21 @@ void mbpfan() sensors = retrieve_sensors(); fans = retrieve_fans(); - set_fans_man(fans); + new_temp = get_temp(sensors); - fan_speed = 2000; + + fan_speed = min_fan_speed; set_fan_speed(fans, fan_speed); if(verbose) { - printf("Sleeping for %d seconds\n", polling_interval); + printf("Sleeping for 2 seconds to get first temp delta\n"); if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); + syslog(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); } } + sleep(2); step_up = (float)( max_fan_speed - min_fan_speed ) / (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); From 93f2fd441046a40d0ba1b4778763e6fd2b8c7b00 Mon Sep 17 00:00:00 2001 From: Olivier Tilmans Date: Sun, 27 Jul 2014 20:00:47 +0200 Subject: [PATCH 069/235] cleaning up resources on exit --- src/daemon.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 07e3271..f7528a0 100755 --- a/src/daemon.c +++ b/src/daemon.c @@ -66,6 +66,31 @@ int delete_pid() return remove(PROGRAM_PID); } +static void cleanup_and_exit(int exit_code) +{ + delete_pid(); + set_fans_auto(fans); + + s_fans *next_fan; + while (fans != NULL) { + next_fan = fans->next; + free(fans->fan_output_path); + free(fans->fan_manual_path); + free(fans); + fans = next_fan; + } + + s_sensors *next_sensor; + while (sensors != NULL) { + next_sensor = sensors->next; + free(sensors->path); + free(sensors); + sensors = next_sensor; + } + + exit(exit_code); +} + void signal_handler(int signal) { @@ -77,25 +102,15 @@ void signal_handler(int signal) case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); - delete_pid(); - //TODO: free resources - set_fans_auto(fans); - exit(EXIT_SUCCESS); - break; + cleanup_and_exit(EXIT_SUCCESS); case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); - delete_pid(); - //TODO: free resources - set_fans_auto(fans); - exit(EXIT_SUCCESS); + cleanup_and_exit(EXIT_SUCCESS); case SIGSTOP: syslog(LOG_WARNING, "Received SIGSTOP signal."); - delete_pid(); - //TODO: free resources - set_fans_auto(fans); - exit(EXIT_SUCCESS); + cleanup_and_exit(EXIT_SUCCESS); default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); From 63c95fd17de0aa6165153a443747598a5f9a08af Mon Sep 17 00:00:00 2001 From: Marji Cermak Date: Wed, 14 Jan 2015 16:38:30 +1100 Subject: [PATCH 070/235] refs #52: try to remove stale pid file instead of aborting. --- src/daemon.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/daemon.c b/src/daemon.c index 07e3271..d8ad669 100755 --- a/src/daemon.c +++ b/src/daemon.c @@ -55,6 +55,12 @@ int read_pid() if(file != NULL) { fscanf(file, "%d", &pid); fclose(file); + if (kill(pid, 0) == -1 && errno == ESRCH) + { /* a process with such a pid does not exist, remove the pid file */ + if (remove(PROGRAM_PID) == 0) { + return -1; + } + } return pid; } From 23c19700845cb5cbfae5daa3ced2e21a9e2d8a47 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Fri, 30 Jan 2015 10:52:30 +0100 Subject: [PATCH 071/235] Added latest contributors Thank you so much everybody! --- AUTHORS | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 66ec6fb..5196437 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,13 +1,17 @@ AUTHORS, CONTRIBUTORS, MAINTAINERS ---------------------------------- -Daniel Graziotin +Daniel Graziotin +Marji Cermak +John Ferlito Ismail Khatib Trevor Joynson Magnus Stubman +Olivier Tilmans Yamakaky -John Ferlito +Yi Yang + ORIGINARY AUTHORS ----------------- From 1be365ca99df583f8181255db9c07adae32934f8 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 8 Feb 2015 09:42:25 +0100 Subject: [PATCH 072/235] README: applesmc/coretemp load on startup #55 --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e481053..4c53151 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,13 @@ This section reports those models where mbpfan was tested successfully. It does Warning ------- Be sure to load the kernel modules **applesmc** and **coretemp**. +These modules are often automatically loaded when booting up GNU/Linux on a Macbook. If that is not the case, you should make sure to load them at system startup. This is _usually_ achieved by inserting the following two lines in the file `/etc/modules` +``` +coretemp +applesmc +``` +Please check the relevant documentation of your GNU/Linux distribution. Arch Linux --------- From e31b272d277f772adda649270b713aab3572c4e2 Mon Sep 17 00:00:00 2001 From: Nicholas Lydeen Date: Fri, 27 Mar 2015 21:38:48 -0500 Subject: [PATCH 073/235] Altered kernel version check to allow 4.x.x kernels --- src/mbpfan.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 42f0178..17dd759 100755 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -71,9 +71,9 @@ bool is_legacy_kernel() char *str_kernel_version; str_kernel_version = strtok(kernel.release, "."); - if (atoi(str_kernel_version) != 3){ - syslog(LOG_INFO, "mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); - printf("mbpfan detected a non 3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); + if (atoi(str_kernel_version) < 3){ + syslog(LOG_INFO, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); + printf("mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); exit(EXIT_FAILURE); } @@ -90,7 +90,7 @@ bool is_legacy_kernel() } } - return (kernel_version < 15); + return (atoi(kernel.release) == 3 && kernel_version < 15); } From 2f8946d0ac78e0b16114ca575cb5eea5c3b649d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Tue, 5 May 2015 16:54:41 +0200 Subject: [PATCH 074/235] let the test pass on kernel >= 4 --- src/minunit.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/minunit.c b/src/minunit.c index d618529..6e6dc59 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -17,16 +17,17 @@ static const char *test_is_legacy_kernel() struct utsname kernel; uname(&kernel); + + int kern_major_release = (int)(kernel.release[0] - '0'); - mu_assert("Kernel major version is not 3.", kernel.release[0] == '3'); - + mu_assert("Kernel major version is minor than 3.", kern_major_release > 2); char *pch; pch = strtok(kernel.release, "."); pch = strtok(NULL, "."); int minor = atoi(pch); - if (minor < 15) + if ((kern_major_release <= 3) && minor < 15) mu_assert("Legacy kernel not detected by mbpfan.", is_legacy_kernel() == 1); else mu_assert("Non-legacy kernel not detected by mbpfan", is_legacy_kernel() == 0); @@ -214,6 +215,7 @@ int tests() { printf("Starting the tests..\n"); printf("It is normal for them to take a bit to finish.\n"); + const char *result = all_tests(); if (result != 0) { @@ -226,4 +228,4 @@ int tests() printf("Tests run: %d\n", tests_run); return result != 0; -} \ No newline at end of file +} From 976aa809ab078e4c80822c07af476e8ede337edf Mon Sep 17 00:00:00 2001 From: Thomas Burette Date: Thu, 18 Jun 2015 17:06:32 +0200 Subject: [PATCH 075/235] Fix execution right mistakenly set on source file --- src/daemon.c | 0 src/mbpfan.c | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/daemon.c mode change 100755 => 100644 src/mbpfan.c diff --git a/src/daemon.c b/src/daemon.c old mode 100755 new mode 100644 diff --git a/src/mbpfan.c b/src/mbpfan.c old mode 100755 new mode 100644 From c7936d125e0cb558112a1632718c059924f41d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Ahlstr=C3=B6m?= Date: Sun, 30 Aug 2015 22:04:01 -0400 Subject: [PATCH 076/235] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 4c53151..27d7996 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) - MacBook Air 5,2 (unknown) +Tested iMac Models +------------------ +This section reports the iMac models where mbpfan was tested successfully. + +- iMac5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) Warning ------- From e59254c4cbe15f77fd78b265c4d2d23aa437606e Mon Sep 17 00:00:00 2001 From: Jens Timmerman Date: Thu, 22 Oct 2015 14:33:30 +0200 Subject: [PATCH 077/235] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27d7996..8e350ea 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ To automatically run mbpfan at boot, run as root: As a special bonus, a service file for systemd is also included. To use it, execute the following (as root): - sudo cp mbpfan.service /usr/lib/systemd/system + sudo cp mbpfan.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl start mbpfan.service From 2f876b6c850fabb9707017db331bf12b0b764d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=85=9E-Xs?= Date: Sun, 20 Dec 2015 23:22:52 +0800 Subject: [PATCH 078/235] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8e350ea..b12c79c 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Tested Macbook Models --------------------- This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. +- MacBook Pro 12,1 13" (Intel i5 - Linux 4.20) - MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - MacBook Pro 8,2 15" (Intel i7 - Linux 3.6.2) From 6270d07ff318365a2dab673ff9c9377e25a63666 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sat, 30 Jan 2016 17:36:45 +0100 Subject: [PATCH 079/235] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b12c79c..49eaead 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,8 @@ Ubuntu Install the ```build-essential``` package. Then, refer to the Generic Install Instructions. +Otherwise, a step-by-step [tutorial for beginners is available on my website](https://ineed.coffee/3838/a-beginners-mbpfan-tutorial-for-ubuntu/). + Generic Install Instructions ------------------------- From e7c60f4b110ff33ff7dd8283b7fa44c1568f167d Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 7 Feb 2016 13:44:30 +0100 Subject: [PATCH 080/235] Create COPYING --- COPYING | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 674 insertions(+) create mode 100644 COPYING diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..9cecc1d --- /dev/null +++ b/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. From 10b9621299f85b02e2fb12d684ae18d3a7300f78 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 25 Feb 2016 11:37:08 +0100 Subject: [PATCH 081/235] [Fixes #73] - Added MacBook 1,1 tu README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 49eaead..0ccd7c7 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) - MacBook Air 5,2 (unknown) +- MacBook 1,1 (Intel Core Duo - Linux 3.16) Tested iMac Models ------------------ From 150fcad08402632fe201c8f90bb8669dcc9e0915 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Mon, 14 Mar 2016 01:07:20 +0000 Subject: [PATCH 082/235] Fix buffer overflow in sprintf --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 17dd759..f369c39 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -129,7 +129,7 @@ t_sensors *retrieve_sensors() int counter; for (counter = 0; counter < 10; counter++) { - char hwmon_path[strlen(path_begin)+1]; + char hwmon_path[strlen(path_begin)+3]; sprintf(hwmon_path, "%s%d", path_begin, counter); From 584b137b2e25e566ef4683244b5ee252aaf3ecca Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 17 Mar 2016 11:11:23 +0100 Subject: [PATCH 083/235] [Fix #68] run tests within source directory --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ccd7c7..97b606f 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ It copies mbpfan to /usr/sbin and mbpfan.conf to /etc Run The Tests (Recommended) --------------------------- -It is recommended to run the tests after installing the program +It is recommended to run the tests after installing the program. Please run the following command _from within the source directory_. sudo ./bin/mbpfan -t From 9306d98ef41da41a11389531a369795647bd1fe6 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 17 Mar 2016 11:15:16 +0100 Subject: [PATCH 084/235] [Fix #65] Correct instructions for systemd --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 97b606f..3f64047 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ As a special bonus, a service file for systemd is also included. To use it, execute the following (as root): sudo cp mbpfan.service /etc/systemd/system/ + sudo systemctl enable mbpfan.service sudo systemctl daemon-reload sudo systemctl start mbpfan.service From 2446a120ee6021d5b2f18696e64ffc98314ed85d Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 17 Mar 2016 12:03:02 +0000 Subject: [PATCH 085/235] Use only 1 extra character for counter --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index f369c39..96ae841 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -129,7 +129,7 @@ t_sensors *retrieve_sensors() int counter; for (counter = 0; counter < 10; counter++) { - char hwmon_path[strlen(path_begin)+3]; + char hwmon_path[strlen(path_begin)+2]; sprintf(hwmon_path, "%s%d", path_begin, counter); From 0e3437b6a570a213f58f347d04267f936706f0b3 Mon Sep 17 00:00:00 2001 From: Pavel Kozlov Date: Thu, 31 Mar 2016 19:12:35 +0300 Subject: [PATCH 086/235] Update Gentoo instructions, resolve #75 --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f64047..5c0f176 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,16 @@ Then, refer to the Generic Install Instructions. Otherwise, a step-by-step [tutorial for beginners is available on my website](https://ineed.coffee/3838/a-beginners-mbpfan-tutorial-for-ubuntu/). +Gentoo +------ + +Type the following command to add the overlay + + sudo layman -a andjscott + +Then install it + + sudo emerge -av mbpfan Generic Install Instructions ------------------------- @@ -148,11 +158,6 @@ To add the script to the default runlevels, run the following as root: chkconfig --level 2345 mbpfan on && chkconfig --level 016 mbpfan off **Gentoo** -An init file is available for gentoo users: mbpfan.init.gentoo -To install, run as root: - - chmod +x mbpfan.init.gentoo - cp mbpfan.init.gentoo /etc/init.d/mbpfan To automatically run mbpfan at boot, run as root: From 31ef9858743eeaed8ba1f970f1d60b970c007e24 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 2 May 2016 11:26:29 +0200 Subject: [PATCH 087/235] [Fix #77] added Upstart instructions in README --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5c0f176..213ea1b 100644 --- a/README.md +++ b/README.md @@ -132,9 +132,17 @@ If installed and using the init file, run with (Ubuntu example) Starting at boot ---------------- **Ubuntu** -For upstart based init systems (Ubuntu), an example upstart job has been -provided for use in place of the LSB-style init script. -To use, execute: + +For systemd based init systems (Ubuntu 16.04+), the file mbpfan.service +has been provided. +For using it, execute: + + sudo cp mbpfan.service /etc/systemd/system/ + sudo systemctl enable mbpfan.service + + +For upstart based init systems (Ubuntu before 16.04), an example upstart job has been provided. +For using it, execute: sudo cp mbpfan.upstart /etc/init/mbpfan.conf sudo start mbpfan From 393504e9abaaf29409ba085761ce22b20b99f888 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 5 Jun 2016 09:55:17 +0200 Subject: [PATCH 088/235] [Fixes #79] mbpfan.conf self-explains itself --- mbpfan.conf | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mbpfan.conf b/mbpfan.conf index 5f2fd5c..1c1c01d 100644 --- a/mbpfan.conf +++ b/mbpfan.conf @@ -1,7 +1,8 @@ [general] -min_fan_speed = 2000 # default is 2000 -max_fan_speed = 6200 # default is 6200 +# see https://ineed.coffee/3838/a-beginners-tutorial-for-mbpfan-under-ubuntu for the values +min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" +max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 -max_temp = 86 # do not set it > 90, default is 86 -polling_interval = 7 # default is 7 +max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 +polling_interval = 7 # default is 7 seconds From 5db89e467f4312d5ad1a2053e4bd48f32efddd96 Mon Sep 17 00:00:00 2001 From: Herminio Hernandez Jr Date: Sun, 3 Jul 2016 18:53:51 -0700 Subject: [PATCH 089/235] created manpage for the binary --- mbpfan.8.gz | Bin 0 -> 580 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 mbpfan.8.gz diff --git a/mbpfan.8.gz b/mbpfan.8.gz new file mode 100644 index 0000000000000000000000000000000000000000..e9152b9630b07f1cca7d81da2fd6a5cd0b5e6661 GIT binary patch literal 580 zcmV-K0=xYmiwFp!=yq2E18rh(W?^nFH~@W9O>5gg5WVlO7;;JutF;>nrj&vc+l`^N zjHDb&)5A&{Sxco|u^&m>U*Fl43!yCqf#rEK@6DSTWlvXdvZg`j!1u~Qrv`)<<$IP9 zbhf7JfR9oGucT{~g0Y@Dsk3FoSyny3;qJ6sA7KSoj}9#yqz-a$gq06)TpzYF zRaBsd{-N%rZm@-Cv`!ffri1k`5OQb=i5HnfQhq+>r*dDi?CuhVFZrq3=g0DEO4w}6 z`(mFWuX5N-m|hdFG;w^VFK4!L_K32D*`#xbF1WomKSt8gFb-~@#wspgU{>^ z;aU#H-^4y^<1k6?_Mf&<5Cr;0To&FK-Lb4F$sdCI;b=;9CoMt*)Hzt`qtAFh)OfK? z^}$&&s9MbV?dA73C7sK&YVZ%%_8*L?t@UH|kQZztwZZ{DTKQ8MPk!NjJBQX4*^lel zUJe0ukoz3rhRcAys=6SoW{uKW;u-# zEf00jK8VCaG%{Z7*jS0gHHtTj Date: Sun, 3 Jul 2016 19:12:15 -0700 Subject: [PATCH 090/235] Update Makefile to use install instad of cp. The manpage, systemd .service file, and README will also be installed. This change will allow the building of .deb packages. --- Makefile | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 6bee6a2..0d28970 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ OUTPUT_PATH = bin/ SOURCE_PATH = src/ EXE = bin/mbpfan CONF = mbpfan.conf +SYSD = mbpfan.service +DOC = README.md +MAN = mbpfan.8.gz ifeq ($(COMPILER), G++) ifeq ($(OS),Windows_NT) @@ -23,8 +26,8 @@ ifeq ($(COMPILER), G++) LIBS = -lm # LIBPATH = -L../gc/.libs LIBPATH = - CPPFLAGS = $(COPT) -g $(INCLUDES) - LDFLAGS = $(LIBPATH) -g $(LIBS) + CPPFLAGS += $(COPT) -g $(INCLUDES) -Wall + LDFLAGS += $(LIBPATH) -g $(LIBS) -Wall DEP = dep else OBJ = obj @@ -61,11 +64,22 @@ tests: uninstall: rm /usr/sbin/mbpfan rm /etc/mbpfan.conf + rm /lib/systemd/system/mbpfan.service + rm /usr/share/man/man8/mbpfan.8.gz + rm -rf /usr/share/doc/mbpfan install: make - cp $(EXE) /usr/sbin - cp -n $(CONF) /etc + install -d $(DESTDIR)/usr/sbin + install -d $(DESTDIR)/etc + install -d $(DESTDIR)/lib/systemd/system + install -d $(DESTDIR)/usr/share/doc/mbpfan + install $(EXE) $(DESTDIR)/usr/sbin + install -m644 $(CONF) $(DESTDIR)/etc + install -m644 $(SYSD) $(DESTDIR)/lib/systemd/system + install -m644 $(DOC) $(DESTDIR)/usr/share/doc/mbpfan + install -d $(DESTDIR)/usr/share/man/man8 + install -m644 $(MAN) $(DESTDIR)/usr/share/man/man8 @echo "" @echo "******************" @echo "INSTALL COMPLETED" From 6eb4bf350c94ac1eeee7bf10ffcb15cea5eafa46 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 12 Jul 2016 10:50:11 +0200 Subject: [PATCH 091/235] Added Mac Mini to list of tested devices --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 213ea1b..7735c03 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,12 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Air 5,2 (unknown) - MacBook 1,1 (Intel Core Duo - Linux 3.16) -Tested iMac Models +Tested iMac/Mini Models ------------------ This section reports the iMac models where mbpfan was tested successfully. - iMac5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) +- Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) Warning ------- From d3c086b5011a0450da814acf43ec72b2407bb826 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 21 Jul 2016 13:37:52 +0200 Subject: [PATCH 092/235] [Fixes #81] added imac 12,1 to list of tested devices --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7735c03..f4c9772 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,8 @@ Tested iMac/Mini Models ------------------ This section reports the iMac models where mbpfan was tested successfully. -- iMac5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) +- iMac 12,1 21.5" (Intel i5 - Linux 4.6.4) +- iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) - Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) Warning From c2e302d66390cf6de9f71238f40c8ef491217708 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Thu, 21 Jul 2016 14:01:47 +0200 Subject: [PATCH 093/235] Corrected some strings in the README.md Changed the iMac to the iMac/Mac-mini and I also corrected the info I gave to dgraziotin by correcting i5 to i7. That's all. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f4c9772..fd42fb7 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,9 @@ This section reports those models where mbpfan was tested successfully. It does Tested iMac/Mini Models ------------------ -This section reports the iMac models where mbpfan was tested successfully. +This section reports the iMac/Mac-mini models where mbpfan was tested successfully. -- iMac 12,1 21.5" (Intel i5 - Linux 4.6.4) +- iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) - iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) - Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) From ffd07561b5b55093bc45ad4edae30629278d4cec Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 29 Aug 2016 10:00:27 +0200 Subject: [PATCH 094/235] [Fix #85] correct test cases for settings --- src/minunit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/minunit.c b/src/minunit.c index 6e6dc59..58af2b5 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -151,7 +151,7 @@ static const char *test_config_file() static const char *test_settings() { retrieve_settings("./mbpfan.conf.test1"); - mu_assert("min_fan_speed value is not 6200", min_fan_speed == 6200); + mu_assert("max_fan_speed value is not 6200", max_fan_speed == 6200); mu_assert("polling_interval is not 1", polling_interval == 1); retrieve_settings("./mbpfan.conf"); mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000); @@ -188,6 +188,7 @@ static const char *test_settings_reload() { signal(SIGHUP, handler); retrieve_settings("./mbpfan.conf"); + printf("Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); mu_assert("min_fan_speed value is not 2000 before SIGHUP", min_fan_speed == 2000); mu_assert("polling_interval is not 7 before SIHUP", polling_interval == 7); raise(SIGHUP); From adccefb1f75da53601f8e59039ad8fad46567f70 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 29 Aug 2016 10:29:11 +0200 Subject: [PATCH 095/235] polished authorship status at beginning of files --- src/daemon.c | 2 +- src/daemon.h | 2 +- src/main.c | 2 +- src/mbpfan.c | 8 ++------ src/mbpfan.h | 2 +- 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 93b0e26..de11a27 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,7 +1,7 @@ /** * Copyright (C) 2012 Peter Lombardo - * Modifications (2012) by Daniel Graziotin * Modifications (2012) by Ismail Khatib + * Modifications (2012-present) by Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/daemon.h b/src/daemon.h index 35b7592..dbbc17f 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -1,5 +1,5 @@ /** - * Copyright (C) (2012) Daniel Graziotin + * Copyright (C) (2012-present) Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/main.c b/src/main.c index e8af708..7bd14df 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ /** - * Copyright (C) (2012-2014) Daniel Graziotin + * Copyright (C) (2012-present) Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/mbpfan.c b/src/mbpfan.c index 96ae841..7870055 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -2,8 +2,8 @@ * mbpfan.c - automatically control fan for MacBook Pro * Copyright (C) 2010 Allan McRae * Modifications by Rafael Vega - * Modifications (2012) by Daniel Graziotin * Modifications (2012) by Ismail Khatib + * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,7 +15,6 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * - * 2014-06-22 - v1.7.0 * * Notes: * Assumes any number of processors and fans (max. 10) @@ -23,10 +22,7 @@ * Requires coretemp and applesmc kernel modules to be loaded. * Requires root use * - * Tested models: - * MacBook Pro 8.1 13" (Intel i7 - Linux 3.2) - * MacBook Pro 6,2 15" (Intel i7 - Linux 3.2) - * MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) + * Tested models: see README.md */ diff --git a/src/mbpfan.h b/src/mbpfan.h index d6250fa..12f7fdc 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -1,6 +1,6 @@ /** * Copyright (C) 2010 Allan McRae - * Modifications (2012) by Daniel Graziotin + * Modifications (2012-present) by Daniel Graziotin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 72e902f7725022750a909ad85b8ce0dbb881c0ed Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 31 Aug 2016 09:26:22 +0200 Subject: [PATCH 096/235] [#80] New Makefile without systemd part --- Makefile | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 0d28970..fe02a4a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,6 @@ OUTPUT_PATH = bin/ SOURCE_PATH = src/ EXE = bin/mbpfan CONF = mbpfan.conf -SYSD = mbpfan.service DOC = README.md MAN = mbpfan.8.gz @@ -26,8 +25,8 @@ ifeq ($(COMPILER), G++) LIBS = -lm # LIBPATH = -L../gc/.libs LIBPATH = - CPPFLAGS += $(COPT) -g $(INCLUDES) -Wall - LDFLAGS += $(LIBPATH) -g $(LIBS) -Wall + CPPFLAGS += $(COPT) -g $(INCLUDES) #-Wall + LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall DEP = dep else OBJ = obj @@ -76,7 +75,6 @@ install: install -d $(DESTDIR)/usr/share/doc/mbpfan install $(EXE) $(DESTDIR)/usr/sbin install -m644 $(CONF) $(DESTDIR)/etc - install -m644 $(SYSD) $(DESTDIR)/lib/systemd/system install -m644 $(DOC) $(DESTDIR)/usr/share/doc/mbpfan install -d $(DESTDIR)/usr/share/man/man8 install -m644 $(MAN) $(DESTDIR)/usr/share/man/man8 @@ -85,7 +83,7 @@ install: @echo "INSTALL COMPLETED" @echo "******************" @echo "" - @echo "A configuration file has been copied to /etc/mbpfan.conf" + @echo "A configuration file has been copied (might overwrite existing file) to /etc/mbpfan.conf." @echo "See README.md file to have mbpfan automatically started at system boot." @echo "" @echo "Please run the tests now with the command" From b46a2f0b200044f6d6c046847f82737b6a44744b Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 31 Aug 2016 09:39:53 +0200 Subject: [PATCH 097/235] [80] little changes to man page --- mbpfan.8.gz | Bin 580 -> 595 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/mbpfan.8.gz b/mbpfan.8.gz index e9152b9630b07f1cca7d81da2fd6a5cd0b5e6661..8b97c4ca3afba97ad33137bebb1a14af89e91f9d 100644 GIT binary patch literal 595 zcmV-Z0<8TXiwFoth{jg{18rh(W?^nFH~?i+%Wm5+5WMp%7CAXUDcKDQ1VvHQZk)zI z{ea{gilzrjT3JLyQs5(La_UF)3;QKqQfd&O7anF`GqcO=?hFpr)aV@ePC961kMJmc z$1)0?tZ7@|N@(Cm;c6*CRYNOuwuo_-9dBT}I$SSza0zF(4lQhj4x)DyD{o-8+^*SJ z0e2i=2~MhkCNu(-(clf#Mtf^|h$(JBY=Cgl_plsdVr|qAJX!~Te*K0S>Q3mIk{$?> zlxbR6_C3{Z>q6kf?mP`IzwP#ia#ONwHR14me>iUTyYfd$xLTK=i%mjaM_=`B4|=QiXz2y3qdzy(5GOGcy!*4-~(GB%tUYec~n#x zhiT;X=Ic5RgdBaHPxnJ>Z&_B9W$a@n&DJ zE1@O!@WqN}X*^Mc4^4tTNlnI6@3uw!m`b+$Vhx+!^`3R!5AJ=QpXj(lMM)}ePXAg= zv~u>6_PSMmad~l>3tr(#YE8@HK6qoL=s7vV$68MApyi5gg5WVlO7;;JutF;>nrj&vc+l`^N zjHDb&)5A&{Sxco|u^&m>U*Fl43!yCqf#rEK@6DSTWlvXdvZg`j!1u~Qrv`)<<$IP9 zbhf7JfR9oGucT{~g0Y@Dsk3FoSyny3;qJ6sA7KSoj}9#yqz-a$gq06)TpzYF zRaBsd{-N%rZm@-Cv`!ffri1k`5OQb=i5HnfQhq+>r*dDi?CuhVFZrq3=g0DEO4w}6 z`(mFWuX5N-m|hdFG;w^VFK4!L_K32D*`#xbF1WomKSt8gFb-~@#wspgU{>^ z;aU#H-^4y^<1k6?_Mf&<5Cr;0To&FK-Lb4F$sdCI;b=;9CoMt*)Hzt`qtAFh)OfK? z^}$&&s9MbV?dA73C7sK&YVZ%%_8*L?t@UH|kQZztwZZ{DTKQ8MPk!NjJBQX4*^lel zUJe0ukoz3rhRcAys=6SoW{uKW;u-# zEf00jK8VCaG%{Z7*jS0gHHtTj Date: Wed, 31 Aug 2016 09:45:05 +0200 Subject: [PATCH 098/235] Added my iMac to compatible models and clarified README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fd42fb7..ffc5da7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -Fan-Control-Daemon +mbpfan ==================== Introduction --------------------- This is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) -Fan-Control-Daemon is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. +mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). * It only uses the temperatures from the processors as input. @@ -52,6 +52,7 @@ Tested iMac/Mini Models ------------------ This section reports the iMac/Mac-mini models where mbpfan was tested successfully. +- iMac Retina 16.2 21.5" (Intel i5 - Linux 4.4.0 Ubuntu 16.04) - iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) - iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) - Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) @@ -102,7 +103,8 @@ Install with sudo make install -It copies mbpfan to /usr/sbin and mbpfan.conf to /etc +It copies mbpfan to /usr/sbin, mbpfan.conf to /etc, +README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 Run The Tests (Recommended) From ce0afe2b24b2343739ec50982b8211de1b48668a Mon Sep 17 00:00:00 2001 From: JR1994 Date: Thu, 1 Sep 2016 11:49:50 -0700 Subject: [PATCH 099/235] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 5196437..d949c5a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -11,6 +11,7 @@ Magnus Stubman Olivier Tilmans Yamakaky Yi Yang +Herminio Hernandez Jr ORIGINARY AUTHORS From 0efcf930c560a84f21ee1529eef23591f68a6d55 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Tue, 13 Sep 2016 10:03:12 +0200 Subject: [PATCH 100/235] [Fix #87] Tested on Mac Mini 6,1 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ffc5da7..07e3fc0 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ This section reports the iMac/Mac-mini models where mbpfan was tested successful - iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) - iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) - Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) +- Mac Mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) Warning ------- From e32e5eb3c84be65cc49aa9ee4dad9ab05603a957 Mon Sep 17 00:00:00 2001 From: Larutan Date: Tue, 11 Oct 2016 13:43:20 +0200 Subject: [PATCH 101/235] Update README.md Tested on Macbook Air 5,2. Added processor and linux version details. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 07e3fc0..bb87ac1 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) -- MacBook Air 5,2 (unknown) +- MacBook Air 5,2 13" (Intel i5 - Linux 3.16) - MacBook 1,1 (Intel Core Duo - Linux 3.16) Tested iMac/Mini Models From 6e010e41162471c39602960d131719aed4825688 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Fri, 18 Nov 2016 14:16:59 -0500 Subject: [PATCH 102/235] added return 0; to line 106 to prevent core dump when compiled with clang --- src/.minunit.c.swp | Bin 0 -> 16384 bytes src/minunit.c | 1 + 2 files changed, 1 insertion(+) create mode 100644 src/.minunit.c.swp diff --git a/src/.minunit.c.swp b/src/.minunit.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..7f81ba70d840c30416c43509e784fd0408ac56cf GIT binary patch literal 16384 zcmeI3e~cSP700)iw6s8gmY;1=HFaoe-z7dfp^3QMMLqJPIk=`F&V``RT5Ef3PqN;% zcE@+QhBW+)+Oz_MD#VZSQ&deU1R(@86`+bg5PyJF2}p%NL+|1s1-P~nvKW#46K&pXM1E~g54Wt@KHIQl`)j+C&SF#3Vd&D{h0Y|iupVaSH z^t`Xo*9Y|br+V(|%k(YPK&pXM1E~g54Wt@KHIQl`)j+C&R0F97QVpaUcm-;}u36Sb z$aR7e0Qmmj#Q%SKy=6TEegwV=J_|ky1egQUU<|wkycvvuE5Mo8S=O(?ufUVw8{jLT z0&H+C7y-XqWm!J~KL%e1UjuD$Be)Jc^;*k14W0mxgOlJR;278gZUPoK{~F7B4tx=O z0o)5#gX_WD!1-5O);aJm@B;WN_zQRr{1iM59sw@c2PVL~00S>yV_DCFUxHtN?}2ZF zZ-E6MKm*jl`@mgbJy;3;ezj$t1%CkF2OkIbfuq0y2Y~<^!CS$VU;D$bnVh-$)3(3|<0_MAAjuI>W2gDceE7eVNT{ z^*TgbO zwv)j}(?TZ4Zk}R=+-7U(9$WU|GJc&>4%(d8F1_1CGt2HxdOdTZ$^l5J*;S+CCeVJdowkjLT%k*&NmeHOl04 z#56wR&fAWtv7MsP1lje4!T_%_ zQs3ct729EZ@i;sZ7RT8RJr7!lEv(4atcgS_-e#H`Bu{a;JTaYN=C!QdSvS|oxtC>K zKH7a?iaf&?e4d+oSqymiY<9IXWGW-`b;-~|VyG1NpH)X02>m=-din%l*x(LEaq>paS8XwmM z%w~>|(&4NkU1%)gwI8`6!xZ4;no^7<7?J3Kxp|+Lwl=7h{Zvpk_0-_XX8je`q zhhh`dGkD!)(oEsNLDxpr3Z-^{wk?}xzfxUC>XO(E(R-`CZi~wg%b_NeY;0^S90FN} zsAtvVw9R=P+3v`)>CY_tR4ueA>dyF#QLVBf^5RHVnh%Sfl2jIql7JACK8NK?4V!de z`bf5-)D!bax?b8HQA3Re)o>OG)fTO-O2muTWlZ>zS_4_l+zv7TrJVh{B*-Iz|O$ze5`{mM?3yaH~!S8&}5s zF1?wF6`EsTK2aLlRCk06$udW@gbb2pw6i2j0$FoJ zrLRnQR5U)S0wtO<`iXs+AGTXG(iJU96t@lz&XU~FM#L4 z@4?T&55afAcfcv|3Ggvc2PN<(a1~eyR)F6kW`7nu1wI3A2hU$)S)T{TK>_>`G4^Tj z1b7^r1doCH!Eta8m;l#Iio5rNS@0h44)AB_?<_bCo&*$se;6DC zK9~hNz;r_;tcvC73fA6iQ{Htc>Z+cd0-Nns4T-;Jq;RfwP3ht=slgrVNR6I*N| z;S~u()gfjqKNIRPo5?4#c994st=#q;2h&G+V(ueaGfA-`v+I<{D9!Ag+YR&XtB2^o zoZn*s8O~I(L@nCuaMR*OP?AASv2h;HD0%|rcZ=%7}e=}q?_X$=tFtZ zTG1@rJ)IQ8uB#NihH9Cxs7kOPWVM+Mhpd+C?u5*HP^X?KivF}7b73GB=~BgEgPcC* zeSJ= Date: Fri, 18 Nov 2016 14:25:30 -0500 Subject: [PATCH 103/235] Delete .minunit.c.swp created by vim --- src/.minunit.c.swp | Bin 16384 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/.minunit.c.swp diff --git a/src/.minunit.c.swp b/src/.minunit.c.swp deleted file mode 100644 index 7f81ba70d840c30416c43509e784fd0408ac56cf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3e~cSP700)iw6s8gmY;1=HFaoe-z7dfp^3QMMLqJPIk=`F&V``RT5Ef3PqN;% zcE@+QhBW+)+Oz_MD#VZSQ&deU1R(@86`+bg5PyJF2}p%NL+|1s1-P~nvKW#46K&pXM1E~g54Wt@KHIQl`)j+C&SF#3Vd&D{h0Y|iupVaSH z^t`Xo*9Y|br+V(|%k(YPK&pXM1E~g54Wt@KHIQl`)j+C&R0F97QVpaUcm-;}u36Sb z$aR7e0Qmmj#Q%SKy=6TEegwV=J_|ky1egQUU<|wkycvvuE5Mo8S=O(?ufUVw8{jLT z0&H+C7y-XqWm!J~KL%e1UjuD$Be)Jc^;*k14W0mxgOlJR;278gZUPoK{~F7B4tx=O z0o)5#gX_WD!1-5O);aJm@B;WN_zQRr{1iM59sw@c2PVL~00S>yV_DCFUxHtN?}2ZF zZ-E6MKm*jl`@mgbJy;3;ezj$t1%CkF2OkIbfuq0y2Y~<^!CS$VU;D$bnVh-$)3(3|<0_MAAjuI>W2gDceE7eVNT{ z^*TgbO zwv)j}(?TZ4Zk}R=+-7U(9$WU|GJc&>4%(d8F1_1CGt2HxdOdTZ$^l5J*;S+CCeVJdowkjLT%k*&NmeHOl04 z#56wR&fAWtv7MsP1lje4!T_%_ zQs3ct729EZ@i;sZ7RT8RJr7!lEv(4atcgS_-e#H`Bu{a;JTaYN=C!QdSvS|oxtC>K zKH7a?iaf&?e4d+oSqymiY<9IXWGW-`b;-~|VyG1NpH)X02>m=-din%l*x(LEaq>paS8XwmM z%w~>|(&4NkU1%)gwI8`6!xZ4;no^7<7?J3Kxp|+Lwl=7h{Zvpk_0-_XX8je`q zhhh`dGkD!)(oEsNLDxpr3Z-^{wk?}xzfxUC>XO(E(R-`CZi~wg%b_NeY;0^S90FN} zsAtvVw9R=P+3v`)>CY_tR4ueA>dyF#QLVBf^5RHVnh%Sfl2jIql7JACK8NK?4V!de z`bf5-)D!bax?b8HQA3Re)o>OG)fTO-O2muTWlZ>zS_4_l+zv7TrJVh{B*-Iz|O$ze5`{mM?3yaH~!S8&}5s zF1?wF6`EsTK2aLlRCk06$udW@gbb2pw6i2j0$FoJ zrLRnQR5U)S0wtO<`iXs+AGTXG(iJU96t@lz&XU~FM#L4 z@4?T&55afAcfcv|3Ggvc2PN<(a1~eyR)F6kW`7nu1wI3A2hU$)S)T{TK>_>`G4^Tj z1b7^r1doCH!Eta8m;l#Iio5rNS@0h44)AB_?<_bCo&*$se;6DC zK9~hNz;r_;tcvC73fA6iQ{Htc>Z+cd0-Nns4T-;Jq;RfwP3ht=slgrVNR6I*N| z;S~u()gfjqKNIRPo5?4#c994st=#q;2h&G+V(ueaGfA-`v+I<{D9!Ag+YR&XtB2^o zoZn*s8O~I(L@nCuaMR*OP?AASv2h;HD0%|rcZ=%7}e=}q?_X$=tFtZ zTG1@rJ)IQ8uB#NihH9Cxs7kOPWVM+Mhpd+C?u5*HP^X?KivF}7b73GB=~BgEgPcC* zeSJ= Date: Tue, 27 Dec 2016 23:20:08 +0100 Subject: [PATCH 104/235] Tested on MacBook Air 1,1 Tested successfully on MacBook Air 1,1 on Ubuntu 16.04 and Ubuntu 16.10,. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bb87ac1..39cc06d 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) - MacBook Air 5,2 13" (Intel i5 - Linux 3.16) +- MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8) - MacBook 1,1 (Intel Core Duo - Linux 3.16) Tested iMac/Mini Models From b897f515f48ae33de49f3563c1fe3f2db906c2fb Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 28 Dec 2016 11:11:19 -0500 Subject: [PATCH 105/235] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index bb87ac1..746c046 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,10 @@ Install with It copies mbpfan to /usr/sbin, mbpfan.conf to /etc, README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 +Compile with Clang +------------------------- +Edit the 'Makefile' and replace G++ with clang++ +Tested and working with Clang 3.9 Run The Tests (Recommended) --------------------------- From cbafd1cb3be52129950507b3f261ad0385f079bb Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 28 Dec 2016 11:12:25 -0500 Subject: [PATCH 106/235] Update README.md Added simple instructions on how to compile with Clang --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 746c046..403e170 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,7 @@ README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 Compile with Clang ------------------------- Edit the 'Makefile' and replace G++ with clang++ + Tested and working with Clang 3.9 Run The Tests (Recommended) From 38eb9e0f91307ea6b0f90ba6b638cf6875d5a9e7 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 28 Dec 2016 11:13:02 -0500 Subject: [PATCH 107/235] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index d949c5a..ae03d37 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,6 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr +Robert Musial ORIGINARY AUTHORS From 754caf6e56c590c11af681c09e1d61b5ff2cd71c Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 28 Dec 2016 16:40:21 +0000 Subject: [PATCH 108/235] [Fix #96] Clang initial support, thanks @rmusial --- README.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 45152ec..51f0489 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ Then install it sudo emerge -av mbpfan + Generic Install Instructions ------------------------- Compile with @@ -105,14 +106,28 @@ Install with sudo make install -It copies mbpfan to /usr/sbin, mbpfan.conf to /etc, +It copies mbpfan to /usr/sbin, mbpfan.conf to /etc (and overwrites existing files), README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 -Compile with Clang -------------------------- +Run the tests now, see two sections below. + +If you would like to compile with Clang instead of GCC, see the next section. + +Compile with Clang (experimental) +--------------------------------- +We are providing an initial experimental support for [Clang and LLVM](http://clang.llvm.org/). +Please go to the next section if you are following a standard installation procedure (most people do). + Edit the 'Makefile' and replace G++ with clang++ -Tested and working with Clang 3.9 +Please run + + make clean + +Before attempting to compile again with Clang. + +Tested and working with Clang 3.8 and 3.9. + Run The Tests (Recommended) --------------------------- From 11ec211b892f2488eee80ac0cbe1bcf3df7ab85e Mon Sep 17 00:00:00 2001 From: Aaron Riekenberg Date: Wed, 28 Dec 2016 16:00:32 -0600 Subject: [PATCH 109/235] Flush standard out before sleeping in verbose mode. --- src/mbpfan.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index 7870055..7066c3b 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -557,6 +557,7 @@ void mbpfan() if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); + fflush(stdout); if(daemonize) { syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); From a8e5a4cc30a06aa04c5bf0a387d2a575d37b2a09 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Thu, 29 Dec 2016 19:03:10 +0100 Subject: [PATCH 110/235] [Fix #98] runscript-> openrc-run gentoo init --- mbpfan.init.gentoo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbpfan.init.gentoo b/mbpfan.init.gentoo index 30a35ea..cd41258 100644 --- a/mbpfan.init.gentoo +++ b/mbpfan.init.gentoo @@ -1,4 +1,4 @@ -#!/sbin/runscript +#!/sbin/openrc-run depend() { use logger From 17708813dd9a17b4888d26fbc42946a8940c186f Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Mon, 16 Jan 2017 11:46:47 +1100 Subject: [PATCH 111/235] Add Macmini5,3 to supported list sudo dmidecode -s system-product-name Macmini5,3 uname -a Linux elementaty-mini 4.4.0-59-generic #80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux sudo make tests make install make[1]: Entering directory '/home/wmoore/Source/mbpfan' make make[2]: Entering directory '/home/wmoore/Source/mbpfan' Linking... g++ -g -lm src/settings.o src/daemon.o src/mbpfan.o src/main.o src/minunit.o src/strmap.o -lm -o bin/mbpfan make[2]: Leaving directory '/home/wmoore/Source/mbpfan' install -d /usr/sbin install -d /etc install -d /lib/systemd/system install -d /usr/share/doc/mbpfan install bin/mbpfan /usr/sbin install -m644 mbpfan.conf /etc install -m644 README.md /usr/share/doc/mbpfan install -d /usr/share/man/man8 install -m644 mbpfan.8.gz /usr/share/man/man8 ****************** INSTALL COMPLETED ****************** A configuration file has been copied (might overwrite existing file) to /etc/mbpfan.conf. See README.md file to have mbpfan automatically started at system boot. Please run the tests now with the command sudo make tests make[1]: Leaving directory '/home/wmoore/Source/mbpfan' /usr/sbin/mbpfan -f -v -t Starting the tests.. It is normal for them to take a bit to finish. Detected kernel version: 4 Detected kernel minor revision: 4 Detected kernel version: 4 Detected kernel minor revision: 4 Using new sensor path for kernel >= 3.0.15 Found hwmon path at /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp Found 5 sensors Found 1 fans Detected kernel version: 4 Detected kernel minor revision: 4 Using new sensor path for kernel >= 3.0.15 Found hwmon path at /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp Found 5 sensors Testing the _supplied_ mbpfan.conf (not the one you are using).. ALL TESTS PASSED Tests run: 8 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 51f0489..6523a22 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ This section reports the iMac/Mac-mini models where mbpfan was tested successful - iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) - iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) - Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) +- Mac Mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) - Mac Mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) Warning From a99aaad680dde54974b73d208933b96c1bdb614b Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 19 Mar 2017 13:43:55 -0400 Subject: [PATCH 112/235] Changes to be committed: modified: AUTHORS Updated personal info. modified: Makefile Beginning work to clean up Makefile. The original makefile appeared to be checking if the system was Windows NT. Since this is for GNU/ Linux, that was removed. Also all of the code is C, but the Makefile was calling the C++ compiler to hide bool issues and missing typedefs (which C++ is ok with but C is not). modified: src/daemon.c Added structs, added stdbool.h modified: src/main.c Added stdbool.h modified: src/mbpfan.c Added stdbool.h modified: src/minunit.c Added stdbool.h --- AUTHORS | 2 +- Makefile | 56 ++++++++++++++++----------------------------------- src/daemon.c | 6 ++++-- src/main.c | 4 +++- src/mbpfan.c | 2 ++ src/minunit.c | 1 + 6 files changed, 28 insertions(+), 43 deletions(-) diff --git a/AUTHORS b/AUTHORS index ae03d37..a2898c8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS diff --git a/Makefile b/Makefile index fe02a4a..169c62d 100644 --- a/Makefile +++ b/Makefile @@ -1,60 +1,38 @@ -COMPILER=G++ +COMPILER=cc -# todo: object files into output path, processing c / c++ files in the same time (?), nested directories for source files (?) C = c OUTPUT_PATH = bin/ SOURCE_PATH = src/ -EXE = bin/mbpfan +BIN = bin/mbpfan CONF = mbpfan.conf DOC = README.md MAN = mbpfan.8.gz -ifeq ($(COMPILER), G++) - ifeq ($(OS),Windows_NT) - OBJ = obj - else - OBJ = o - endif - COPT = - CCMD = g++ - OBJFLAG = -o - EXEFLAG = -o -# INCLUDES = -I../.includes - INCLUDES = -# LIBS = -lgc - LIBS = -lm -# LIBPATH = -L../gc/.libs - LIBPATH = - CPPFLAGS += $(COPT) -g $(INCLUDES) #-Wall - LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall - DEP = dep -else - OBJ = obj - COPT = /O2 - CCMD = cl - OBJFLAG = /Fo - EXEFLAG = /Fe -# INCLUDES = /I..\\.includes - INCLUDES = -# LIBS = ..\\.libs\\libgc.lib - LIBS = - CPPFLAGS = $(COPT) /DEBUG $(INCLUDES) - LDFLAGS = /DEBUG -endif +COPT = +CC = cc +OBJFLAG = -o +BINFLAG = -o +INCLUDES = +LIBS = -lm +LIBPATH = +CFLAGS += $(COPT) -g $(INCLUDES) #-Wall +LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall +DEP = dep + OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) %.$(OBJ):%.$(C) mkdir -p bin @echo Compiling $(basename $<)... - $(CCMD) -c $(CPPFLAGS) $(CXXFLAGS) $< $(OBJFLAG)$@ + $(CC) -c $(CFLAGS) $< $(OBJFLAG)$@ all: $(OBJS) @echo Linking... - $(CCMD) $(LDFLAGS) $^ $(LIBS) $(EXEFLAG) $(EXE) + $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(BIN) clean: - rm -rf $(SOURCE_PATH)*.$(OBJ) $(EXE) + rm -rf $(SOURCE_PATH)*.$(OBJ) $(BIN) tests: make install @@ -73,7 +51,7 @@ install: install -d $(DESTDIR)/etc install -d $(DESTDIR)/lib/systemd/system install -d $(DESTDIR)/usr/share/doc/mbpfan - install $(EXE) $(DESTDIR)/usr/sbin + install $(BIN) $(DESTDIR)/usr/sbin install -m644 $(CONF) $(DESTDIR)/etc install -m644 $(DOC) $(DESTDIR)/usr/share/doc/mbpfan install -d $(DESTDIR)/usr/share/man/man8 diff --git a/src/daemon.c b/src/daemon.c index de11a27..27a0e9d 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,6 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -77,7 +79,7 @@ static void cleanup_and_exit(int exit_code) delete_pid(); set_fans_auto(fans); - s_fans *next_fan; + struct s_fans *next_fan; while (fans != NULL) { next_fan = fans->next; free(fans->fan_output_path); @@ -86,7 +88,7 @@ static void cleanup_and_exit(int exit_code) fans = next_fan; } - s_sensors *next_sensor; + struct s_sensors *next_sensor; while (sensors != NULL) { next_sensor = sensors->next; free(sensors->path); diff --git a/src/main.c b/src/main.c index 7bd14df..0a4cc99 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -145,4 +147,4 @@ int main(int argc, char *argv[]) void (*fan_control)() = mbpfan; go_daemon(fan_control); exit(EXIT_SUCCESS); -} \ No newline at end of file +} diff --git a/src/mbpfan.c b/src/mbpfan.c index 7870055..ad57e1c 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,6 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,6 +33,7 @@ #include #include #include +#include #include #include #include "mbpfan.h" diff --git a/src/minunit.c b/src/minunit.c index 1042cf8..3653309 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "global.h" #include "mbpfan.h" From e339f811fccb49b7f41963be934853ed12289a6c Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 19 Mar 2017 13:54:29 -0400 Subject: [PATCH 113/235] Changes to be committed: modified: README.md Changed instructions for how to use Clang. It is much easier since the Makefile now just uses the system's compiler, and correctly uses C instead of C++ --- README.md | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6523a22..85a62e1 100644 --- a/README.md +++ b/README.md @@ -112,22 +112,8 @@ README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 Run the tests now, see two sections below. -If you would like to compile with Clang instead of GCC, see the next section. - -Compile with Clang (experimental) ---------------------------------- -We are providing an initial experimental support for [Clang and LLVM](http://clang.llvm.org/). -Please go to the next section if you are following a standard installation procedure (most people do). - -Edit the 'Makefile' and replace G++ with clang++ - -Please run - - make clean - -Before attempting to compile again with Clang. - -Tested and working with Clang 3.8 and 3.9. +If you would like to compile with Clang instead of GCC, simply set your system's +default compiler to be Clang (tested with Clang 3.8) Run The Tests (Recommended) From c72090d2bbcf31cf786a5853a3d5ed3427a4fef4 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 20 Mar 2017 14:15:11 -0400 Subject: [PATCH 114/235] Update AUTHORS --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index a2898c8..ae03d37 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS From c52a624bac98a5354a38c8e52e32158b21fdf01d Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 20 Mar 2017 14:15:58 -0400 Subject: [PATCH 115/235] Update daemon.c --- src/daemon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon.c b/src/daemon.c index 27a0e9d..0ec8ead 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,7 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From b71375f6afb8a102a23b13850f297aee61e5b709 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 20 Mar 2017 14:16:21 -0400 Subject: [PATCH 116/235] Update main.c --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 0a4cc99..df5389a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From a7d3eb265df6b54c3cfb9051333be0c6d3736217 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 20 Mar 2017 14:16:50 -0400 Subject: [PATCH 117/235] Update mbpfan.c --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index ad57e1c..af294bc 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,7 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 8ed45aa5c2e783ec91bb700aa2282ce01bf980e5 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 22 Mar 2017 00:31:11 -0400 Subject: [PATCH 118/235] Update daemon.c --- src/daemon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon.c b/src/daemon.c index 0ec8ead..611eef8 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,7 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 13543b3b5550e3f22cf2902d448463014f2e48e2 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 22 Mar 2017 00:32:04 -0400 Subject: [PATCH 119/235] Update main.c --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index df5389a..7e67a5f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 75002c070263d1e56cc0bd45688ad89b8f3b6ec3 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 22 Mar 2017 00:33:58 -0400 Subject: [PATCH 120/235] Update mbpfan.c --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index af294bc..e3d1736 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,7 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From eda7485de9022744fa0a76881dcb697ae75c28ea Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Wed, 22 Mar 2017 00:35:46 -0400 Subject: [PATCH 121/235] Update AUTHORS --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index ae03d37..7fcf2bd 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS From 34cf49932dd6eb3370ae8b847057f809725da9c6 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Fri, 24 Mar 2017 18:01:01 -0400 Subject: [PATCH 122/235] Update AUTHORS --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 7fcf2bd..ae03d37 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS From e2a62a9d21ce81b29997c79898cba5f9627f9982 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Fri, 24 Mar 2017 18:01:41 -0400 Subject: [PATCH 123/235] Update daemon.c --- src/daemon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon.c b/src/daemon.c index 611eef8..0ec8ead 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,7 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From b149238c297e6970aee3a400fe3488bc72e0ea94 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Fri, 24 Mar 2017 18:02:43 -0400 Subject: [PATCH 124/235] Update main.c --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 7e67a5f..df5389a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 2f5c736b6909deeb3fd9a89e591171a0ea395d97 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Fri, 24 Mar 2017 18:03:03 -0400 Subject: [PATCH 125/235] Update mbpfan.c --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index e3d1736..af294bc 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,7 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 1cf14e00c4aae38c4734fc25189fbec97545c1d8 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 26 Mar 2017 01:51:27 -0400 Subject: [PATCH 126/235] Update AUTHORS --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index ae03d37..5c87ba3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS From 28ca34edb25ba0dd0345f89bee539867f650a27d Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 26 Mar 2017 01:52:09 -0400 Subject: [PATCH 127/235] Update mbpfan.c --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index af294bc..e3d1736 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,7 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From b454fcef60479c42cb1dadc4ade357fce441d763 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 26 Mar 2017 01:52:45 -0400 Subject: [PATCH 128/235] Update main.c --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index df5389a..7e67a5f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 591b37a0e3e02fd5bf25557cfc6636905a875ee1 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Sun, 26 Mar 2017 01:53:12 -0400 Subject: [PATCH 129/235] Update daemon.c --- src/daemon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon.c b/src/daemon.c index 0ec8ead..611eef8 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,7 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From e7dd42e1255ad9e10134ca0f107c0a4f1b3eb7eb Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 27 Mar 2017 11:44:34 -0400 Subject: [PATCH 130/235] changes to legacy kernel detection --- AUTHORS | 2 +- README.md | 3 ++- src/daemon.c | 2 +- src/main.c | 2 +- src/mbpfan.c | 34 ++++++++++++++++++++++------------ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/AUTHORS b/AUTHORS index 5c87ba3..ae03d37 100644 --- a/AUTHORS +++ b/AUTHORS @@ -12,7 +12,7 @@ Olivier Tilmans Yamakaky Yi Yang Herminio Hernandez Jr -Robert Musial +Robert Musial ORIGINARY AUTHORS diff --git a/README.md b/README.md index 85a62e1..ed113bc 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ Supported distributions: - RedHat - CentOS - Gentoo - +- Alpine +- Trisquel Tested Macbook Models --------------------- diff --git a/src/daemon.c b/src/daemon.c index 611eef8..0ec8ead 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -2,7 +2,7 @@ * Copyright (C) 2012 Peter Lombardo * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/main.c b/src/main.c index 7e67a5f..df5389a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /** * Copyright (C) (2012-present) Daniel Graziotin - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/mbpfan.c b/src/mbpfan.c index e3d1736..b122845 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -4,7 +4,7 @@ * Modifications by Rafael Vega * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] - * Modifications (2017-present) by Robert Musial + * Modifications (2017-present) by Robert Musial * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,20 +75,30 @@ bool is_legacy_kernel() exit(EXIT_FAILURE); } - str_kernel_version = strtok(NULL, "."); - int kernel_version = atoi(str_kernel_version); + + // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen + fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); - if(verbose) { - printf("Detected kernel version: %s\n", kernel.release); - printf("Detected kernel minor revision: %s\n", str_kernel_version); - - if(daemonize) { - syslog(LOG_INFO, "Kernel version: %s", kernel.release); - syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); - } + if (errno == EISDIR) { + return 0; + } else { + return 1; } - return (atoi(kernel.release) == 3 && kernel_version < 15); + // str_kernel_version = strtok(NULL, "."); + // int kernel_version = atoi(str_kernel_version); + + // if(verbose) { + // printf("Detected kernel version: %s\n", kernel.release); + // printf("Detected kernel minor revision: %s\n", str_kernel_version); + + // if(daemonize) { + // syslog(LOG_INFO, "Kernel version: %s", kernel.release); + // syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); + // } + // } + + // return (atoi(kernel.release) == 3 && kernel_version < 15); } From a7ca28f6867e6790cbce552f84fb69f45a9fc582 Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 27 Mar 2017 12:59:09 -0400 Subject: [PATCH 131/235] Removed unneeded line from Makefile --- Makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Makefile b/Makefile index 169c62d..ace8c0c 100644 --- a/Makefile +++ b/Makefile @@ -17,8 +17,6 @@ LIBS = -lm LIBPATH = CFLAGS += $(COPT) -g $(INCLUDES) #-Wall LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall -DEP = dep - OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) From 32256bc3a2e445f9b4eac62634b1e588d538d91a Mon Sep 17 00:00:00 2001 From: Robert Musial Date: Mon, 27 Mar 2017 13:03:02 -0400 Subject: [PATCH 132/235] Updated README to reflect new testing with LLVM toolchain --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ed113bc..62185e4 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,8 @@ README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 Run the tests now, see two sections below. If you would like to compile with Clang instead of GCC, simply set your system's -default compiler to be Clang (tested with Clang 3.8) +default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang +4.0 along with llvm-lld (The LLVM Linker). Run The Tests (Recommended) From c3e1c9c2b789985d9390472ba4105b9c49b89667 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 2 Apr 2017 16:31:39 +0000 Subject: [PATCH 133/235] Reverted to old legacy kernel detection [#92 needs investigation] --- src/mbpfan.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index b122845..fc19865 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -75,30 +75,31 @@ bool is_legacy_kernel() exit(EXIT_FAILURE); } - + // possible fix for https://github.com/dgraziotin/mbpfan/issues/92 + // to be soon investigated. // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen - fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); + // fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); - if (errno == EISDIR) { - return 0; - } else { - return 1; - } + // if (errno == EISDIR) { + // return 0; + // } else { + // return 1; + // } - // str_kernel_version = strtok(NULL, "."); - // int kernel_version = atoi(str_kernel_version); + str_kernel_version = strtok(NULL, "."); + int kernel_version = atoi(str_kernel_version); - // if(verbose) { - // printf("Detected kernel version: %s\n", kernel.release); - // printf("Detected kernel minor revision: %s\n", str_kernel_version); + if(verbose) { + printf("Detected kernel version: %s\n", kernel.release); + printf("Detected kernel minor revision: %s\n", str_kernel_version); - // if(daemonize) { - // syslog(LOG_INFO, "Kernel version: %s", kernel.release); - // syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); - // } - // } + if(daemonize) { + syslog(LOG_INFO, "Kernel version: %s", kernel.release); + syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); + } + } - // return (atoi(kernel.release) == 3 && kernel_version < 15); + return (atoi(kernel.release) == 3 && kernel_version < 15); } From 592895dc610a53e8a031cfcd4601a96edca7cce4 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 2 Apr 2017 16:50:20 +0000 Subject: [PATCH 134/235] [Fix #105] fixed memory leak in retrieve_sensors() --- src/mbpfan.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index 57dec44..7a81c8d 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -228,6 +228,9 @@ t_sensors *retrieve_sensors() exit(EXIT_FAILURE); } + free(path_begin); + path_begin = NULL; + return sensors_head; } From 1ee0e1f1fa6a899ea965965fc26470f1a6fad114 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Sun, 2 Apr 2017 16:59:50 +0000 Subject: [PATCH 135/235] [fix #92] support for CentOS w/ Linux 3.10.0 but new sensors path --- src/mbpfan.c | 46 +++++++++++++++++++++++----------------------- src/mbpfan.h | 2 +- src/minunit.c | 23 ----------------------- src/minunit.h | 1 - 4 files changed, 24 insertions(+), 48 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 7a81c8d..0f7efa4 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -61,7 +61,7 @@ t_sensors* sensors = NULL; t_fans* fans = NULL; -bool is_legacy_kernel() +bool is_legacy_sensors_path() { struct utsname kernel; uname(&kernel); @@ -75,31 +75,31 @@ bool is_legacy_kernel() exit(EXIT_FAILURE); } - // possible fix for https://github.com/dgraziotin/mbpfan/issues/92 - // to be soon investigated. + // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen - // fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); + fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); - // if (errno == EISDIR) { - // return 0; - // } else { - // return 1; - // } + if (errno == EISDIR) { + return 0; + } else { + return 1; + } - str_kernel_version = strtok(NULL, "."); - int kernel_version = atoi(str_kernel_version); + // + // str_kernel_version = strtok(NULL, "."); + // int kernel_version = atoi(str_kernel_version); - if(verbose) { - printf("Detected kernel version: %s\n", kernel.release); - printf("Detected kernel minor revision: %s\n", str_kernel_version); + // if(verbose) { + // printf("Detected kernel version: %s\n", kernel.release); + // printf("Detected kernel minor revision: %s\n", str_kernel_version); - if(daemonize) { - syslog(LOG_INFO, "Kernel version: %s", kernel.release); - syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); - } - } + // if(daemonize) { + // syslog(LOG_INFO, "Kernel version: %s", kernel.release); + // syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); + // } + // } - return (atoi(kernel.release) == 3 && kernel_version < 15); + // return (atoi(kernel.release) == 3 && kernel_version < 15); } @@ -112,7 +112,7 @@ t_sensors *retrieve_sensors() char *path = NULL; char *path_begin = NULL; - if (is_legacy_kernel()) { + if (is_legacy_sensors_path()) { if(verbose) { printf("Using legacy sensor path for kernel < 3.15.0\n"); @@ -126,10 +126,10 @@ t_sensors *retrieve_sensors() } else { if(verbose) { - printf("Using new sensor path for kernel >= 3.0.15\n"); + printf("Using new sensor path for kernel >= 3.0.15 or some CentOS versions with kernel 3.10.0\n"); if(daemonize) { - syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15"); + syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15 or some CentOS versions with kernel 3.10.0 "); } } diff --git a/src/mbpfan.h b/src/mbpfan.h index 12f7fdc..31f04a4 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -46,7 +46,7 @@ typedef struct s_fans t_fans; /** * Return true if the kernel is < 3.15.0 */ -bool is_legacy_kernel(); +bool is_legacy_sensors_path(); /** * Tries to use the settings located in diff --git a/src/minunit.c b/src/minunit.c index 3653309..7f6e053 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -13,28 +13,6 @@ int tests_run = 0; -static const char *test_is_legacy_kernel() -{ - - struct utsname kernel; - uname(&kernel); - - int kern_major_release = (int)(kernel.release[0] - '0'); - - mu_assert("Kernel major version is minor than 3.", kern_major_release > 2); - char *pch; - pch = strtok(kernel.release, "."); - pch = strtok(NULL, "."); - - int minor = atoi(pch); - - if ((kern_major_release <= 3) && minor < 15) - mu_assert("Legacy kernel not detected by mbpfan.", is_legacy_kernel() == 1); - else - mu_assert("Non-legacy kernel not detected by mbpfan", is_legacy_kernel() == 0); - - return 0; -} static const char *test_sensor_paths() { @@ -203,7 +181,6 @@ static const char *test_settings_reload() static const char *all_tests() { - mu_run_test(test_is_legacy_kernel); mu_run_test(test_sensor_paths); mu_run_test(test_fan_paths); mu_run_test(test_get_temp); diff --git a/src/minunit.h b/src/minunit.h index e60bd4a..c1ab00b 100644 --- a/src/minunit.h +++ b/src/minunit.h @@ -12,7 +12,6 @@ extern int tests_run; -static const char *test_is_legacy_kernel(); static const char *test_sensor_paths(); static const char *test_fan_paths(); static const char *test_get_temp(); From 7590a5ae3f42202b16d570bf1bda04bf60ee920f Mon Sep 17 00:00:00 2001 From: Alex Kir Date: Mon, 3 Apr 2017 03:45:36 +0800 Subject: [PATCH 136/235] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 62185e4..c62972d 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) - MacBook Air 5,2 13" (Intel i5 - Linux 3.16) - MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8) +- MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) - MacBook 1,1 (Intel Core Duo - Linux 3.16) Tested iMac/Mini Models From 7384b3ad9feb8472208c3dc0c8cc6aebdcf9023d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6kt=C3=BCrk=20Y=C3=BCksek?= Date: Sun, 2 Apr 2017 16:16:32 -0400 Subject: [PATCH 137/235] Update the install instructions for Gentoo Linux --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 62185e4..de46a15 100644 --- a/README.md +++ b/README.md @@ -89,14 +89,9 @@ Otherwise, a step-by-step [tutorial for beginners is available on my website](ht Gentoo ------ -Type the following command to add the overlay - - sudo layman -a andjscott - -Then install it - - sudo emerge -av mbpfan +Install the ```mbpfan``` package with: + sudo emerge -av app-laptop/mbpfan Generic Install Instructions ------------------------- From d965ec7b1bc5c96390af0c4f8e8d39a5823734b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6kt=C3=BCrk=20Y=C3=BCksek?= Date: Sun, 2 Apr 2017 16:43:59 -0400 Subject: [PATCH 138/235] Makefile: do not call make recursively inside the install target Instead of calling make inside the install target to make sure that mbpfan is compiled prior to installing, declare $(BIN) as a prerequisite for the target. This also enables parallel compilation from the install target in addition to avoiding unnecessary re-linking. --- Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ace8c0c..ed2390a 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,9 @@ OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) @echo Compiling $(basename $<)... $(CC) -c $(CFLAGS) $< $(OBJFLAG)$@ -all: $(OBJS) +all: $(BIN) + +$(BIN): $(OBJS) @echo Linking... $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(BIN) @@ -43,8 +45,7 @@ uninstall: rm /usr/share/man/man8/mbpfan.8.gz rm -rf /usr/share/doc/mbpfan -install: - make +install: $(BIN) install -d $(DESTDIR)/usr/sbin install -d $(DESTDIR)/etc install -d $(DESTDIR)/lib/systemd/system From 4730988419365135157f041fd8023f43eed57e08 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 21 Jun 2017 11:26:32 +0200 Subject: [PATCH 139/235] [fix #118] protect macro definitions --- src/mbpfan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 0f7efa4..a9cd563 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -41,8 +41,8 @@ #include "settings.h" /* lazy min/max... */ -#define min(a,b) a < b ? a : b -#define max(a,b) a > b ? a : b +#define min(a,b) ((a) < (b) ? (a) : (b)) +#define max(a,b) ((a) > (b) ? (a) : (b)) int min_fan_speed = 2000; int max_fan_speed = 6200; From f20a2274e12f4481f2ed9075ff2bf0404cbcc065 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 21 Jun 2017 11:32:55 +0200 Subject: [PATCH 140/235] [#117] call closedir(dir) when checking requirements --- src/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.c b/src/main.c index df5389a..0568a46 100644 --- a/src/main.c +++ b/src/main.c @@ -85,6 +85,8 @@ void check_requirements() exit(EXIT_FAILURE); } + closedir(dir); + } pclose(fd); @@ -100,6 +102,8 @@ void check_requirements() exit(EXIT_FAILURE); } + closedir(dir); + } pclose(fd); From 5f2da79651612e1991307ebaabe7d5c6aa7d9262 Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Wed, 21 Jun 2017 11:39:16 +0200 Subject: [PATCH 141/235] [Fix #117] eliminate redundant exec() in check_requirements --- src/main.c | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/src/main.c b/src/main.c index 0568a46..d447cbd 100644 --- a/src/main.c +++ b/src/main.c @@ -59,7 +59,7 @@ void check_requirements() /** * Check for root - */ + */ uid_t uid=getuid(), euid=geteuid(); @@ -71,42 +71,28 @@ void check_requirements() /** * Check for coretemp and applesmc modules - * Credits: -http://stackoverflow.com/questions/12978794 */ - FILE *fd = popen("lsmod | grep coretemp", "r"); - char buf[16]; - - if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { - DIR* dir = opendir(CORETEMP_PATH); - - if (ENOENT == errno) { - syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); + DIR* dir = opendir(CORETEMP_PATH); + if (ENOENT == errno) { + syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + exit(EXIT_FAILURE); } - pclose(fd); + closedir(dir); - fd = popen("lsmod | grep applesmc", "r"); - if (!(fread (buf, 1, sizeof (buf), fd) > 0)) { - DIR* dir = opendir(APPLESMC_PATH); - - if (ENOENT == errno) { - syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); + dir = opendir(APPLESMC_PATH); + if (ENOENT == errno) { + syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); + printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + exit(EXIT_FAILURE); } - pclose(fd); + closedir(dir); + } From 35f85c73c016a289c00abe0ba336ba75add467d5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 12 Jul 2017 18:15:38 -0700 Subject: [PATCH 142/235] Fix README nits --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 661e96c..c6bbd64 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Supported distributions: - Alpine - Trisquel -Tested Macbook Models +Tested MacBook Models --------------------- This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. @@ -51,7 +51,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) - MacBook 1,1 (Intel Core Duo - Linux 3.16) -Tested iMac/Mini Models +Tested iMac/Mac Mini Models ------------------ This section reports the iMac/Mac-mini models where mbpfan was tested successfully. @@ -65,7 +65,7 @@ This section reports the iMac/Mac-mini models where mbpfan was tested successful Warning ------- Be sure to load the kernel modules **applesmc** and **coretemp**. -These modules are often automatically loaded when booting up GNU/Linux on a Macbook. If that is not the case, you should make sure to load them at system startup. This is _usually_ achieved by inserting the following two lines in the file `/etc/modules` +These modules are often automatically loaded when booting up GNU/Linux on a MacBook. If that is not the case, you should make sure to load them at system startup. This is _usually_ achieved by inserting the following two lines in the file `/etc/modules` ``` coretemp applesmc From 12a6bb0daa11472aeb0abfc7273945e5e045b517 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 12 Jul 2017 19:18:13 -0700 Subject: [PATCH 143/235] Correct kernel version message to >= 3.15.0 This agrees with the other message which emits < 3.15.0. --- src/mbpfan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index a9cd563..b9cea0d 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -126,10 +126,10 @@ t_sensors *retrieve_sensors() } else { if(verbose) { - printf("Using new sensor path for kernel >= 3.0.15 or some CentOS versions with kernel 3.10.0\n"); + printf("Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0\n"); if(daemonize) { - syslog(LOG_INFO, "Using new sensor path for kernel >= 3.0.15 or some CentOS versions with kernel 3.10.0 "); + syslog(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); } } From 230f0be0877c7c0a643b26ca64fa2c067a5ff65a Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 12 Jul 2017 19:55:06 -0700 Subject: [PATCH 144/235] Do not attempt to catch uncatchable SIGSTOP Eliminates a Valgrind warning: ==19239== Warning: ignored attempt to set SIGSTOP handler in sigaction(); ==19239== the SIGSTOP signal is uncatchable --- src/daemon.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 0ec8ead..8bb57e5 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -116,10 +116,6 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGINT signal."); cleanup_and_exit(EXIT_SUCCESS); - case SIGSTOP: - syslog(LOG_WARNING, "Received SIGSTOP signal."); - cleanup_and_exit(EXIT_SUCCESS); - default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); break; @@ -133,7 +129,6 @@ void go_daemon(void (*fan_control)()) signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - signal(SIGSTOP, signal_handler); syslog(LOG_INFO, "%s starting up", PROGRAM_NAME); From 9034e1f843a2cae7d16cf3eda823db86efdfb509 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Jul 2017 17:04:03 -0700 Subject: [PATCH 145/235] Rewrite confusing conditional --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index a9cd563..c9d2596 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -222,7 +222,7 @@ t_sensors *retrieve_sensors() } } - if (!sensors_found > 0){ + if (sensors_found == 0){ syslog(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.\n"); printf("mbpfan could not detect any temp sensor. Please contact the developer.\n"); exit(EXIT_FAILURE); From 399aad782a2daf478ab3801e1fc12f8f23ec1ab6 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Jul 2017 15:28:23 -0700 Subject: [PATCH 146/235] Do not repeatedly reopen fan and sensor devices This avoids unnecessary system calls and saves battery. --- src/daemon.c | 6 ++++++ src/global.h | 4 +++- src/mbpfan.c | 26 ++++++++++++-------------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 0ec8ead..0a5dbb4 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -82,6 +82,9 @@ static void cleanup_and_exit(int exit_code) struct s_fans *next_fan; while (fans != NULL) { next_fan = fans->next; + if (fans->file != NULL) { + fclose(fans->file); + } free(fans->fan_output_path); free(fans->fan_manual_path); free(fans); @@ -91,6 +94,9 @@ static void cleanup_and_exit(int exit_code) struct s_sensors *next_sensor; while (sensors != NULL) { next_sensor = sensors->next; + if (sensors->file != NULL) { + fclose(sensors->file); + } free(sensors->path); free(sensors); sensors = next_sensor; diff --git a/src/global.h b/src/global.h index 9b64acf..19db7c1 100644 --- a/src/global.h +++ b/src/global.h @@ -8,13 +8,15 @@ extern const char* PROGRAM_NAME; extern const char* PROGRAM_PID; struct s_sensors { + FILE* file; char* path; unsigned int temperature; struct s_sensors *next; }; struct s_fans { - char* path; + FILE* file; + char* path; // TODO: unused char* fan_output_path; char* fan_manual_path; struct s_fans *next; diff --git a/src/mbpfan.c b/src/mbpfan.c index a9cd563..b090087 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -206,7 +206,7 @@ t_sensors *retrieve_sensors() tmp->next->next = NULL; } - fclose(file); + s->file = file; sensors_found++; } @@ -273,7 +273,7 @@ t_fans *retrieve_fans() strncat( path_manual, path_man_end, strlen(path_begin) ); - FILE *file = fopen(path_output, "r"); + FILE *file = fopen(path_output, "w"); if(file != NULL) { fan = (t_fans *) malloc( sizeof( t_fans ) ); @@ -297,7 +297,7 @@ t_fans *retrieve_fans() tmp->next->next = NULL; } - fclose(file); + fan->file = file; fans_found++; } @@ -362,11 +362,11 @@ t_sensors *refresh_sensors(t_sensors *sensors) t_sensors *tmp = sensors; while(tmp != NULL) { - FILE *file = fopen(tmp->path, "r"); - - if(file != NULL) { - fscanf(file, "%d", &tmp->temperature); - fclose(file); + if(tmp->file != NULL) { + char buf[16]; + int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0); + buf[len] = '\0'; + sscanf(buf, "%d", &tmp->temperature); } tmp = tmp->next; @@ -380,14 +380,12 @@ t_sensors *refresh_sensors(t_sensors *sensors) void set_fan_speed(t_fans* fans, int speed) { t_fans *tmp = fans; - FILE *file; while(tmp != NULL) { - file = fopen(tmp->fan_output_path, "rw+"); - - if(file != NULL) { - fprintf(file, "%d", speed); - fclose(file); + if(tmp->file != NULL) { + char buf[16]; + int len = snprintf(buf, sizeof(buf), "%d", speed); + pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0); } tmp = tmp->next; From e95e19a3ea19d72c6b3134df05b4c9b596e7c80e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 20 Jul 2017 03:49:31 -0700 Subject: [PATCH 147/235] Avoid freeing static strings --- src/mbpfan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index ecd4969..3d0b3da 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -121,7 +121,7 @@ t_sensors *retrieve_sensors() } } - path_begin = (char *) "/sys/devices/platform/coretemp.0/temp"; + path_begin = strdup("/sys/devices/platform/coretemp.0/temp"); } else { @@ -133,7 +133,7 @@ t_sensors *retrieve_sensors() } } - path_begin = (char *) "/sys/devices/platform/coretemp.0/hwmon/hwmon"; + path_begin = strdup("/sys/devices/platform/coretemp.0/hwmon/hwmon"); int counter; for (counter = 0; counter < 10; counter++) { From cc970129fa42bae02278af71bf652262b11ae573 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Jul 2017 15:02:27 -0700 Subject: [PATCH 148/235] Catch SIGQUIT This resets automatic fan control. --- src/daemon.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/daemon.c b/src/daemon.c index 326301e..7a94985 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -118,6 +118,10 @@ void signal_handler(int signal) syslog(LOG_WARNING, "Received SIGTERM signal."); cleanup_and_exit(EXIT_SUCCESS); + case SIGQUIT: + syslog(LOG_WARNING, "Received SIGQUIT signal."); + cleanup_and_exit(EXIT_SUCCESS); + case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); cleanup_and_exit(EXIT_SUCCESS); @@ -134,6 +138,7 @@ void go_daemon(void (*fan_control)()) // Setup signal handling before we start signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); + signal(SIGQUIT, signal_handler); signal(SIGINT, signal_handler); syslog(LOG_INFO, "%s starting up", PROGRAM_NAME); From 8c2e11caae436a45aa74e845bc424f74028e048c Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 21 Jul 2017 18:21:15 -0700 Subject: [PATCH 149/235] Enable -Wall and -Wextra --- Makefile | 2 +- src/strmap.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ed2390a..b5e1868 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ BINFLAG = -o INCLUDES = LIBS = -lm LIBPATH = -CFLAGS += $(COPT) -g $(INCLUDES) #-Wall +CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) diff --git a/src/strmap.c b/src/strmap.c index d553b44..45853e3 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -389,7 +389,7 @@ static unsigned long hash(const char *str) unsigned long hash = 5381; int c; - while (c = *str++) { + while ((c = *str++)) { hash = ((hash << 5) + hash) + c; } From 7297a18e1a9c9312bf4dc295b19b0029b1ba32ec Mon Sep 17 00:00:00 2001 From: Doug Beney Date: Thu, 10 Aug 2017 15:46:56 -0400 Subject: [PATCH 150/235] TOC, coretemp/applesmc directions, Macbook support, Solus I added a table of contents for easy navigation, added the directions you provided me on figuring out if my system has applesmc/coretemp installed, I added my Macbook to tested devices, and I added Solus to supported operating systems. --- README.md | 98 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index c6bbd64..230e49a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,9 @@ mbpfan ==================== -Introduction ---------------------- This is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) -mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. +mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). * It only uses the temperatures from the processors as input. @@ -15,9 +13,24 @@ This enhanced version assumes any number of processors and fans (max. 10). * Verbose mode for both syslog and stdout * Users can configure it using the file /etc/mbpfan.conf +**Table Of Contents** + +- [Supported GNU/Linux Distributions](#supported-gnulinux-distributions) +- [Tested Macbook Models](#tested-macbook-models) +- [Tested iMac/Mac Mini Models](#tested-imacmac-mini-models) +- [Requirements](#requirements) +- [Installation](#installation) + - [Arch Linux](#arch-linux) + - [Ubuntu](#ubuntu) + - [Gentoo](#gentoo) + - [Generic Install Instructions (All Other Operating Systems)](#generic-install-instructions-all-other-operating-systems) +- [Run Instructions](#run-instructions) +- [Starting at boot](#starting-at-boot) +- [Usage](#usage) +- [License](#license) + +## Supported GNU/Linux Distributions -Supported GNU/Linux Distributions ---------------------------------- We provide scripts to to load mbpfan daemon at system boot for many distros. Please note that the support is provided by volunteers. mbpfan needs tests and bug reports. @@ -32,12 +45,14 @@ Supported distributions: - Gentoo - Alpine - Trisquel +- Solus + +## Tested Macbook Models -Tested MacBook Models ---------------------- -This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. +This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. - MacBook Pro 12,1 13" (Intel i5 - Linux 4.20) +- MacBook Pro 11,4 15" (Intel i7 - Linux 4.9.41) - MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - MacBook Pro 8,2 15" (Intel i7 - Linux 3.6.2) @@ -51,8 +66,8 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) - MacBook 1,1 (Intel Core Duo - Linux 3.16) -Tested iMac/Mac Mini Models ------------------- +## Tested iMac/Mac Mini Models + This section reports the iMac/Mac-mini models where mbpfan was tested successfully. - iMac Retina 16.2 21.5" (Intel i5 - Linux 4.4.0 Ubuntu 16.04) @@ -62,10 +77,24 @@ This section reports the iMac/Mac-mini models where mbpfan was tested successful - Mac Mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) - Mac Mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) -Warning -------- +## Requirements + Be sure to load the kernel modules **applesmc** and **coretemp**. -These modules are often automatically loaded when booting up GNU/Linux on a MacBook. If that is not the case, you should make sure to load them at system startup. This is _usually_ achieved by inserting the following two lines in the file `/etc/modules` + +These modules are often automatically loaded when booting up GNU/Linux on a MacBook. If that is not the case, you should make sure to load them at system startup. + +**How do I know if applesmc and coretemp are loaded?** + +In most distributions, you can run the following command: + +`lsmod | grep coretemp && lsmod | grep applesmc` + +If you see `coretemp` and `applesmc` listed, you are all set. + +**If you do not see `coretemp` and `applesmc` listed, you must load them.** + +This is _usually_ achieved by inserting the following two lines in the file `/etc/modules` + ``` coretemp applesmc @@ -73,29 +102,29 @@ applesmc Please check the relevant documentation of your GNU/Linux distribution. -Arch Linux ---------- +## Installation + +### Arch Linux + See [mbpfan-git at AUR](https://aur.archlinux.org/packages/mbpfan-git/). Otherwise, please refer to the Generic Instructions. -Ubuntu ------- +### Ubuntu Install the ```build-essential``` package. Then, refer to the Generic Install Instructions. Otherwise, a step-by-step [tutorial for beginners is available on my website](https://ineed.coffee/3838/a-beginners-mbpfan-tutorial-for-ubuntu/). -Gentoo ------- +### Gentoo Install the ```mbpfan``` package with: sudo emerge -av app-laptop/mbpfan -Generic Install Instructions -------------------------- +### Generic Install Instructions (All Other Operating Systems) + Compile with make @@ -104,14 +133,14 @@ Install with sudo make install -It copies mbpfan to /usr/sbin, mbpfan.conf to /etc (and overwrites existing files), +It copies mbpfan to /usr/sbin, mbpfan.conf to /etc (and overwrites existing files), README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 Run the tests now, see two sections below. If you would like to compile with Clang instead of GCC, simply set your system's default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang -4.0 along with llvm-lld (The LLVM Linker). +4.0 along with llvm-lld (The LLVM Linker). Run The Tests (Recommended) @@ -125,8 +154,8 @@ or sudo make tests -Run Instructions ----------------- +## Run Instructions + If not installed, run with sudo bin/mbpfan @@ -140,8 +169,8 @@ If installed and using the init file, run with (Ubuntu example) sudo service mbpfan start -Starting at boot ----------------- +## Starting at boot + **Ubuntu** For systemd based init systems (Ubuntu 16.04+), the file mbpfan.service @@ -152,14 +181,14 @@ For using it, execute: sudo systemctl enable mbpfan.service -For upstart based init systems (Ubuntu before 16.04), an example upstart job has been provided. +For upstart based init systems (Ubuntu before 16.04), an example upstart job has been provided. For using it, execute: sudo cp mbpfan.upstart /etc/init/mbpfan.conf sudo start mbpfan **Debian** -An init file suitable for /lib/lsb/init-functions (Debian) +An init file suitable for /lib/lsb/init-functions (Debian) is located in the main folder of the source files, called mbpfan.init.debian Rename it to mbpfan, give it execution permissions (chmod +x mbpfan) and move it to /etc/init.d @@ -197,8 +226,7 @@ To start the service automatically at boot, also execute the following: sudo systemctl enable mbpfan.service -Usage -------- +## Usage Usage: ./mbpfan OPTION(S) @@ -208,13 +236,13 @@ Usage -v Be (a lot) verbose -License ---------------------- +## License + GNU General Public License version 3 -Based On ---------------------- +**This Project Is Based On:** + * http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ * http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ * https://launchpad.net/macfanctld From 14ce4b859f775c69a92fdd521050d6ff6d8c3fe4 Mon Sep 17 00:00:00 2001 From: sasan Date: Mon, 14 Aug 2017 16:12:47 +0430 Subject: [PATCH 151/235] add ".o" to object files --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index ed2390a..ba5de5a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ COMPILER=cc C = c +OBJ = o OUTPUT_PATH = bin/ SOURCE_PATH = src/ BIN = bin/mbpfan From 160d44cd17de5a3edea5b1be98cd5cc712221893 Mon Sep 17 00:00:00 2001 From: sasan Date: Mon, 14 Aug 2017 17:38:29 +0430 Subject: [PATCH 152/235] remove "-v" flag from systemd service file. It spams the syslog every time the daemon is waked up. --- mbpfan.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbpfan.service b/mbpfan.service index f2983ea..a4b61e8 100644 --- a/mbpfan.service +++ b/mbpfan.service @@ -5,7 +5,7 @@ After=sysinit.target [Service] Type=simple -ExecStart=/usr/sbin/mbpfan -fv +ExecStart=/usr/sbin/mbpfan -f ExecReload=/usr/bin/kill -HUP $MAINPID PIDFile=/var/run/mbpfan.pid Restart=always From e45a1d7bf07b6f4895754827b62bdd24a42ff67d Mon Sep 17 00:00:00 2001 From: sasan Date: Mon, 14 Aug 2017 18:02:12 +0430 Subject: [PATCH 153/235] add credits where due. added credit for using Kristensson's settings managment library. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 230e49a..f00bb54 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ This enhanced version assumes any number of processors and fans (max. 10). - [Starting at boot](#starting-at-boot) - [Usage](#usage) - [License](#license) +- [Credits](#credits) ## Supported GNU/Linux Distributions @@ -248,3 +249,9 @@ GNU General Public License version 3 * https://launchpad.net/macfanctld * http://paste2.org/p/862259 * http://www.lobotomo.com/products/FanControl/ + +## Credits + +**This Project uses following library:** + +* [ANSI C Application Settings Managment](http://pokristensson.com/settings.html) by Per Ola Kristensson. From 650471a2735cfd0ed06c4a1a4d79fed3a472feae Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Jul 2017 16:35:39 -0700 Subject: [PATCH 154/235] Clean up string creation via smprintf and strdup This fixes some bugs when using the incorrect strlen in strncat. --- src/mbpfan.c | 70 +++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index ecd4969..28fe43c 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -27,6 +27,7 @@ */ +#include #include #include #include @@ -61,6 +62,29 @@ t_sensors* sensors = NULL; t_fans* fans = NULL; +static char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); +static char *smprintf(const char *fmt, ...) +{ + char *buf; + int cnt; + va_list ap; + + // find buffer length + va_start(ap, fmt); + cnt = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + if (cnt < 0) { + return NULL; + } + + // create and write to buffer + buf = malloc(cnt + 1); + va_start(ap, fmt); + vsnprintf(buf, cnt + 1, fmt, ap); + va_end(ap); + return buf; +} + bool is_legacy_sensors_path() { struct utsname kernel; @@ -147,9 +171,7 @@ t_sensors *retrieve_sensors() if (errno == EISDIR) { - path_begin = (char*) malloc(sizeof( char ) * (strlen(hwmon_path) + strlen("/temp") + 1)); - strcpy(path_begin, hwmon_path); - strcat(path_begin, "/temp"); + path_begin = smprintf("%s/temp", hwmon_path); if(verbose) { printf("Found hwmon path at %s\n", path_begin); @@ -167,28 +189,17 @@ t_sensors *retrieve_sensors() const char *path_end = "_input"; - int path_size = strlen(path_begin) + strlen(path_end) + 2; - char number[2]; - sprintf(number,"%d",0); - int sensors_found = 0; int counter = 0; for(counter = 0; counter<10; counter++) { - path = (char*) malloc(sizeof( char ) * path_size); - - sprintf(number,"%d",counter); - path[0] = '\0'; - strncat( path, path_begin, strlen(path_begin) ); - strncat( path, number, strlen(number) ); - strncat( path, path_end, strlen(path_begin) ); + path = smprintf("%s%d%s", path_begin, counter, path_end); FILE *file = fopen(path, "r"); if(file != NULL) { s = (t_sensors *) malloc( sizeof( t_sensors ) ); - s->path = (char *) malloc(sizeof( char ) * path_size); - strcpy(s->path, path); + s->path = strdup(path); fscanf(file, "%d", &s->temperature); if (sensors_head == NULL) { @@ -248,39 +259,20 @@ t_fans *retrieve_fans() const char *path_output_end = "_output"; const char *path_man_end = "_manual"; - int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2; - int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2; - char number[2]; - sprintf(number,"%d",0); - int counter = 0; int fans_found = 0; for(counter = 0; counter<10; counter++) { - path_output = (char*) malloc(sizeof( char ) * path_min_size); - path_output[0] = '\0'; - path_manual = (char*) malloc(sizeof( char ) * path_man_size); - path_manual[0] = '\0'; - sprintf(number,"%d",counter); - - strncat( path_output, path_begin, strlen(path_begin) ); - strncat( path_output, number, strlen(number) ); - strncat( path_output, path_output_end, strlen(path_begin) ); - - strncat( path_manual, path_begin, strlen(path_begin) ); - strncat( path_manual, number, strlen(number) ); - strncat( path_manual, path_man_end, strlen(path_begin) ); - + path_output = smprintf("%s%d%s", path_begin, counter, path_output_end); + path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end); FILE *file = fopen(path_output, "w"); if(file != NULL) { fan = (t_fans *) malloc( sizeof( t_fans ) ); - fan->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size); - fan->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size); - strcpy(fan->fan_output_path, path_output); - strcpy(fan->fan_manual_path, path_manual); + fan->fan_output_path = strdup(path_output); + fan->fan_manual_path = strdup(path_manual); if (fans_head == NULL) { fans_head = fan; From 33e936300f05250627e852e7a65104d7f9e82a46 Mon Sep 17 00:00:00 2001 From: Peter Evjan Date: Sun, 5 Nov 2017 10:57:49 +1100 Subject: [PATCH 155/235] Removed redundant systemd instructions There were 3 sections giving advice on how to set up systemd. Removed 2 of them. The first one under the Ubuntu header was incomplete, it didn't start the service immediately. The 3rd one was already mentioned in the 2nd section. --- README.md | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/README.md b/README.md index f00bb54..9a26648 100644 --- a/README.md +++ b/README.md @@ -174,13 +174,7 @@ If installed and using the init file, run with (Ubuntu example) **Ubuntu** -For systemd based init systems (Ubuntu 16.04+), the file mbpfan.service -has been provided. -For using it, execute: - - sudo cp mbpfan.service /etc/systemd/system/ - sudo systemctl enable mbpfan.service - +For systemd based init systems (Ubuntu 16.04+), see the systemd section below. For upstart based init systems (Ubuntu before 16.04), an example upstart job has been provided. For using it, execute: @@ -222,11 +216,6 @@ execute the following (as root): sudo systemctl daemon-reload sudo systemctl start mbpfan.service -To start the service automatically at boot, also execute the following: - - sudo systemctl enable mbpfan.service - - ## Usage Usage: ./mbpfan OPTION(S) From ce3218407fbbaacd408ac251ba6f30fadf259ea7 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 15 Aug 2018 13:14:35 -0700 Subject: [PATCH 156/235] Check root privileges before running tests References #155. --- src/main.c | 1 + src/main.h | 1 + src/minunit.c | 3 +++ 3 files changed, 5 insertions(+) create mode 100644 src/main.h diff --git a/src/main.c b/src/main.c index d447cbd..37a852c 100644 --- a/src/main.c +++ b/src/main.c @@ -29,6 +29,7 @@ #include "mbpfan.h" #include "daemon.h" #include "global.h" +#include "main.h" #include "minunit.h" int daemonize = 1; diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..13013e8 --- /dev/null +++ b/src/main.h @@ -0,0 +1 @@ +void check_requirements(); diff --git a/src/minunit.c b/src/minunit.c index 7f6e053..4b8aaca 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -9,6 +9,7 @@ #include "global.h" #include "mbpfan.h" #include "settings.h" +#include "main.h" #include "minunit.h" int tests_run = 0; @@ -193,6 +194,8 @@ static const char *all_tests() int tests() { + check_requirements(); + printf("Starting the tests..\n"); printf("It is normal for them to take a bit to finish.\n"); From e8850f682c1256ea7e50c2367f706d4dfd236e5a Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 4 Oct 2017 07:38:04 -0700 Subject: [PATCH 157/235] Plug little leak from reused variable --- src/mbpfan.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index be40e3e..2be29c8 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -171,6 +171,7 @@ t_sensors *retrieve_sensors() if (errno == EISDIR) { + free(path_begin); path_begin = smprintf("%s/temp", hwmon_path); if(verbose) { From 6aba92d64915fc3ea71ce651133ab16804ca9091 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 12 Jul 2017 18:11:19 -0700 Subject: [PATCH 158/235] Configure timer slack for 1 second This allows the kernel scheduler to coalesce wakeups, improving battery life. Linux defaults to 50 ms slack. Reference: http://lwn.net/Articles/369549/ --- src/daemon.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/daemon.c b/src/daemon.c index 7a94985..a4574e3 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -17,6 +17,7 @@ */ +#include #include #include #include @@ -153,6 +154,11 @@ void go_daemon(void (*fan_control)()) openlog(PROGRAM_NAME, LOG_CONS, LOG_USER); } + // configure timer slack + int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0); + if (err == -1) { + perror("prctl"); + } pid_t pid_slave; pid_t sid_slave; From ca52e9cefdb919740ce34f325e8521ccba549728 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 14 Jul 2017 12:00:29 -0700 Subject: [PATCH 159/235] Call nanosleep instead of sleep in polling loop This avoids unnecessary rt_sigprocmask and rt_sigaction system calls. --- src/mbpfan.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 2be29c8..acbdc08 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -571,6 +572,11 @@ void mbpfan() } } - sleep(polling_interval); + // call nanosleep instead of sleep to avoid rt_sigprocmask and + // rt_sigaction + struct timespec ts; + ts.tv_sec = polling_interval; + ts.tv_nsec = 0; + nanosleep(&ts, NULL); } } From 885a084f5cdfca443a9665787135de394a2cc833 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 12 Jul 2017 17:56:05 -0700 Subject: [PATCH 160/235] Correctly test sensor path for Linux prior to 3.15 Previously the fopen write mode caused the call to spuriously report newer sensors. Now we check the newer sensors path explicitly. Also plug file descriptor leak and more carefully check return values. --- README.md | 1 + src/mbpfan.c | 45 +++++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 9a26648..fb179a6 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) +- MacBook Air 6,1 13" (Intel i7 - Linux 3.13) - MacBook Air 5,2 13" (Intel i5 - Linux 3.16) - MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8) - MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) diff --git a/src/mbpfan.c b/src/mbpfan.c index acbdc08..67958bc 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -100,31 +100,25 @@ bool is_legacy_sensors_path() exit(EXIT_FAILURE); } - - // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen - fopen("/sys/devices/platform/coretemp.0/hwmon", "wb"); + const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon"; + int counter; - if (errno == EISDIR) { - return 0; - } else { - return 1; - } + for (counter = 0; counter < 10; counter++) { + char hwmon_path[strlen(path_begin)+2]; + sprintf(hwmon_path, "%s%d", path_begin, counter); - // - // str_kernel_version = strtok(NULL, "."); - // int kernel_version = atoi(str_kernel_version); - - // if(verbose) { - // printf("Detected kernel version: %s\n", kernel.release); - // printf("Detected kernel minor revision: %s\n", str_kernel_version); + FILE *file = fopen(hwmon_path, "rb"); + int isdir = file == NULL && errno == EISDIR; + if (file != NULL) { + fclose(file); + } - // if(daemonize) { - // syslog(LOG_INFO, "Kernel version: %s", kernel.release); - // syslog(LOG_INFO, "Detected kernel minor revision: %s", str_kernel_version); - // } - // } + if (isdir) { + return 0; + } + } - // return (atoi(kernel.release) == 3 && kernel_version < 15); + return 1; } @@ -167,10 +161,13 @@ t_sensors *retrieve_sensors() sprintf(hwmon_path, "%s%d", path_begin, counter); - // thanks http://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen - fopen(hwmon_path, "wb"); + FILE *file = fopen(hwmon_path, "rb"); + int isdir = file == NULL && errno == EISDIR; + if (file != NULL) { + fclose(file); + } - if (errno == EISDIR) { + if (isdir) { free(path_begin); path_begin = smprintf("%s/temp", hwmon_path); From ddc219746f2f0d3aa40cd4f1d076f81d994112c6 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 20 Aug 2018 15:19:04 -0700 Subject: [PATCH 161/235] Allow overriding CC with environment variable --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 769e0c2..f5766ab 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ DOC = README.md MAN = mbpfan.8.gz COPT = -CC = cc +CC ?= cc OBJFLAG = -o BINFLAG = -o INCLUDES = From 8c89a671e8f9f3e3539a26d4b4a851a832f6c17e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 20 Aug 2018 15:19:26 -0700 Subject: [PATCH 162/235] Address GCC 7 warnings --- src/daemon.c | 3 +++ src/mbpfan.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/daemon.c b/src/daemon.c index a4574e3..bd4a491 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -118,14 +118,17 @@ void signal_handler(int signal) case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); cleanup_and_exit(EXIT_SUCCESS); + break; case SIGQUIT: syslog(LOG_WARNING, "Received SIGQUIT signal."); cleanup_and_exit(EXIT_SUCCESS); + break; case SIGINT: syslog(LOG_WARNING, "Received SIGINT signal."); cleanup_and_exit(EXIT_SUCCESS); + break; default: syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); diff --git a/src/mbpfan.c b/src/mbpfan.c index 67958bc..4e20529 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -306,7 +306,7 @@ t_fans *retrieve_fans() } } - if (!fans_found > 0){ + if (fans_found == 0){ syslog(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.\n"); printf("mbpfan could not detect any fan. Please contact the developer.\n"); exit(EXIT_FAILURE); From ac099c8bd27d052e12aca53986a7e92e8dc3a241 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 20 Aug 2018 15:27:48 -0700 Subject: [PATCH 163/235] Report error when failing to set fan speed References #155. --- src/mbpfan.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 4e20529..8a4ac1e 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -376,7 +376,10 @@ void set_fan_speed(t_fans* fans, int speed) if(tmp->file != NULL) { char buf[16]; int len = snprintf(buf, sizeof(buf), "%d", speed); - pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0); + int res = pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0); + if (res == -1) { + perror("Could not set fan speed"); + } } tmp = tmp->next; From e58a67f630c2d22370381aabbac3fe47da6b87d1 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 14 Jul 2017 02:30:59 -0700 Subject: [PATCH 164/235] Avoid setting speed when already set to that speed --- src/global.h | 1 + src/mbpfan.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index 19db7c1..9f73014 100644 --- a/src/global.h +++ b/src/global.h @@ -19,6 +19,7 @@ struct s_fans { char* path; // TODO: unused char* fan_output_path; char* fan_manual_path; + int old_speed; struct s_fans *next; }; diff --git a/src/mbpfan.c b/src/mbpfan.c index 8a4ac1e..1a7d385 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -272,6 +272,7 @@ t_fans *retrieve_fans() fan = (t_fans *) malloc( sizeof( t_fans ) ); fan->fan_output_path = strdup(path_output); fan->fan_manual_path = strdup(path_manual); + fan->old_speed = 0; if (fans_head == NULL) { fans_head = fan; @@ -373,13 +374,14 @@ void set_fan_speed(t_fans* fans, int speed) t_fans *tmp = fans; while(tmp != NULL) { - if(tmp->file != NULL) { + if(tmp->file != NULL && tmp->old_speed != speed) { char buf[16]; int len = snprintf(buf, sizeof(buf), "%d", speed); int res = pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0); if (res == -1) { perror("Could not set fan speed"); } + tmp->old_speed = speed; } tmp = tmp->next; From 084b5c3cfae7e6cb3cf9f3063a0a28d8fc62122c Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 20 Aug 2018 22:05:12 -0700 Subject: [PATCH 165/235] Improve capitalization, spelling, and whitespace --- README.md | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index fb179a6..145d398 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,18 @@ This is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/0 mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). -* It only uses the temperatures from the processors as input. -* It requires coretemp and applesmc kernel modules to be loaded. -* It requires root use -* It daemonizes or stays in foreground -* Verbose mode for both syslog and stdout -* Users can configure it using the file /etc/mbpfan.conf +* It only uses the temperatures from the processors as input. +* It requires coretemp and applesmc kernel modules to be loaded. +* It requires root use +* It daemonizes or stays in foreground +* Verbose mode for both syslog and stdout +* Users can configure it using the file /etc/mbpfan.conf **Table Of Contents** - [Supported GNU/Linux Distributions](#supported-gnulinux-distributions) -- [Tested Macbook Models](#tested-macbook-models) -- [Tested iMac/Mac Mini Models](#tested-imacmac-mini-models) +- [Tested MacBook Models](#tested-macbook-models) +- [Tested iMac/Mac mini Models](#tested-imacmac-mini-models) - [Requirements](#requirements) - [Installation](#installation) - [Arch Linux](#arch-linux) @@ -48,7 +48,7 @@ Supported distributions: - Trisquel - Solus -## Tested Macbook Models +## Tested MacBook Models This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. @@ -68,16 +68,16 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) - MacBook 1,1 (Intel Core Duo - Linux 3.16) -## Tested iMac/Mac Mini Models +## Tested iMac/Mac mini Models -This section reports the iMac/Mac-mini models where mbpfan was tested successfully. +This section reports the iMac/Mac mini models where mbpfan was tested successfully. -- iMac Retina 16.2 21.5" (Intel i5 - Linux 4.4.0 Ubuntu 16.04) +- iMac Retina 16.2 21.5" (Intel i5 - Linux 4.4.0 Ubuntu 16.04) - iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) - iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) -- Mac Mini 2,1 (Core 2 Duo - Linux 4.4.0) -- Mac Mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) -- Mac Mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) +- Mac mini 2,1 (Core 2 Duo - Linux 4.4.0) +- Mac mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) +- Mac mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) ## Requirements @@ -89,7 +89,9 @@ These modules are often automatically loaded when booting up GNU/Linux on a MacB In most distributions, you can run the following command: -`lsmod | grep coretemp && lsmod | grep applesmc` +```bash +lsmod | grep -e applesmc -e coretemp +``` If you see `coretemp` and `applesmc` listed, you are all set. @@ -244,4 +246,4 @@ GNU General Public License version 3 **This Project uses following library:** -* [ANSI C Application Settings Managment](http://pokristensson.com/settings.html) by Per Ola Kristensson. +* [ANSI C Application Settings Management](http://pokristensson.com/settings.html) by Per Ola Kristensson. From d717ffb220a96a56359433c6db41a01180e21102 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Tue, 21 Aug 2018 02:25:59 -0700 Subject: [PATCH 166/235] Add gaul to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index ae03d37..89fd00e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,6 +5,7 @@ Daniel Graziotin Marji Cermak John Ferlito +Andrew Gaul Ismail Khatib Trevor Joynson Magnus Stubman From 4a3ea930ad93c089e3da2441d762e3b24600b9cc Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 23 Aug 2018 19:39:02 -0700 Subject: [PATCH 167/235] Correctly detect legacy or modern sensors Tighten check by looking for an actual sensor instead of a directory. Fixes #162. Fixes regression from 885a084f5cdfca443a9665787135de394a2cc833. --- src/mbpfan.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 1a7d385..5662087 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -86,7 +86,7 @@ static char *smprintf(const char *fmt, ...) return buf; } -bool is_legacy_sensors_path() +bool is_modern_sensors_path() { struct utsname kernel; uname(&kernel); @@ -100,25 +100,21 @@ bool is_legacy_sensors_path() exit(EXIT_FAILURE); } - const char *path_begin = "/sys/devices/platform/coretemp.0/hwmon/hwmon"; int counter; for (counter = 0; counter < 10; counter++) { - char hwmon_path[strlen(path_begin)+2]; - sprintf(hwmon_path, "%s%d", path_begin, counter); - - FILE *file = fopen(hwmon_path, "rb"); - int isdir = file == NULL && errno == EISDIR; - if (file != NULL) { - fclose(file); - } - - if (isdir) { - return 0; + int temp; + for (temp = 1; temp < 10; ++temp) { + char *path = smprintf("/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input", counter, temp); + int res = access(path, R_OK); + free(path); + if (res == 0) { + return 1; + } } } - return 1; + return 0; } @@ -131,7 +127,7 @@ t_sensors *retrieve_sensors() char *path = NULL; char *path_begin = NULL; - if (is_legacy_sensors_path()) { + if (!is_modern_sensors_path()) { if(verbose) { printf("Using legacy sensor path for kernel < 3.15.0\n"); @@ -161,13 +157,8 @@ t_sensors *retrieve_sensors() sprintf(hwmon_path, "%s%d", path_begin, counter); - FILE *file = fopen(hwmon_path, "rb"); - int isdir = file == NULL && errno == EISDIR; - if (file != NULL) { - fclose(file); - } - - if (isdir) { + int res = access(hwmon_path, R_OK); + if (res == 0) { free(path_begin); path_begin = smprintf("%s/temp", hwmon_path); From 810adc671b386d9669245550ff8c521eb6bc48f6 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 21 Jul 2017 08:43:21 -0700 Subject: [PATCH 168/235] Detect minimum and maximum fan speeds Default to previous values if detection fails. Configuration can still override these values. Fixes #114. --- src/main.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/mbpfan.c | 19 +++++++++++++++---- src/mbpfan.h | 2 ++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 37a852c..5b92af4 100644 --- a/src/main.c +++ b/src/main.c @@ -98,6 +98,48 @@ void check_requirements() } +static int read_value(const char *path) +{ + int value = -1; + FILE *file = fopen(path, "r"); + if (file != NULL) { + fscanf(file, "%d", &value); + fclose(file); + } + return value; +} + + +static void set_defaults(void) +{ + int i; + char *path; + int value; + for (i = 1; i <= 10; ++i) { + path = smprintf("%s/fan%d_min", APPLESMC_PATH, i); + value = read_value(path); + if (value != -1 && (min_fan_speed == -1 || value < min_fan_speed)) { + min_fan_speed = value; + } + free(path); + + path = smprintf("%s/fan%d_max", APPLESMC_PATH, i); + value = read_value(path); + if (value != -1 && (max_fan_speed == -1 || value > max_fan_speed)) { + max_fan_speed = value; + } + free(path); + } + + if (min_fan_speed == -1) { + min_fan_speed = 2000; + } + if (max_fan_speed == -1) { + max_fan_speed = 6200; + } +} + + int main(int argc, char *argv[]) { @@ -133,6 +175,7 @@ int main(int argc, char *argv[]) check_requirements(); + set_defaults(); // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; diff --git a/src/mbpfan.c b/src/mbpfan.c index 5662087..c0bf297 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -46,8 +46,9 @@ #define min(a,b) ((a) < (b) ? (a) : (b)) #define max(a,b) ((a) > (b) ? (a) : (b)) -int min_fan_speed = 2000; -int max_fan_speed = 6200; +// TODO: per-fan minimum and maximum? +int min_fan_speed = -1; +int max_fan_speed = -1; /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum @@ -63,8 +64,7 @@ t_sensors* sensors = NULL; t_fans* fans = NULL; -static char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); -static char *smprintf(const char *fmt, ...) +char *smprintf(const char *fmt, ...) { char *buf; int cnt; @@ -498,6 +498,17 @@ void mbpfan() retrieve_settings(NULL); + if (min_fan_speed > max_fan_speed) { + syslog(LOG_INFO, "Invalid fan speeds: %d %d", min_fan_speed, max_fan_speed); + printf("Invalid fan speeds: %d %d\n", min_fan_speed, max_fan_speed); + exit(EXIT_FAILURE); + } + if (low_temp > high_temp || high_temp > max_temp) { + syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp); + printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_temp); + exit(EXIT_FAILURE); + } + sensors = retrieve_sensors(); fans = retrieve_fans(); set_fans_man(fans); diff --git a/src/mbpfan.h b/src/mbpfan.h index 31f04a4..33fb21a 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -43,6 +43,8 @@ typedef struct s_sensors t_sensors; struct s_fans; typedef struct s_fans t_fans; +char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); + /** * Return true if the kernel is < 3.15.0 */ From 87f3fc2c166c87d0cc873ca064cb2e0f5d22eb0b Mon Sep 17 00:00:00 2001 From: faithful Date: Sun, 19 Aug 2018 15:14:03 +0200 Subject: [PATCH 169/235] First spec attempt --- mbpfan.spec | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 mbpfan.spec diff --git a/mbpfan.spec b/mbpfan.spec new file mode 100644 index 0000000..3c62719 --- /dev/null +++ b/mbpfan.spec @@ -0,0 +1,68 @@ +Name: mbpfan +URL: https://github.com/dgraziotin/mbpfan +License: GPLv3 +Group: System Environment/Daemons +Version: %{SOURCE_VERSION} +Release: 1 +Summary: A simple daemon to control fan speed on all MacBook/MacBook Pros (probably all Apple computers) for Linux 3.x.x and 4.x.x +Source: v%{version}.tar.gz + +%description +This is an enhanced version of Allan McRae mbpfan + +mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. This enhanced version assumes any number of processors and fans (max. 10). + +It only uses the temperatures from the processors as input. +It requires coretemp and applesmc kernel modules to be loaded. +It requires root use +It daemonizes or stays in foreground +Verbose mode for both syslog and stdout +Users can configure it using the file /etc/mbpfan.conf + +%prep +%setup -q -n %{name}-%{version} + +%build +make + +%install +install -D -m755 bin/mbpfan $RPM_BUILD_ROOT/usr/sbin/mbpfan +install -D -m644 mbpfan.conf $RPM_BUILD_ROOT/etc/mbpfan.conf +install -D -m644 mbpfan.service $RPM_BUILD_ROOT/usr/lib/systemd/system/mbpfan.service + +%clean +rm -rf $RPM_BUILD_ROOT + +%post +%systemd_post mbpfan.service +# If it is a first installation then autoconfigure daemon +if [[ ${1} == 1 ]]; then + min_fan_speed="$(cat /sys/devices/platform/applesmc.768/fan*_min| uniq| sort |head -1)" + max_fan_speed="$(cat /sys/devices/platform/applesmc.768/fan*_max| uniq| sort |tail -1)" + let max_temp=$(cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max|uniq |sort |tail -1)/1000 + sed -i "s/min_fan_speed = [^\t]\+/min_fan_speed = $min_fan_speed/g" /etc/mbpfan.conf + sed -i "s/max_fan_speed = [^\t]\+/max_fan_speed = $max_fan_speed/g" /etc/mbpfan.conf + sed -i "s/max_temp = [^\t]\+/max_temp = $max_temp/g" /etc/mbpfan.conf +fi +echo "Attention: mbpfan preconfigured with sane values, to run it type:" +echo "systemctl start mbpfan" +echo "To run also at boot, type:" +echo "systemctl enable mbpfan" + +%preun +%systemd_preun mbpfan.service + +%postun +%systemd_postun_with_restart mbpfan.service + +%files +%defattr (-,root,root) +%doc AUTHORS README.md +/usr/sbin/mbpfan +%config /etc/mbpfan.conf +/usr/lib/systemd/system/mbpfan.service + +%changelog +* Sun Aug 19 2018 Michele Codutti - 2.0.2-2 +- Autoconfig with suggested procedure. +- Initial packaging From 8db63a40af7a616f590feca36042bb6ec327d1db Mon Sep 17 00:00:00 2001 From: codutti Date: Mon, 10 Sep 2018 19:36:26 +0200 Subject: [PATCH 170/235] Removed autoconfig with suggested procedure because has been integrated on mbpfan. Commented auto detect values from config file. --- mbpfan.conf | 4 ++-- mbpfan.spec | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/mbpfan.conf b/mbpfan.conf index 1c1c01d..f6aac6f 100644 --- a/mbpfan.conf +++ b/mbpfan.conf @@ -1,7 +1,7 @@ [general] # see https://ineed.coffee/3838/a-beginners-tutorial-for-mbpfan-under-ubuntu for the values -min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" -max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" +#min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" +#max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 diff --git a/mbpfan.spec b/mbpfan.spec index 3c62719..8d1f550 100644 --- a/mbpfan.spec +++ b/mbpfan.spec @@ -3,7 +3,7 @@ URL: https://github.com/dgraziotin/mbpfan License: GPLv3 Group: System Environment/Daemons Version: %{SOURCE_VERSION} -Release: 1 +Release: 3 Summary: A simple daemon to control fan speed on all MacBook/MacBook Pros (probably all Apple computers) for Linux 3.x.x and 4.x.x Source: v%{version}.tar.gz @@ -35,16 +35,10 @@ rm -rf $RPM_BUILD_ROOT %post %systemd_post mbpfan.service -# If it is a first installation then autoconfigure daemon -if [[ ${1} == 1 ]]; then - min_fan_speed="$(cat /sys/devices/platform/applesmc.768/fan*_min| uniq| sort |head -1)" - max_fan_speed="$(cat /sys/devices/platform/applesmc.768/fan*_max| uniq| sort |tail -1)" - let max_temp=$(cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max|uniq |sort |tail -1)/1000 - sed -i "s/min_fan_speed = [^\t]\+/min_fan_speed = $min_fan_speed/g" /etc/mbpfan.conf - sed -i "s/max_fan_speed = [^\t]\+/max_fan_speed = $max_fan_speed/g" /etc/mbpfan.conf - sed -i "s/max_temp = [^\t]\+/max_temp = $max_temp/g" /etc/mbpfan.conf -fi -echo "Attention: mbpfan preconfigured with sane values, to run it type:" +echo "mbpfan will auto detect sane values for min and max fan speeds." +echo "If you want to customize these values please edit:" +echo "/etc/mbpfan.conf" +echo "To start the daemon now type:" echo "systemctl start mbpfan" echo "To run also at boot, type:" echo "systemctl enable mbpfan" @@ -63,6 +57,9 @@ echo "systemctl enable mbpfan" /usr/lib/systemd/system/mbpfan.service %changelog +* Mon Sep 10 2018 Michele Codutti - 2.0.2-3 +- Removed autoconfig with suggested procedure because has been integrated on mbpfan. + * Sun Aug 19 2018 Michele Codutti - 2.0.2-2 - Autoconfig with suggested procedure. - Initial packaging From b0cdfd4c276476d41f9fe2f6326fbcd4e9fa08bc Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Sep 2018 10:06:52 -0700 Subject: [PATCH 171/235] Propagate exit code from unit tests Also flag errors more clearly. References #164. --- src/main.c | 4 +--- src/minunit.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 5b92af4..4413e89 100644 --- a/src/main.c +++ b/src/main.c @@ -157,9 +157,7 @@ int main(int argc, char *argv[]) break; case 't': - tests(); - exit(EXIT_SUCCESS); - break; + return tests(); case 'v': verbose = 1; diff --git a/src/minunit.c b/src/minunit.c index 4b8aaca..8e88eea 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -202,7 +202,7 @@ int tests() const char *result = all_tests(); if (result != 0) { - printf("%s \n", result); + printf("Error: %s \n", result); } else { printf("ALL TESTS PASSED\n"); From 1716d9d877a08e0bce0b9a5cf7222ff3a4829e66 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 13 Sep 2018 10:29:48 -0700 Subject: [PATCH 172/235] Update unit tests for fan speed auto-detection Fixes #164. References #114. --- mbpfan.conf.test0 | 8 ++++++++ src/main.c | 2 +- src/main.h | 1 + src/minunit.c | 12 ++++++------ 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 mbpfan.conf.test0 diff --git a/mbpfan.conf.test0 b/mbpfan.conf.test0 new file mode 100644 index 0000000..1c1c01d --- /dev/null +++ b/mbpfan.conf.test0 @@ -0,0 +1,8 @@ +[general] +# see https://ineed.coffee/3838/a-beginners-tutorial-for-mbpfan-under-ubuntu for the values +min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" +max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" +low_temp = 63 # try ranges 55-63, default is 63 +high_temp = 66 # try ranges 58-66, default is 66 +max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 +polling_interval = 7 # default is 7 seconds diff --git a/src/main.c b/src/main.c index 4413e89..75db7b9 100644 --- a/src/main.c +++ b/src/main.c @@ -110,7 +110,7 @@ static int read_value(const char *path) } -static void set_defaults(void) +void set_defaults(void) { int i; char *path; diff --git a/src/main.h b/src/main.h index 13013e8..31474f2 100644 --- a/src/main.h +++ b/src/main.h @@ -1 +1,2 @@ void check_requirements(); +void set_defaults(); diff --git a/src/minunit.c b/src/minunit.c index 8e88eea..fc19871 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -117,8 +117,6 @@ static const char *test_config_file() return 0; } - mu_assert("Could not read min_fan_speed from config file",settings_get_int(settings, "general", "min_fan_speed") != 0); - mu_assert("Could not read max_fan_speed from config file",settings_get_int(settings, "general", "max_fan_speed") != 0); mu_assert("Could not read low_temp from config file",settings_get_int(settings, "general", "low_temp") != 0); mu_assert("Could not read high_temp from config file",settings_get_int(settings, "general", "high_temp") != 0); mu_assert("Could not read max_temp from config file",settings_get_int(settings, "general", "max_temp") != 0); @@ -131,10 +129,12 @@ static const char *test_config_file() static const char *test_settings() { + set_defaults(); retrieve_settings("./mbpfan.conf.test1"); mu_assert("max_fan_speed value is not 6200", max_fan_speed == 6200); mu_assert("polling_interval is not 1", polling_interval == 1); - retrieve_settings("./mbpfan.conf"); + set_defaults(); + retrieve_settings("./mbpfan.conf.test0"); mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000); mu_assert("polling_interval is not 7", polling_interval == 7); return 0; @@ -170,11 +170,11 @@ static const char *test_settings_reload() signal(SIGHUP, handler); retrieve_settings("./mbpfan.conf"); printf("Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); - mu_assert("min_fan_speed value is not 2000 before SIGHUP", min_fan_speed == 2000); - mu_assert("polling_interval is not 7 before SIHUP", polling_interval == 7); + // cannot tests min_fan_speed since it is not set and thus auto-detected + mu_assert("polling_interval is not 7 before SIGHUP", polling_interval == 7); raise(SIGHUP); mu_assert("min_fan_speed value is not 6200 after SIGHUP", min_fan_speed == 6200); - mu_assert("polling_interval is not 1 after SIHUP", polling_interval == 1); + mu_assert("polling_interval is not 1 after SIGHUP", polling_interval == 1); retrieve_settings("./mbpfan.conf"); return 0; } From 968133eb103ce543e2fab6c55e306be023509452 Mon Sep 17 00:00:00 2001 From: Ati Sharma Date: Wed, 19 Sep 2018 21:23:21 +0100 Subject: [PATCH 173/235] Add support for many processors (not just cores) * Add support for many processors (not just cores) Add an outer loop in mbpfan.c:retrieve_sensors to loop over all coretemp directories. Also increase hard-coded limits from 10 to 12 or 16 since the temp endpoints are sometimes indexed that high. For example, this is required for supporting a Mac Pro with two processors of 16 cores, requiring /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp{1..12} /sys/devices/platform/coretemp.1/hwmon/hwmon1/temp{1..12} This solves issue #152 "Mac pro with two processors". --- src/mbpfan.c | 112 ++++++++++++++++++++++++++++----------------------- src/mbpfan.h | 3 +- 2 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index c0bf297..27c63e8 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -5,6 +5,7 @@ * Modifications (2012) by Ismail Khatib * Modifications (2012-present) by Daniel Graziotin [CURRENT MAINTAINER] * Modifications (2017-present) by Robert Musial + * Modifications (2018-present) by Ati Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,7 +19,8 @@ * * * Notes: - * Assumes any number of processors and fans (max. 10) + * Assumes any number of processors, cores and fans (max. 6, 16, 12 + * as defined in NUM_PROCESSORS, NUM_HWMONS, NUM_TEMP_INPUTS and NUM_FANS) * It uses only the temperatures from the processors as input. * Requires coretemp and applesmc kernel modules to be loaded. * Requires root use @@ -58,6 +60,12 @@ int low_temp = 63; // try ranges 55-63 int high_temp = 66; // try ranges 58-66 int max_temp = 86; // do not set it > 90 +// maximum number of processors etc supported +#define NUM_PROCESSORS 6 +#define NUM_HWMONS 12 +#define NUM_TEMP_INPUTS 16 +#define NUM_FANS 10 + int polling_interval = 7; t_sensors* sensors = NULL; @@ -102,9 +110,9 @@ bool is_modern_sensors_path() int counter; - for (counter = 0; counter < 10; counter++) { + for (counter = 0; counter < NUM_HWMONS; counter++) { int temp; - for (temp = 1; temp < 10; ++temp) { + for (temp = 1; temp < NUM_TEMP_INPUTS; ++temp) { char *path = smprintf("/sys/devices/platform/coretemp.0/hwmon/hwmon%d/temp%d_input", counter, temp); int res = access(path, R_OK); free(path); @@ -127,6 +135,9 @@ t_sensors *retrieve_sensors() char *path = NULL; char *path_begin = NULL; + const char *path_end = "_input"; + int sensors_found = 0; + if (!is_modern_sensors_path()) { if(verbose) { printf("Using legacy sensor path for kernel < 3.15.0\n"); @@ -148,71 +159,72 @@ t_sensors *retrieve_sensors() } } - path_begin = strdup("/sys/devices/platform/coretemp.0/hwmon/hwmon"); + // loop over up to 6 processors + int processor; + for (processor = 0; processor < NUM_PROCESSORS; processor++) { - int counter; - for (counter = 0; counter < 10; counter++) { + path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); - char hwmon_path[strlen(path_begin)+2]; + int counter; + for (counter = 0; counter < NUM_HWMONS; counter++) { - sprintf(hwmon_path, "%s%d", path_begin, counter); + char hwmon_path[strlen(path_begin)+2]; - int res = access(hwmon_path, R_OK); - if (res == 0) { + sprintf(hwmon_path, "%s%d", path_begin, counter); - free(path_begin); - path_begin = smprintf("%s/temp", hwmon_path); + int res = access(hwmon_path, R_OK); + if (res == 0) { - if(verbose) { - printf("Found hwmon path at %s\n", path_begin); + free(path_begin); + path_begin = smprintf("%s/temp", hwmon_path); - if(daemonize) { - syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin); - } + if(verbose) { + printf("Found hwmon path at %s\n", path_begin); - } + if(daemonize) { + syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin); + } - break; - } - } - } + } - const char *path_end = "_input"; + break; + } + } - int sensors_found = 0; + int core = 0; + for(core = 0; corepath = strdup(path); + fscanf(file, "%d", &s->temperature); - if(file != NULL) { - s = (t_sensors *) malloc( sizeof( t_sensors ) ); - s->path = strdup(path); - fscanf(file, "%d", &s->temperature); + if (sensors_head == NULL) { + sensors_head = s; + sensors_head->next = NULL; - if (sensors_head == NULL) { - sensors_head = s; - sensors_head->next = NULL; + } else { + t_sensors *tmp = sensors_head; - } else { - t_sensors *tmp = sensors_head; - - while (tmp->next != NULL) { - tmp = tmp->next; - } + while (tmp->next != NULL) { + tmp = tmp->next; + } - tmp->next = s; - tmp->next->next = NULL; - } + tmp->next = s; + tmp->next->next = NULL; + } - s->file = file; - sensors_found++; - } + s->file = file; + sensors_found++; + } - free(path); - path = NULL; + free(path); + path = NULL; + } + } } if(verbose) { @@ -252,7 +264,7 @@ t_fans *retrieve_fans() int counter = 0; int fans_found = 0; - for(counter = 0; counter<10; counter++) { + for(counter = 0; counter Date: Tue, 15 Jan 2019 15:44:54 -0800 Subject: [PATCH 174/235] Plug little leak Found via Valgrind. --- src/mbpfan.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index 27c63e8..4832099 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -163,6 +163,9 @@ t_sensors *retrieve_sensors() int processor; for (processor = 0; processor < NUM_PROCESSORS; processor++) { + if (path_begin != NULL) { + free(path_begin); + } path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); int counter; From 50f3a67ab4c2d6fad6db1648401e65a606483652 Mon Sep 17 00:00:00 2001 From: mikaelbjoern <46425169+mikaelbjoern@users.noreply.github.com> Date: Fri, 18 Jan 2019 20:26:11 +0100 Subject: [PATCH 175/235] Add support for many processors (not just cores) Add an outer loop in mbpfan.c:retrieve_sensors to loop over all coretemp directories. Also increase hard-coded limits from 10 to 12 or 16 since the temp endpoints are sometimes indexed that high. For example, this is required for supporting a Mac Pro with two processors of 16 cores, requiring /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp{1..12} /sys/devices/platform/coretemp.1/hwmon/hwmon1/temp{1..12} Fixes #152. --- mbpfan.conf | 14 ++- mbpfan.conf.test0 | 4 +- mbpfan.conf.test1 | 4 +- mbpfan.conf.test2 | 7 ++ src/daemon.c | 2 +- src/global.h | 7 +- src/main.c | 47 ---------- src/main.h | 1 - src/mbpfan.c | 215 ++++++++++++++++++++++++++++------------------ src/mbpfan.h | 15 ++-- src/minunit.c | 52 ++++++++--- 11 files changed, 209 insertions(+), 159 deletions(-) create mode 100644 mbpfan.conf.test2 diff --git a/mbpfan.conf b/mbpfan.conf index f6aac6f..e65129b 100644 --- a/mbpfan.conf +++ b/mbpfan.conf @@ -1,7 +1,17 @@ [general] # see https://ineed.coffee/3838/a-beginners-tutorial-for-mbpfan-under-ubuntu for the values -#min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" -#max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" +# +# mbpfan will load the max / min speed of from the files produced by the applesmc driver. If these files are not found it will set all fans to the default of min_speed = 2000 and max_speed = 6200 +# by setting the values for the speeds in this config it will override whatever it finds in: +# /sys/devices/platform/applesmc.768/fan*_min +# /sys/devices/platform/applesmc.768/fan*_max +# or the defaults. +# +# multiple fans can be configured by using the config key of min_fan*_speed and max_fan*_speed +# the number used will correlate to the file number of the fan in the applesmc driver that are used to control the fan speed. +# +#min_fan1_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" +#max_fan1_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 diff --git a/mbpfan.conf.test0 b/mbpfan.conf.test0 index 1c1c01d..13bccf6 100644 --- a/mbpfan.conf.test0 +++ b/mbpfan.conf.test0 @@ -1,7 +1,7 @@ [general] # see https://ineed.coffee/3838/a-beginners-tutorial-for-mbpfan-under-ubuntu for the values -min_fan_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" -max_fan_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" +min_fan1_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" +max_fan1_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 diff --git a/mbpfan.conf.test1 b/mbpfan.conf.test1 index e17bdbd..8a18c95 100644 --- a/mbpfan.conf.test1 +++ b/mbpfan.conf.test1 @@ -1,6 +1,6 @@ [general] -min_fan_speed = 6200 # default is 2000 -max_fan_speed = 6200 # default is 6200 +min_fan1_speed = 6200 # default is 2000 +max_fan1_speed = 6200 # default is 6200 low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # do not set it > 90, default is 86 diff --git a/mbpfan.conf.test2 b/mbpfan.conf.test2 new file mode 100644 index 0000000..9451ef8 --- /dev/null +++ b/mbpfan.conf.test2 @@ -0,0 +1,7 @@ +[general] +min_fan1_speed = 2000 # default is 2000 +min_fan2_speed = 2000 # default is 6200 +low_temp = 63 # try ranges 55-63, default is 63 +high_temp = 66 # try ranges 58-66, default is 66 +max_temp = 86 # do not set it > 90, default is 86 +polling_interval = 1 # default is 7 diff --git a/src/daemon.c b/src/daemon.c index bd4a491..42fc75f 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -112,7 +112,7 @@ void signal_handler(int signal) switch(signal) { case SIGHUP: syslog(LOG_WARNING, "Received SIGHUP signal."); - retrieve_settings(NULL); + retrieve_settings(NULL, fans); break; case SIGTERM: diff --git a/src/global.h b/src/global.h index 9f73014..46e5fc8 100644 --- a/src/global.h +++ b/src/global.h @@ -19,7 +19,12 @@ struct s_fans { char* path; // TODO: unused char* fan_output_path; char* fan_manual_path; + int step_up; + int step_down; + int fan_id; int old_speed; + int fan_max_speed; + int fan_min_speed; struct s_fans *next; }; @@ -29,4 +34,4 @@ typedef struct s_fans t_fans; extern t_sensors* sensors; extern t_fans* fans; -#endif \ No newline at end of file +#endif diff --git a/src/main.c b/src/main.c index 75db7b9..2c4286b 100644 --- a/src/main.c +++ b/src/main.c @@ -54,7 +54,6 @@ void print_usage(int argc, char *argv[]) } } - void check_requirements() { @@ -97,49 +96,6 @@ void check_requirements() } - -static int read_value(const char *path) -{ - int value = -1; - FILE *file = fopen(path, "r"); - if (file != NULL) { - fscanf(file, "%d", &value); - fclose(file); - } - return value; -} - - -void set_defaults(void) -{ - int i; - char *path; - int value; - for (i = 1; i <= 10; ++i) { - path = smprintf("%s/fan%d_min", APPLESMC_PATH, i); - value = read_value(path); - if (value != -1 && (min_fan_speed == -1 || value < min_fan_speed)) { - min_fan_speed = value; - } - free(path); - - path = smprintf("%s/fan%d_max", APPLESMC_PATH, i); - value = read_value(path); - if (value != -1 && (max_fan_speed == -1 || value > max_fan_speed)) { - max_fan_speed = value; - } - free(path); - } - - if (min_fan_speed == -1) { - min_fan_speed = 2000; - } - if (max_fan_speed == -1) { - max_fan_speed = 6200; - } -} - - int main(int argc, char *argv[]) { @@ -170,10 +126,7 @@ int main(int argc, char *argv[]) } } - - check_requirements(); - set_defaults(); // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; diff --git a/src/main.h b/src/main.h index 31474f2..13013e8 100644 --- a/src/main.h +++ b/src/main.h @@ -1,2 +1 @@ void check_requirements(); -void set_defaults(); diff --git a/src/mbpfan.c b/src/mbpfan.c index 4832099..f183229 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -28,7 +28,6 @@ * Tested models: see README.md */ - #include #include #include @@ -48,10 +47,6 @@ #define min(a,b) ((a) < (b) ? (a) : (b)) #define max(a,b) ((a) > (b) ? (a) : (b)) -// TODO: per-fan minimum and maximum? -int min_fan_speed = -1; -int max_fan_speed = -1; - /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature @@ -65,13 +60,14 @@ int max_temp = 86; // do not set it > 90 #define NUM_HWMONS 12 #define NUM_TEMP_INPUTS 16 #define NUM_FANS 10 +#define NUM_MIN_FAN_SPEED_DEFAULT 2000 +#define NUM_MAX_FAN_SPEED_DEFAULT 6200 int polling_interval = 7; t_sensors* sensors = NULL; t_fans* fans = NULL; - char *smprintf(const char *fmt, ...) { char *buf; @@ -128,7 +124,6 @@ bool is_modern_sensors_path() t_sensors *retrieve_sensors() { - t_sensors *sensors_head = NULL; t_sensors *s = NULL; @@ -250,20 +245,33 @@ t_sensors *retrieve_sensors() return sensors_head; } +static int read_value(const char *path) +{ + int value = -1; + FILE *file = fopen(path, "r"); + if (file != NULL) { + fscanf(file, "%d", &value); + fclose(file); + } + return value; +} t_fans *retrieve_fans() { - t_fans *fans_head = NULL; t_fans *fan = NULL; char *path_output = NULL; char *path_manual = NULL; + char *path_fan_max = NULL; + char *path_fan_min = NULL; const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; const char *path_output_end = "_output"; const char *path_man_end = "_manual"; - + const char *path_max_speed = "_max"; + const char *path_min_speed = "_min"; + int counter = 0; int fans_found = 0; @@ -271,6 +279,8 @@ t_fans *retrieve_fans() path_output = smprintf("%s%d%s", path_begin, counter, path_output_end); path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end); + path_fan_min = smprintf("%s%d%s",path_begin, counter, path_min_speed); + path_fan_max = smprintf("%s%d%s",path_begin, counter, path_max_speed); FILE *file = fopen(path_output, "w"); @@ -278,6 +288,20 @@ t_fans *retrieve_fans() fan = (t_fans *) malloc( sizeof( t_fans ) ); fan->fan_output_path = strdup(path_output); fan->fan_manual_path = strdup(path_manual); + fan->fan_id = counter; + + int fan_speed = read_value(path_fan_min); + if(fan_speed == -1 || fan_speed < NUM_MIN_FAN_SPEED_DEFAULT) + fan->fan_min_speed = NUM_MIN_FAN_SPEED_DEFAULT; + else + fan->fan_min_speed = fan_speed; + + fan_speed = read_value(path_fan_max); + if(fan_speed == -1 || fan_speed > NUM_MAX_FAN_SPEED_DEFAULT) + fan->fan_max_speed = NUM_MAX_FAN_SPEED_DEFAULT; + else + fan->fan_max_speed = fan_speed; + fan->old_speed = 0; if (fans_head == NULL) { @@ -298,7 +322,10 @@ t_fans *retrieve_fans() fan->file = file; fans_found++; } - + free(path_fan_min); + path_fan_min = NULL; + free(path_fan_max); + path_fan_max = NULL; free(path_output); path_output = NULL; free(path_manual); @@ -319,14 +346,11 @@ t_fans *retrieve_fans() exit(EXIT_FAILURE); } - return fans_head; } - static void set_fans_mode(t_fans *fans, int mode) { - t_fans *tmp = fans; FILE *file; @@ -356,7 +380,6 @@ void set_fans_auto(t_fans *fans) t_sensors *refresh_sensors(t_sensors *sensors) { - t_sensors *tmp = sensors; while(tmp != NULL) { @@ -373,29 +396,29 @@ t_sensors *refresh_sensors(t_sensors *sensors) return sensors; } - -/* Controls the speed of the fan */ -void set_fan_speed(t_fans* fans, int speed) +/* Controls the speed of a fan */ +void set_fan_speed(t_fans* fan, int speed) { - t_fans *tmp = fans; - - while(tmp != NULL) { - if(tmp->file != NULL && tmp->old_speed != speed) { - char buf[16]; - int len = snprintf(buf, sizeof(buf), "%d", speed); - int res = pwrite(fileno(tmp->file), buf, len, /*offset=*/ 0); - if (res == -1) { - perror("Could not set fan speed"); - } - tmp->old_speed = speed; - } - - tmp = tmp->next; + if(fan != NULL && fan->file != NULL && fan->old_speed != speed) { + char buf[16]; + int len = snprintf(buf, sizeof(buf), "%d", speed); + int res = pwrite(fileno(fan->file), buf, len, /*offset=*/ 0); + if (res == -1) { + perror("Could not set fan speed"); + } + fan->old_speed = speed; } - } +void set_fan_minimum_speed(t_fans* fans) +{ + t_fans *tmp = fans; + while(tmp != NULL) { + set_fan_speed(tmp,tmp->fan_min_speed); + tmp = tmp->next; + } +} unsigned short get_temp(t_sensors* sensors) { sensors = refresh_sensors(sensors); @@ -421,8 +444,7 @@ unsigned short get_temp(t_sensors* sensors) return temp; } - -void retrieve_settings(const char* settings_path) +void retrieve_settings(const char* settings_path, t_fans* fans) { Settings *settings = NULL; int result = 0; @@ -461,19 +483,29 @@ void retrieve_settings(const char* settings_path) } } else { - /* Read configfile values */ - result = settings_get_int(settings, "general", "min_fan_speed"); - - if (result != 0) { - min_fan_speed = result; - } + + t_fans *fan = fans; - result = settings_get_int(settings, "general", "max_fan_speed"); + while(fan != NULL) { - if (result != 0) { - max_fan_speed = result; - } + char* config_key; + config_key = smprintf("min_fan%d_speed", fan->fan_id); + /* Read configfile values */ + result = settings_get_int(settings, "general", config_key); + if (result != 0) { + fan->fan_min_speed = result; + } + free(config_key); + + config_key = smprintf("max_fan%d_speed", fan->fan_id); + result = settings_get_int(settings, "general", config_key); + if (result != 0) { + fan->fan_max_speed = result; + } + free(config_key); + fan = fan->next; + } result = settings_get_int(settings, "general", "low_temp"); if (result != 0) { @@ -504,34 +536,48 @@ void retrieve_settings(const char* settings_path) } } - void mbpfan() { int old_temp, new_temp, fan_speed, steps; int temp_change; - int step_up, step_down; - - retrieve_settings(NULL); + + sensors = retrieve_sensors(); + fans = retrieve_fans(); - if (min_fan_speed > max_fan_speed) { - syslog(LOG_INFO, "Invalid fan speeds: %d %d", min_fan_speed, max_fan_speed); - printf("Invalid fan speeds: %d %d\n", min_fan_speed, max_fan_speed); - exit(EXIT_FAILURE); + retrieve_settings(NULL, fans); + + t_fans* fan = fans; + while(fan != NULL) { + + if (fan->fan_min_speed > fan->fan_max_speed) { + syslog(LOG_INFO, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); + printf("Invalid fan speeds: %d %d\n", fan->fan_min_speed, fan->fan_max_speed); + exit(EXIT_FAILURE); + } + fan = fan->next; } + if (low_temp > high_temp || high_temp > max_temp) { syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp); printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_temp); exit(EXIT_FAILURE); } - sensors = retrieve_sensors(); - fans = retrieve_fans(); set_fans_man(fans); new_temp = get_temp(sensors); + set_fan_minimum_speed(fans); + + fan = fans; + while(fan != NULL) { - fan_speed = min_fan_speed; - set_fan_speed(fans, fan_speed); + fan->step_up = (float)( fan->fan_max_speed - fan->fan_min_speed ) / + (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); + + fan->step_down = (float)( fan->fan_max_speed - fan->fan_min_speed ) / + (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); + fan = fan->next; + } if(verbose) { printf("Sleeping for 2 seconds to get first temp delta\n"); @@ -542,45 +588,46 @@ void mbpfan() } sleep(2); - step_up = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); - - step_down = (float)( max_fan_speed - min_fan_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); - while(1) { old_temp = new_temp; new_temp = get_temp(sensors); - if(new_temp >= max_temp && fan_speed != max_fan_speed) { - fan_speed = max_fan_speed; - } + fan = fans; - if(new_temp <= low_temp && fan_speed != min_fan_speed) { - fan_speed = min_fan_speed; - } + while(fan != NULL) { + fan_speed = fan->old_speed; - temp_change = new_temp - old_temp; + if(new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { + fan_speed = fan->fan_max_speed; + } - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { - steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; - fan_speed = max( fan_speed, ceil(min_fan_speed + steps * step_up) ); - } + if(new_temp <= low_temp && fan_speed != fan->fan_min_speed) { + fan_speed = fan->fan_min_speed; + } - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { - steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; - fan_speed = min( fan_speed, floor(max_fan_speed - steps * step_down) ); - } + temp_change = new_temp - old_temp; - if(verbose) { - printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { + steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; + fan_speed = max( fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up) ); + } - if(daemonize) { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); + if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { + steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; + fan_speed = min( fan_speed, floor(fan->fan_max_speed - steps * fan->step_down) ); } - } - set_fan_speed(fans, fan_speed); + if(verbose) { + printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + + if(daemonize) { + syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); + } + } + + set_fan_speed(fan, fan_speed); + fan = fan->next; + } if(verbose) { printf("Sleeping for %d seconds\n", polling_interval); @@ -590,7 +637,7 @@ void mbpfan() syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); } } - + // call nanosleep instead of sleep to avoid rt_sigprocmask and // rt_sigaction struct timespec ts; diff --git a/src/mbpfan.h b/src/mbpfan.h index 37a38d6..5198a13 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -17,11 +17,6 @@ #ifndef _MBPFAN_H_ #define _MBPFAN_H_ -/** Basic fan speed parameters - */ -extern int min_fan_speed; -extern int max_fan_speed; - /** Temperature Thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature @@ -55,7 +50,7 @@ bool is_legacy_sensors_path(); * /etc/mbpfan.conf * If it fails, the default hardcoded settings are used */ -void retrieve_settings(const char* settings_path); +void retrieve_settings(const char* settings_path, t_fans *fans); /** * Detect the sensors in /sys/devices/platform/coretemp.0/temp @@ -89,11 +84,15 @@ void set_fans_man(t_fans *fans); void set_fans_auto(t_fans *fans); /** - * Given a list of sensors with associated fans + * Given a sensors with associated fans * Change their speed */ -void set_fan_speed(t_fans* fans, int speed); +void set_fan_speed(t_fans* fan, int speed); +/** + * Given a list of fans set their minumum fan speed + */ +void set_fan_minimum_speed(t_fans* fans); /** * Return average CPU temp in degrees (ceiling) */ diff --git a/src/minunit.c b/src/minunit.c index fc19871..7389e1f 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -129,30 +129,55 @@ static const char *test_config_file() static const char *test_settings() { - set_defaults(); - retrieve_settings("./mbpfan.conf.test1"); - mu_assert("max_fan_speed value is not 6200", max_fan_speed == 6200); + t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); + fan->fan_id = 1; + fan->fan_max_speed = -1; + fan->next = NULL; + + retrieve_settings("./mbpfan.conf.test1", fan); + mu_assert("max_fan_speed value is not 6200", fan->fan_max_speed == 6200); mu_assert("polling_interval is not 1", polling_interval == 1); - set_defaults(); - retrieve_settings("./mbpfan.conf.test0"); - mu_assert("min_fan_speed value is not 2000", min_fan_speed == 2000); + + fan->fan_min_speed = -1; + retrieve_settings("./mbpfan.conf.test0", fan); + mu_assert("min_fan_speed value is not 2000", fan->fan_min_speed == 2000); mu_assert("polling_interval is not 7", polling_interval == 7); + + t_fans* fan2 = (t_fans *)malloc(sizeof(t_fans)); + fan2->fan_id = 2; + fan2->fan_max_speed = -1; + fan->next = fan2; + + retrieve_settings("./mbpfan.conf.test2", fan); + mu_assert("min_fan1_speed value is not 2000", fan->fan_min_speed == 2000); + mu_assert("min_fan2_speed value is not 2000", fan->next->fan_min_speed == 2000); + + free(fan2); + fan->next = NULL; + free(fan); + return 0; -} +} int received = 0; static void handler(int signal) { + t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); + fan->fan_id = 1; + fan->next = NULL; + switch(signal) { case SIGHUP: received = 1; - retrieve_settings("./mbpfan.conf.test1"); + retrieve_settings("./mbpfan.conf.test1", fan); + free(fan); break; default: received = 0; + free(fan); break; } } @@ -167,15 +192,20 @@ static const char *test_sighup_receive() static const char *test_settings_reload() { + t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); + fan->fan_id = 1; + fan->next = NULL; + signal(SIGHUP, handler); - retrieve_settings("./mbpfan.conf"); + retrieve_settings("./mbpfan.conf", fan); printf("Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); // cannot tests min_fan_speed since it is not set and thus auto-detected mu_assert("polling_interval is not 7 before SIGHUP", polling_interval == 7); raise(SIGHUP); - mu_assert("min_fan_speed value is not 6200 after SIGHUP", min_fan_speed == 6200); + mu_assert("min_fan_speed value is not 6200 after SIGHUP", fan->fan_min_speed == 6200); mu_assert("polling_interval is not 1 after SIGHUP", polling_interval == 1); - retrieve_settings("./mbpfan.conf"); + retrieve_settings("./mbpfan.conf", fan); + free(fan); return 0; } From dd2c305593e43778f072f48e3f6331ba151548c9 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Jan 2019 11:32:16 -0800 Subject: [PATCH 176/235] Change default polling_interval to 1 This allows mbpfan to react more quickly to changing workloads and avoid CPU throttling. Also change unit test expectations. Fixes #159. --- mbpfan.conf | 2 +- mbpfan.conf.test0 | 2 +- mbpfan.conf.test1 | 2 +- mbpfan.conf.test2 | 2 +- src/mbpfan.c | 2 +- src/minunit.c | 6 +++--- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mbpfan.conf b/mbpfan.conf index e65129b..39d4b57 100644 --- a/mbpfan.conf +++ b/mbpfan.conf @@ -15,4 +15,4 @@ low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 -polling_interval = 7 # default is 7 seconds +polling_interval = 1 # default is 1 seconds diff --git a/mbpfan.conf.test0 b/mbpfan.conf.test0 index 13bccf6..5095078 100644 --- a/mbpfan.conf.test0 +++ b/mbpfan.conf.test0 @@ -5,4 +5,4 @@ max_fan1_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/ap low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 -polling_interval = 7 # default is 7 seconds +polling_interval = 7 # default is 1 second diff --git a/mbpfan.conf.test1 b/mbpfan.conf.test1 index 8a18c95..bea4137 100644 --- a/mbpfan.conf.test1 +++ b/mbpfan.conf.test1 @@ -4,4 +4,4 @@ max_fan1_speed = 6200 # default is 6200 low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # do not set it > 90, default is 86 -polling_interval = 1 # default is 7 +polling_interval = 2 # default is 1 second diff --git a/mbpfan.conf.test2 b/mbpfan.conf.test2 index 9451ef8..90b5537 100644 --- a/mbpfan.conf.test2 +++ b/mbpfan.conf.test2 @@ -4,4 +4,4 @@ min_fan2_speed = 2000 # default is 6200 low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # do not set it > 90, default is 86 -polling_interval = 1 # default is 7 +polling_interval = 1 # default is 1 second diff --git a/src/mbpfan.c b/src/mbpfan.c index f183229..0afe2a4 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -63,7 +63,7 @@ int max_temp = 86; // do not set it > 90 #define NUM_MIN_FAN_SPEED_DEFAULT 2000 #define NUM_MAX_FAN_SPEED_DEFAULT 6200 -int polling_interval = 7; +int polling_interval = 1; t_sensors* sensors = NULL; t_fans* fans = NULL; diff --git a/src/minunit.c b/src/minunit.c index 7389e1f..2818b50 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -136,7 +136,7 @@ static const char *test_settings() retrieve_settings("./mbpfan.conf.test1", fan); mu_assert("max_fan_speed value is not 6200", fan->fan_max_speed == 6200); - mu_assert("polling_interval is not 1", polling_interval == 1); + mu_assert("polling_interval is not 2", polling_interval == 2); fan->fan_min_speed = -1; retrieve_settings("./mbpfan.conf.test0", fan); @@ -200,10 +200,10 @@ static const char *test_settings_reload() retrieve_settings("./mbpfan.conf", fan); printf("Testing the _supplied_ mbpfan.conf (not the one you are using)..\n"); // cannot tests min_fan_speed since it is not set and thus auto-detected - mu_assert("polling_interval is not 7 before SIGHUP", polling_interval == 7); + mu_assert("polling_interval is not 1 before SIGHUP", polling_interval == 1); raise(SIGHUP); mu_assert("min_fan_speed value is not 6200 after SIGHUP", fan->fan_min_speed == 6200); - mu_assert("polling_interval is not 1 after SIGHUP", polling_interval == 1); + mu_assert("polling_interval is not 2 after SIGHUP", polling_interval == 2); retrieve_settings("./mbpfan.conf", fan); free(fan); return 0; From 07f19dea5ff2d12c39a4c30480ab03d83d5adba2 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Jan 2019 11:38:18 -0800 Subject: [PATCH 177/235] Add Travis configuration. References #173. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f55bd0b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +dist: xenial +language: c +script: + - make From e0b2de34ebac3b57ec664fdaf4a71cc54450d9ce Mon Sep 17 00:00:00 2001 From: Daniel Graziotin Date: Mon, 21 Jan 2019 14:20:00 +0100 Subject: [PATCH 178/235] Added travis badge [refs 173] --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 145d398..7d5872a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ mbpfan ==================== +[![Build Status](https://travis-ci.org/dgraziotin/mbpfan.svg?branch=master)](https://travis-ci.org/dgraziotin/mbpfan) + This is an enhanced version of [Allan McRae mbpfan](http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/) mbpfan is a daemon that uses input from coretemp module and sets the fan speed using the applesmc module. From 53e1c352218075a581ff013a37621103994a417f Mon Sep 17 00:00:00 2001 From: Ati Date: Tue, 22 Jan 2019 23:20:55 +0000 Subject: [PATCH 179/235] Add testing of Mac Pro models to README. Ref Issue #140 and (incidentally) #167 --- AUTHORS | 1 + README.md | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/AUTHORS b/AUTHORS index 89fd00e..3c0b507 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,6 +14,7 @@ Yamakaky Yi Yang Herminio Hernandez Jr Robert Musial +Ati Sharma ORIGINARY AUTHORS diff --git a/README.md b/README.md index 7d5872a..0ead57c 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,13 @@ This section reports the iMac/Mac mini models where mbpfan was tested successful - Mac mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) - Mac mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) +## Tested Mac Pro Models + +This section reports the Mac Pro models where mbpfan was tested successfully. + +- Mac Pro 3,1 +- Mac Pro 4,1 (with 5,1 firmware and hex-core Xeon X5690 mod - Linux 4.14) + ## Requirements Be sure to load the kernel modules **applesmc** and **coretemp**. From c77f23aa1b53a881e09a7b78e5a8c8e9dbe9c67d Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 23 Jan 2019 12:15:44 -0800 Subject: [PATCH 180/235] Change minimum and maximum fan speeds MacBook Airs allow 1200-6500 and Mac Pro allow 500-5200. Allow mbpfan to configure smaller and larger values. Also fix typo in constant names. --- src/mbpfan.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 0afe2a4..85879ac 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -60,8 +60,9 @@ int max_temp = 86; // do not set it > 90 #define NUM_HWMONS 12 #define NUM_TEMP_INPUTS 16 #define NUM_FANS 10 -#define NUM_MIN_FAN_SPEED_DEFAULT 2000 -#define NUM_MAX_FAN_SPEED_DEFAULT 6200 +// sane defaults when user provides unexpected values +#define MIN_FAN_SPEED_DEFAULT 500 +#define MAX_FAN_SPEED_DEFAULT 6500 int polling_interval = 1; @@ -291,14 +292,14 @@ t_fans *retrieve_fans() fan->fan_id = counter; int fan_speed = read_value(path_fan_min); - if(fan_speed == -1 || fan_speed < NUM_MIN_FAN_SPEED_DEFAULT) - fan->fan_min_speed = NUM_MIN_FAN_SPEED_DEFAULT; + if(fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) + fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; else fan->fan_min_speed = fan_speed; fan_speed = read_value(path_fan_max); - if(fan_speed == -1 || fan_speed > NUM_MAX_FAN_SPEED_DEFAULT) - fan->fan_max_speed = NUM_MAX_FAN_SPEED_DEFAULT; + if(fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) + fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; else fan->fan_max_speed = fan_speed; From 5df44bd5c216f0c0b6775f571654bb4f80aa4c28 Mon Sep 17 00:00:00 2001 From: Krutonium <3945538+Krutonium@users.noreply.github.com> Date: Thu, 2 May 2019 00:42:22 -0400 Subject: [PATCH 181/235] Add MacBook Pro 3,1 Works Perfectly, as far as I can tell. The cooling on this computer isn't great so it rarely spins down. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0ead57c..e5ca173 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 7,1 15" (Intel Core 2 Duo - Linux 3.13) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) +- MacBook Pro 3,1 17" (Intel Core 2 Duo - Linux 5.0.7) - MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) - MacBook Air 6,1 13" (Intel i7 - Linux 3.13) - MacBook Air 5,2 13" (Intel i5 - Linux 3.16) From f87082261f9e9635bdb36dc9dc8a501ae8dece54 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 28 Apr 2019 15:13:45 +0900 Subject: [PATCH 182/235] Recommend using Debian and Ubuntu packages --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5ca173..ec0239b 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,9 @@ This enhanced version assumes any number of processors and fans (max. 10). - [Requirements](#requirements) - [Installation](#installation) - [Arch Linux](#arch-linux) - - [Ubuntu](#ubuntu) + - [Debian](#debian) - [Gentoo](#gentoo) + - [Ubuntu](#ubuntu) - [Generic Install Instructions (All Other Operating Systems)](#generic-install-instructions-all-other-operating-systems) - [Run Instructions](#run-instructions) - [Starting at boot](#starting-at-boot) @@ -123,13 +124,13 @@ Please check the relevant documentation of your GNU/Linux distribution. See [mbpfan-git at AUR](https://aur.archlinux.org/packages/mbpfan-git/). Otherwise, please refer to the Generic Instructions. +### Debian -### Ubuntu - -Install the ```build-essential``` package. -Then, refer to the Generic Install Instructions. +On Debian 10 or later install via: -Otherwise, a step-by-step [tutorial for beginners is available on my website](https://ineed.coffee/3838/a-beginners-mbpfan-tutorial-for-ubuntu/). +``` +sudo apt-get install mbpfan +``` ### Gentoo @@ -137,6 +138,14 @@ Install the ```mbpfan``` package with: sudo emerge -av app-laptop/mbpfan +### Ubuntu + +On Ubuntu 18.04 or later install via: + +``` +sudo apt-get install mbpfan +``` + ### Generic Install Instructions (All Other Operating Systems) Compile with From d63fa7c7bf69cf9600f7facbf34155206d08ba96 Mon Sep 17 00:00:00 2001 From: Krutonium <3945538+Krutonium@users.noreply.github.com> Date: Tue, 14 May 2019 03:53:39 -0400 Subject: [PATCH 183/235] Add install instructions for Solus As the title says, this adds the command line for Solus, since this is in the repos. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index ec0239b..da8d28c 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,16 @@ Install the ```mbpfan``` package with: sudo emerge -av app-laptop/mbpfan + +### Solus + +On Solus, install the package with: +``` +sudo eopkg install mbpfan +``` +then enable the service. + + ### Ubuntu On Ubuntu 18.04 or later install via: From 50834997b708b328cc0d7e3d851e2d29487e1caa Mon Sep 17 00:00:00 2001 From: jolupa Date: Tue, 9 Jul 2019 02:40:35 +0200 Subject: [PATCH 184/235] CRUX Linux package --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index da8d28c..0c93be8 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ This enhanced version assumes any number of processors and fans (max. 10). - [Requirements](#requirements) - [Installation](#installation) - [Arch Linux](#arch-linux) + - [CRUX Linux](#crux-linux) - [Debian](#debian) - [Gentoo](#gentoo) - [Ubuntu](#ubuntu) @@ -124,6 +125,10 @@ Please check the relevant documentation of your GNU/Linux distribution. See [mbpfan-git at AUR](https://aur.archlinux.org/packages/mbpfan-git/). Otherwise, please refer to the Generic Instructions. +### CRUX Linux + +Follow the instructions on [jolupalabs REPO](https://github.com/jolupa/jolupalabs) for installation. + ### Debian On Debian 10 or later install via: From 99ab1d0d99fe80cdb9be75108e8d8a41f23539e2 Mon Sep 17 00:00:00 2001 From: David Dreggors Date: Fri, 16 Aug 2019 15:04:28 -0400 Subject: [PATCH 185/235] Adding another 'tested with' line to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0c93be8..85272b4 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ This section reports those models where mbpfan was tested successfully. It does - MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) - MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) - MacBook Pro 8,2 15" (Intel i7 - Linux 3.6.2) +- MacBook Pro 8,1 13" (Intel i5 - 2435M Linux 5.0.9 Fedora 30) - MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) - MacBook Pro 7,1 15" (Intel Core 2 Duo - Linux 3.13) - MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) From 9f55c0c2f02b15caa7bdd4e0d38ab9897f490006 Mon Sep 17 00:00:00 2001 From: Lyes Saadi <33597258+LyesSaadi@users.noreply.github.com> Date: Fri, 4 Oct 2019 23:04:05 +0100 Subject: [PATCH 186/235] Adding Fedora installation instructions. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 85272b4..524d3b1 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,15 @@ On Debian 10 or later install via: sudo apt-get install mbpfan ``` +### Fedora + +There is a [Fedora COPR](https://copr.fedorainfracloud.org/coprs/lyessaadi/mbpfan/) to install mbpfan until the package is submitted upstream. + +``` +sudo dnf copr enable lyessaadi/mbpfan +sudo dnf install mbpfan +``` + ### Gentoo Install the ```mbpfan``` package with: From 272ef08924c61b64fb3b72c6c6175b3b9bf24da9 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 4 Oct 2019 15:14:14 -0700 Subject: [PATCH 187/235] Add missing distro to table of contents --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 524d3b1..95effa3 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,9 @@ This enhanced version assumes any number of processors and fans (max. 10). - [Arch Linux](#arch-linux) - [CRUX Linux](#crux-linux) - [Debian](#debian) + - [Fedora](#fedora) - [Gentoo](#gentoo) + - [Solus](#solus) - [Ubuntu](#ubuntu) - [Generic Install Instructions (All Other Operating Systems)](#generic-install-instructions-all-other-operating-systems) - [Run Instructions](#run-instructions) From 559a2f39f10e19c38deceb4f81b746012dcf2f56 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 7 Oct 2019 19:48:05 -0700 Subject: [PATCH 188/235] Prefer smprintf instead of unsafe sprintf Previously checking fans >= 10 overflowed the buffer and corrupted the heap. Found via AddressSanitizer. Regression from 968133eb103ce543e2fab6c55e306be023509452. --- src/mbpfan.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 85879ac..b9da5bf 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -167,9 +167,7 @@ t_sensors *retrieve_sensors() int counter; for (counter = 0; counter < NUM_HWMONS; counter++) { - char hwmon_path[strlen(path_begin)+2]; - - sprintf(hwmon_path, "%s%d", path_begin, counter); + char *hwmon_path = smprintf("%s%d", path_begin, counter); int res = access(hwmon_path, R_OK); if (res == 0) { @@ -186,8 +184,11 @@ t_sensors *retrieve_sensors() } + free(hwmon_path); break; } + + free(hwmon_path); } int core = 0; From 4f3cdfca3bb06690c915272484e65872b2a9fd4c Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 7 Oct 2019 19:37:12 -0700 Subject: [PATCH 189/235] Label fans during logging This is more useful on desktops than laptops. --- src/daemon.c | 1 + src/global.h | 1 + src/mbpfan.c | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 42fc75f..dd108ff 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -86,6 +86,7 @@ static void cleanup_and_exit(int exit_code) if (fans->file != NULL) { fclose(fans->file); } + free(fans->label); free(fans->fan_output_path); free(fans->fan_manual_path); free(fans); diff --git a/src/global.h b/src/global.h index 46e5fc8..5cd41eb 100644 --- a/src/global.h +++ b/src/global.h @@ -17,6 +17,7 @@ struct s_sensors { struct s_fans { FILE* file; char* path; // TODO: unused + char* label; char* fan_output_path; char* fan_manual_path; int step_up; diff --git a/src/mbpfan.c b/src/mbpfan.c index b9da5bf..8e843e3 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -258,18 +258,38 @@ static int read_value(const char *path) return value; } +static void read_value_str(const char *path, char *str, size_t len) +{ + FILE *file = fopen(path, "r"); + if (file != NULL) { + fgets(str, len, file); + fclose(file); + } +} + +static void trim_trailing_whitespace(char *str) +{ + for (ssize_t i = strlen(str) - 1; i >= 0; --i) { + if (isspace(str[i]) || str[i] == '\n') { + str[i] = '\0'; + } + } +} + t_fans *retrieve_fans() { t_fans *fans_head = NULL; t_fans *fan = NULL; char *path_output = NULL; + char *path_label = NULL; char *path_manual = NULL; char *path_fan_max = NULL; char *path_fan_min = NULL; const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; const char *path_output_end = "_output"; + const char *path_label_end = "_label"; const char *path_man_end = "_manual"; const char *path_max_speed = "_max"; const char *path_min_speed = "_min"; @@ -280,6 +300,7 @@ t_fans *retrieve_fans() for(counter = 0; counterfan_max_speed = MAX_FAN_SPEED_DEFAULT; else fan->fan_max_speed = fan_speed; - + + size_t max_label_len = 64; + fan->label = malloc(max_label_len); + read_value_str(path_label, fan->label, max_label_len); + trim_trailing_whitespace(fan->label); + fan->old_speed = 0; if (fans_head == NULL) { @@ -326,6 +352,8 @@ t_fans *retrieve_fans() } free(path_fan_min); path_fan_min = NULL; + free(path_label); + path_label = NULL; free(path_fan_max); path_fan_max = NULL; free(path_output); @@ -620,10 +648,10 @@ void mbpfan() } if(verbose) { - printf("Old Temp %d: New Temp: %d, Fan Speed: %d\n", old_temp, new_temp, fan_speed); + printf("Fan: %s Old Temp %d: New Temp: %d Speed: %d\n", fan->label, old_temp, new_temp, fan_speed); if(daemonize) { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d, Fan Speed: %d", old_temp, new_temp, fan_speed); + syslog(LOG_INFO, "Fan: %s Old Temp %d: New Temp: %d Speed: %d", fan->label, old_temp, new_temp, fan_speed); } } From 6452c64d7ab68703de203846ef7a650cea75910d Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 29 Aug 2019 16:52:17 -0700 Subject: [PATCH 190/235] Do not recommend running tests References #190. --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 95effa3..a268b87 100644 --- a/README.md +++ b/README.md @@ -186,16 +186,14 @@ Install with It copies mbpfan to /usr/sbin, mbpfan.conf to /etc (and overwrites existing files), README.md to /usr/share/doc/mbpfan, and mbpfan.8.gz to /usr/share/man/man8 -Run the tests now, see two sections below. - If you would like to compile with Clang instead of GCC, simply set your system's default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang 4.0 along with llvm-lld (The LLVM Linker). -Run The Tests (Recommended) ---------------------------- -It is recommended to run the tests after installing the program. Please run the following command _from within the source directory_. +Run The Tests (Optional) +------------------------ +Users may run the tests after installing the program. Please run the following command _from within the source directory_. sudo ./bin/mbpfan -t @@ -203,6 +201,8 @@ or sudo make tests +Note that this only works on MacBook and not desktop computers due to different environment expectations. + ## Run Instructions From 64f6ea509a8b5ced9e12d76f3d2094f29dcbfb6b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 16 Oct 2019 09:57:19 -0700 Subject: [PATCH 191/235] Log fan label and speed next to each other --- src/mbpfan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 8e843e3..3f2c01e 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -648,10 +648,10 @@ void mbpfan() } if(verbose) { - printf("Fan: %s Old Temp %d: New Temp: %d Speed: %d\n", fan->label, old_temp, new_temp, fan_speed); + printf("Old Temp %d: New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed); if(daemonize) { - syslog(LOG_INFO, "Fan: %s Old Temp %d: New Temp: %d Speed: %d", fan->label, old_temp, new_temp, fan_speed); + syslog(LOG_INFO, "Old Temp %d: New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); } } From 98a349724e42fef1b391fe4e601acc65abae97d9 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 16 Oct 2019 10:03:12 -0700 Subject: [PATCH 192/235] Make logging consistent --- src/mbpfan.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 3f2c01e..24f58b1 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -648,10 +648,10 @@ void mbpfan() } if(verbose) { - printf("Old Temp %d: New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed); + printf("Old Temp: %d New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed); if(daemonize) { - syslog(LOG_INFO, "Old Temp %d: New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); + syslog(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); } } From 4722e79e7272f5f02d2d6526a92819328dd110d2 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 24 Oct 2019 15:42:24 -0700 Subject: [PATCH 193/235] Increase maximum sensors 2011 iMac has at least 50. References #148. --- src/mbpfan.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 24f58b1..25aa537 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -19,8 +19,8 @@ * * * Notes: - * Assumes any number of processors, cores and fans (max. 6, 16, 12 - * as defined in NUM_PROCESSORS, NUM_HWMONS, NUM_TEMP_INPUTS and NUM_FANS) + * Assumes any number of processors, cores, sensors and fans + * (as defined in NUM_PROCESSORS, NUM_HWMONS, NUM_TEMP_INPUTS and NUM_FANS) * It uses only the temperatures from the processors as input. * Requires coretemp and applesmc kernel modules to be loaded. * Requires root use @@ -58,7 +58,7 @@ int max_temp = 86; // do not set it > 90 // maximum number of processors etc supported #define NUM_PROCESSORS 6 #define NUM_HWMONS 12 -#define NUM_TEMP_INPUTS 16 +#define NUM_TEMP_INPUTS 64 #define NUM_FANS 10 // sane defaults when user provides unexpected values #define MIN_FAN_SPEED_DEFAULT 500 From 8984322c0addc9fd929ffd8d6f1288a0452ce53f Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 27 Oct 2019 17:03:31 -0700 Subject: [PATCH 194/235] Specify C99 standard This allows // comments and for loop declarations. Also specify some feature test macros to access some system calls. References #200. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f5766ab..be84021 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ BINFLAG = -o INCLUDES = LIBS = -lm LIBPATH = -CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function +CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) From a28dd3e208c45c309006a3f5372c2d699bee4dc5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 18 Sep 2019 17:21:24 -0700 Subject: [PATCH 195/235] Recalibrate after unexpected polling interval During suspend fans may reset from manual to automatic control. Fixes #148. --- src/mbpfan.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/mbpfan.c b/src/mbpfan.c index 25aa537..14bf795 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -609,6 +609,7 @@ void mbpfan() fan = fan->next; } +recalibrate: if(verbose) { printf("Sleeping for 2 seconds to get first temp delta\n"); @@ -668,11 +669,26 @@ void mbpfan() } } + time_t before_sleep = time(NULL); + // call nanosleep instead of sleep to avoid rt_sigprocmask and // rt_sigaction struct timespec ts; ts.tv_sec = polling_interval; ts.tv_nsec = 0; nanosleep(&ts, NULL); + + time_t after_sleep = time(NULL); + if(after_sleep - before_sleep > 2 * polling_interval) { + printf("Clock skew detected - slept for %ld seconds but expected %d\n", after_sleep - before_sleep, polling_interval); + fflush(stdout); + + if(daemonize) { + syslog(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); + } + + set_fans_man(fans); + goto recalibrate; + } } } From 2c743b08e91cf9aee3612de98fe25dc36e9fc4ce Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 19 Oct 2019 16:30:46 -0700 Subject: [PATCH 196/235] Calculate temperature using maximum Previously mbpfan used the mean temperature of all sensors which could throttle processors by too slowly spinning the fans. Fixes #199. --- src/mbpfan.c | 16 +++------------- src/mbpfan.h | 2 +- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 14bf795..fad6ac9 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -452,26 +452,16 @@ void set_fan_minimum_speed(t_fans* fans) unsigned short get_temp(t_sensors* sensors) { sensors = refresh_sensors(sensors); - int sum_temp = 0; - unsigned short temp = 0; + unsigned int temp = 0; t_sensors* tmp = sensors; - int number_sensors = 0; - while(tmp != NULL) { - sum_temp += tmp->temperature; + temp = max(temp, tmp->temperature); tmp = tmp->next; - number_sensors++; - } - - // just to be safe - if (number_sensors == 0) { - number_sensors++; } - temp = (unsigned short)( ceil( (float)( sum_temp ) / (number_sensors * 1000) ) ); - return temp; + return temp / 1000; } void retrieve_settings(const char* settings_path, t_fans* fans) diff --git a/src/mbpfan.h b/src/mbpfan.h index 5198a13..595bc09 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -94,7 +94,7 @@ void set_fan_speed(t_fans* fan, int speed); */ void set_fan_minimum_speed(t_fans* fans); /** - * Return average CPU temp in degrees (ceiling) + * Return maximum CPU temp in degrees */ unsigned short get_temp(t_sensors* sensors); From 2ed1671aa7ef9c0dfff6bea78fc155410215348f Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 28 Oct 2019 23:21:19 -0700 Subject: [PATCH 197/235] Prefer simpler strtod over sscanf --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index fad6ac9..2e02fd6 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -417,7 +417,7 @@ t_sensors *refresh_sensors(t_sensors *sensors) char buf[16]; int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0); buf[len] = '\0'; - sscanf(buf, "%d", &tmp->temperature); + tmp->temperature = strtod(buf, NULL); } tmp = tmp->next; From d818571a786b3e61af07b8f08b63a3dea0f8583e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 24 Oct 2019 01:09:34 -0700 Subject: [PATCH 198/235] Fix clang-tidy issues --- src/daemon.c | 2 -- src/mbpfan.c | 4 ++-- src/mbpfan.h | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index dd108ff..afdef6a 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -243,6 +243,4 @@ void go_daemon(void (*fan_control)()) if(daemonize) { syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); } - - return; } diff --git a/src/mbpfan.c b/src/mbpfan.c index 2e02fd6..477f6e8 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -592,10 +592,10 @@ void mbpfan() while(fan != NULL) { fan->step_up = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2 ); + (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2.0 ); fan->step_down = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2 ); + (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2.0 ); fan = fan->next; } diff --git a/src/mbpfan.h b/src/mbpfan.h index 595bc09..ce9aa8c 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -17,6 +17,8 @@ #ifndef _MBPFAN_H_ #define _MBPFAN_H_ +#include + /** Temperature Thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature From 0219257effff3d34adc2fcf44e8d07f1bde57dbb Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 28 Oct 2019 23:41:46 -0700 Subject: [PATCH 199/235] Move supported models to wiki --- README.md | 45 ++++----------------------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index a268b87..7224def 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,7 @@ This enhanced version assumes any number of processors and fans (max. 10). **Table Of Contents** - [Supported GNU/Linux Distributions](#supported-gnulinux-distributions) -- [Tested MacBook Models](#tested-macbook-models) -- [Tested iMac/Mac mini Models](#tested-imacmac-mini-models) +- [Tested Mac Models](#tested-mac-models) - [Requirements](#requirements) - [Installation](#installation) - [Arch Linux](#arch-linux) @@ -54,45 +53,9 @@ Supported distributions: - Trisquel - Solus -## Tested MacBook Models - -This section reports those models where mbpfan was tested successfully. It does not necessarily mean that the daemon does not work on non-listed models. - -- MacBook Pro 12,1 13" (Intel i5 - Linux 4.20) -- MacBook Pro 11,4 15" (Intel i7 - Linux 4.9.41) -- MacBook Pro 11,1 13" (Intel i5 - Linux 3.14, Linux 3.15) -- MacBook Pro 9,2 13" (Intel i5/i7 - Linux 3.10) -- MacBook Pro 8,2 15" (Intel i7 - Linux 3.6.2) -- MacBook Pro 8,1 13" (Intel i5 - 2435M Linux 5.0.9 Fedora 30) -- MacBook Pro 8,1 13" (Intel i7 - Linux 3.2.0) -- MacBook Pro 7,1 15" (Intel Core 2 Duo - Linux 3.13) -- MacBook Pro 6,2 15" (Intel i7 - Linux 3.5.0) -- MacBook Pro 6,2 15" (Intel i7 - Linux 3.2.0) -- MacBook Pro 3,1 17" (Intel Core 2 Duo - Linux 5.0.7) -- MacBook Pro 2,2 15" (Intel Core 2 Duo - Linux 3.4.4) -- MacBook Air 6,1 13" (Intel i7 - Linux 3.13) -- MacBook Air 5,2 13" (Intel i5 - Linux 3.16) -- MacBook Air 1,1 13" (Intel Core Duo - Linux 4.4, Linux 4.8) -- MacBook Air 7,2 13" (Intel Core Duo - Linux 4.10) -- MacBook 1,1 (Intel Core Duo - Linux 3.16) - -## Tested iMac/Mac mini Models - -This section reports the iMac/Mac mini models where mbpfan was tested successfully. - -- iMac Retina 16.2 21.5" (Intel i5 - Linux 4.4.0 Ubuntu 16.04) -- iMac 12,1 21.5" (Intel i7 - Linux 4.6.4) -- iMac 5,1 17" (Intel T7400 (Core 2 Duo) - Linux 14.04 Ubuntu) -- Mac mini 2,1 (Core 2 Duo - Linux 4.4.0) -- Mac mini 5,3 (Core i7 2.0 - Linux 4.4.0 elementary/Ubuntu) -- Mac mini 6,1 (Core i7 2.3 - Linux 4.7.3-4-ck Archlinux) - -## Tested Mac Pro Models - -This section reports the Mac Pro models where mbpfan was tested successfully. - -- Mac Pro 3,1 -- Mac Pro 4,1 (with 5,1 firmware and hex-core Xeon X5690 mod - Linux 4.14) +## Tested Mac Models + +See https://github.com/linux-on-mac/mbpfan/wiki/Tested-Mac-Models . ## Requirements From abda2b8901b004557d3abb0fc0e90df6b68d3d65 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 10:38:21 -0700 Subject: [PATCH 200/235] Add logging/printing helper This eliminates duplicate code, fixes some log levels, and removes unnecessary trailing newlines. Fixes #202. --- src/daemon.c | 21 +++---------- src/main.c | 10 +++--- src/mbpfan.c | 86 +++++++++++----------------------------------------- src/util.c | 18 +++++++++++ src/util.h | 6 ++++ 5 files changed, 50 insertions(+), 91 deletions(-) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/src/daemon.c b/src/daemon.c index afdef6a..7ce03fd 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -33,6 +33,7 @@ #include "mbpfan.h" #include "global.h" #include "daemon.h" +#include "util.h" int write_pid(int pid) { @@ -207,33 +208,21 @@ void go_daemon(void (*fan_control)()) if (read_pid() == -1) { if (verbose) { - printf("Writing a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID); - syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); + mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); } if (write_pid(current_pid) == 0) { - syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID); - - if (verbose) { - printf("ERROR: Can not create a .pid file at: %s. Aborting\n", PROGRAM_PID); - } - + mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID); exit(EXIT_FAILURE); } else { if (verbose) { - printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID); - syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); + mbp_log(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID); } } } else { - syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID); - - if (verbose) { - printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", PROGRAM_PID); - } - + mbp_log(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID); exit(EXIT_FAILURE); } diff --git a/src/main.c b/src/main.c index 2c4286b..df22318 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ #include "global.h" #include "main.h" #include "minunit.h" +#include "util.h" int daemonize = 1; int verbose = 0; @@ -64,8 +65,7 @@ void check_requirements() uid_t uid=getuid(), euid=geteuid(); if (uid != 0 || euid != 0) { - syslog(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); - printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME); + mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); exit(EXIT_FAILURE); } @@ -75,8 +75,7 @@ void check_requirements() DIR* dir = opendir(CORETEMP_PATH); if (ENOENT == errno) { - syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); exit(EXIT_FAILURE); } @@ -86,8 +85,7 @@ void check_requirements() dir = opendir(APPLESMC_PATH); if (ENOENT == errno) { - syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME); + mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); exit(EXIT_FAILURE); } diff --git a/src/mbpfan.c b/src/mbpfan.c index 477f6e8..129c708 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -42,6 +42,7 @@ #include "mbpfan.h" #include "global.h" #include "settings.h" +#include "util.h" /* lazy min/max... */ #define min(a,b) ((a) < (b) ? (a) : (b)) @@ -100,8 +101,7 @@ bool is_modern_sensors_path() str_kernel_version = strtok(kernel.release, "."); if (atoi(str_kernel_version) < 3){ - syslog(LOG_INFO, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); - printf("mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); + mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); exit(EXIT_FAILURE); } @@ -136,11 +136,7 @@ t_sensors *retrieve_sensors() if (!is_modern_sensors_path()) { if(verbose) { - printf("Using legacy sensor path for kernel < 3.15.0\n"); - - if(daemonize) { - syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0"); - } + mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0"); } path_begin = strdup("/sys/devices/platform/coretemp.0/temp"); @@ -148,11 +144,7 @@ t_sensors *retrieve_sensors() } else { if(verbose) { - printf("Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0\n"); - - if(daemonize) { - syslog(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); - } + mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); } // loop over up to 6 processors @@ -176,12 +168,7 @@ t_sensors *retrieve_sensors() path_begin = smprintf("%s/temp", hwmon_path); if(verbose) { - printf("Found hwmon path at %s\n", path_begin); - - if(daemonize) { - syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin); - } - + mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin); } free(hwmon_path); @@ -228,16 +215,11 @@ t_sensors *retrieve_sensors() } if(verbose) { - printf("Found %d sensors\n", sensors_found); - - if(daemonize) { - syslog(LOG_INFO, "Found %d sensors", sensors_found); - } + mbp_log(LOG_INFO, "Found %d sensors", sensors_found); } if (sensors_found == 0){ - syslog(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.\n"); - printf("mbpfan could not detect any temp sensor. Please contact the developer.\n"); + mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer."); exit(EXIT_FAILURE); } @@ -363,16 +345,11 @@ t_fans *retrieve_fans() } if(verbose) { - printf("Found %d fans\n", fans_found); - - if(daemonize) { - syslog(LOG_INFO, "Found %d fans", fans_found); - } + mbp_log(LOG_INFO, "Found %d fans", fans_found); } if (fans_found == 0){ - syslog(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.\n"); - printf("mbpfan could not detect any fan. Please contact the developer.\n"); + mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer."); exit(EXIT_FAILURE); } @@ -481,11 +458,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans) if (f == NULL) { /* Could not open configfile */ if(verbose) { - printf("Couldn't open configfile, using defaults\n"); - - if(daemonize) { - syslog(LOG_INFO, "Couldn't open configfile, using defaults"); - } + mbp_log(LOG_INFO, "Couldn't open configfile, using defaults"); } } else { @@ -495,11 +468,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans) if (settings == NULL) { /* Could not read configfile */ if(verbose) { - printf("Couldn't read configfile\n"); - - if(daemonize) { - syslog(LOG_WARNING, "Couldn't read configfile"); - } + mbp_log(LOG_WARNING, "Couldn't read configfile"); } } else { @@ -570,16 +539,14 @@ void mbpfan() while(fan != NULL) { if (fan->fan_min_speed > fan->fan_max_speed) { - syslog(LOG_INFO, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); - printf("Invalid fan speeds: %d %d\n", fan->fan_min_speed, fan->fan_max_speed); + mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); exit(EXIT_FAILURE); } fan = fan->next; } if (low_temp > high_temp || high_temp > max_temp) { - syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp); - printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_temp); + mbp_log(LOG_ERR, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp); exit(EXIT_FAILURE); } @@ -601,11 +568,7 @@ void mbpfan() recalibrate: if(verbose) { - printf("Sleeping for 2 seconds to get first temp delta\n"); - - if(daemonize) { - syslog(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); - } + mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); } sleep(2); @@ -639,11 +602,7 @@ void mbpfan() } if(verbose) { - printf("Old Temp: %d New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed); - - if(daemonize) { - syslog(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); - } + mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); } set_fan_speed(fan, fan_speed); @@ -651,12 +610,7 @@ void mbpfan() } if(verbose) { - printf("Sleeping for %d seconds\n", polling_interval); - fflush(stdout); - - if(daemonize) { - syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval); - } + mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval); } time_t before_sleep = time(NULL); @@ -670,13 +624,7 @@ void mbpfan() time_t after_sleep = time(NULL); if(after_sleep - before_sleep > 2 * polling_interval) { - printf("Clock skew detected - slept for %ld seconds but expected %d\n", after_sleep - before_sleep, polling_interval); - fflush(stdout); - - if(daemonize) { - syslog(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); - } - + mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); set_fans_man(fans); goto recalibrate; } diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..73600c4 --- /dev/null +++ b/src/util.c @@ -0,0 +1,18 @@ +#include +#include +#define __USE_MISC +#include + +#include "global.h" + +void mbp_log(int level, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + if (daemonize) { + vsyslog(level, fmt, args); + } + vprintf(fmt, args); + puts(""); // trailing newline + va_end(args); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..1f3819e --- /dev/null +++ b/src/util.h @@ -0,0 +1,6 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ + +void mbp_log(int level, const char *fmt, ...); + +#endif From 110ccb58144d1808f2200aef17b12a19a18a86cd Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 16:04:33 -0700 Subject: [PATCH 201/235] Address Valgrind warnings during unit tests --- src/minunit.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/minunit.c b/src/minunit.c index 2818b50..e59b3a3 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -146,6 +146,7 @@ static const char *test_settings() t_fans* fan2 = (t_fans *)malloc(sizeof(t_fans)); fan2->fan_id = 2; fan2->fan_max_speed = -1; + fan2->next = NULL; fan->next = fan2; retrieve_settings("./mbpfan.conf.test2", fan); @@ -194,6 +195,7 @@ static const char *test_settings_reload() { t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); fan->fan_id = 1; + fan->fan_min_speed = -1; fan->next = NULL; signal(SIGHUP, handler); From dae65c20a5cd9b468acbe83d4974988741c7d9d0 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 20:22:54 -0700 Subject: [PATCH 202/235] Only specify LIBS once The $(BIN) rule already specifies this. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index be84021..d5223ae 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ INCLUDES = LIBS = -lm LIBPATH = CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 -LDFLAGS += $(LIBPATH) -g $(LIBS) #-Wall +LDFLAGS += $(LIBPATH) -g OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) From 28bc0a1beadc690235c8d383d4ceb682bafabf9f Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 16:04:51 -0700 Subject: [PATCH 203/235] Plug memory leaks during unit tests Found via Valgrind. --- src/minunit.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/minunit.c b/src/minunit.c index e59b3a3..a9aedb9 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -14,6 +14,27 @@ int tests_run = 0; +static void free_fans(t_fans* fans) +{ + while (fans != NULL) { + t_fans* tmp = fans->next; + free(fans->fan_manual_path); + free(fans->fan_output_path); + free(fans->label); + free(fans); + fans = tmp; + } +} + +static void free_sensors(t_sensors* sensors) +{ + while (sensors != NULL) { + t_sensors* tmp = sensors->next; + free(sensors->path); + free(sensors); + sensors = tmp; + } +} static const char *test_sensor_paths() { @@ -31,6 +52,7 @@ static const char *test_sensor_paths() tmp = tmp->next; } + free_sensors(sensors); return 0; } @@ -51,6 +73,7 @@ static const char *test_fan_paths() } mu_assert("No fans found", found_fan_path != 0); + free_fans(fans); return 0; } @@ -95,6 +118,7 @@ static const char *test_get_temp() stress(2000); unsigned short temp_2 = get_temp(sensors); mu_assert("Invalid Higher temp test (if fan was already spinning high, this is not worrying)", temp_1 < temp_2); + free_sensors(sensors); return 0; } @@ -196,6 +220,9 @@ static const char *test_settings_reload() t_fans* fan = (t_fans *) malloc( sizeof( t_fans ) ); fan->fan_id = 1; fan->fan_min_speed = -1; + fan->fan_manual_path = NULL; + fan->fan_output_path = NULL; + fan->label = NULL; fan->next = NULL; signal(SIGHUP, handler); @@ -207,7 +234,7 @@ static const char *test_settings_reload() mu_assert("min_fan_speed value is not 6200 after SIGHUP", fan->fan_min_speed == 6200); mu_assert("polling_interval is not 2 after SIGHUP", polling_interval == 2); retrieve_settings("./mbpfan.conf", fan); - free(fan); + free_fans(fan); return 0; } From 1243d7df6c9e6e655460f811c18f336ad404d5b8 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 20:59:08 -0700 Subject: [PATCH 204/235] Update unit tests for fan speed auto-detection Follows on to 716d9d877a08e0bce0b9a5cf7222ff3a4829e66. References #190. --- src/minunit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/minunit.c b/src/minunit.c index a9aedb9..5934ffb 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -231,7 +231,7 @@ static const char *test_settings_reload() // cannot tests min_fan_speed since it is not set and thus auto-detected mu_assert("polling_interval is not 1 before SIGHUP", polling_interval == 1); raise(SIGHUP); - mu_assert("min_fan_speed value is not 6200 after SIGHUP", fan->fan_min_speed == 6200); + // cannot tests min_fan_speed since it is not set and thus auto-detected mu_assert("polling_interval is not 2 after SIGHUP", polling_interval == 2); retrieve_settings("./mbpfan.conf", fan); free_fans(fan); From 0ae57217f7b0e449ea029b1fd3c3475075f46178 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 30 Oct 2019 21:04:52 -0700 Subject: [PATCH 205/235] Emit version number on startup Also configure syslog before logging. References #163. --- src/daemon.c | 4 ++-- src/global.h | 1 + src/main.c | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 7ce03fd..38104ac 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -147,8 +147,6 @@ void go_daemon(void (*fan_control)()) signal(SIGQUIT, signal_handler); signal(SIGINT, signal_handler); - syslog(LOG_INFO, "%s starting up", PROGRAM_NAME); - // Setup syslog logging - see SETLOGMASK(3) if(verbose) { setlogmask(LOG_UPTO(LOG_DEBUG)); @@ -159,6 +157,8 @@ void go_daemon(void (*fan_control)()) openlog(PROGRAM_NAME, LOG_CONS, LOG_USER); } + mbp_log(LOG_INFO, "%s %s starting up", PROGRAM_NAME, PROGRAM_VERSION); + // configure timer slack int err = prctl(PR_SET_TIMERSLACK, 1000 * 1000 * 1000, 0, 0, 0); if (err == -1) { diff --git a/src/global.h b/src/global.h index 5cd41eb..d524407 100644 --- a/src/global.h +++ b/src/global.h @@ -5,6 +5,7 @@ extern int daemonize; extern int verbose; extern const char* PROGRAM_NAME; +extern const char* PROGRAM_VERSION; extern const char* PROGRAM_PID; struct s_sensors { diff --git a/src/main.c b/src/main.c index df22318..2b797c9 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,7 @@ int daemonize = 1; int verbose = 0; const char *PROGRAM_NAME = "mbpfan"; +const char *PROGRAM_VERSION = "2.2.0"; const char *PROGRAM_PID = "/var/run/mbpfan.pid"; const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0"; From 29eeec18f50dc6db1e2ccbd3978213cd762c0e4c Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 1 Nov 2019 12:21:59 -0700 Subject: [PATCH 206/235] Prefer HTTPS where possible Also remove broken link. --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 7224def..fa219d0 100644 --- a/README.md +++ b/README.md @@ -248,8 +248,7 @@ GNU General Public License version 3 * http://allanmcrae.com/2010/05/simple-macbook-pro-fan-daemon/ * http://allanmcrae.com/2011/08/mbp-fan-daemon-update/ * https://launchpad.net/macfanctld -* http://paste2.org/p/862259 -* http://www.lobotomo.com/products/FanControl/ +* https://www.lobotomo.com/products/FanControl/ ## Credits From b21aaa832ccb3e15a908d93b079fba7b66b82dcf Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 1 Nov 2019 12:23:34 -0700 Subject: [PATCH 207/235] Reparent links into credits section --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index fa219d0..9ba4b1b 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,7 @@ execute the following (as root): GNU General Public License version 3 +## Credits **This Project Is Based On:** @@ -250,8 +251,6 @@ GNU General Public License version 3 * https://launchpad.net/macfanctld * https://www.lobotomo.com/products/FanControl/ -## Credits - **This Project uses following library:** * [ANSI C Application Settings Management](http://pokristensson.com/settings.html) by Per Ola Kristensson. From 0a9f03e6150affc8ed170076b42146ebc6fd579a Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 24 Nov 2019 13:51:28 +0900 Subject: [PATCH 208/235] Annotate mbp_log as having printf parameters --- src/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.h b/src/util.h index 1f3819e..9bf99ce 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,6 @@ #ifndef _UTIL_H_ #define _UTIL_H_ -void mbp_log(int level, const char *fmt, ...); +void mbp_log(int level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); #endif From 7ac2eb01fc3ba2c85f8340693a679e3ecea5318b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 24 Nov 2019 13:51:49 +0900 Subject: [PATCH 209/235] Do not resuse va_list without reinitialization Fixes #206. --- src/util.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util.c b/src/util.c index 73600c4..3022632 100644 --- a/src/util.c +++ b/src/util.c @@ -12,6 +12,9 @@ void mbp_log(int level, const char *fmt, ...) if (daemonize) { vsyslog(level, fmt, args); } + va_end(args); + + va_start(args, fmt); vprintf(fmt, args); puts(""); // trailing newline va_end(args); From 49f544fd8d596fa13d5525a5b042eee311568c67 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 24 Nov 2019 16:09:31 +0900 Subject: [PATCH 210/235] Only call va_start when needed --- src/util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util.c b/src/util.c index 3022632..136bcc6 100644 --- a/src/util.c +++ b/src/util.c @@ -8,11 +8,11 @@ void mbp_log(int level, const char *fmt, ...) { va_list args; - va_start(args, fmt); if (daemonize) { + va_start(args, fmt); vsyslog(level, fmt, args); + va_end(args); } - va_end(args); va_start(args, fmt); vprintf(fmt, args); From dd4ed658dac938bc22a5321d5906022e7919861f Mon Sep 17 00:00:00 2001 From: Lyes Saadi <33597258+LyesSaadi@users.noreply.github.com> Date: Sun, 12 Jan 2020 07:09:52 +0100 Subject: [PATCH 211/235] Offcial Fedora Package #181 --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ba4b1b..654d457 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,9 @@ sudo apt-get install mbpfan ### Fedora -There is a [Fedora COPR](https://copr.fedorainfracloud.org/coprs/lyessaadi/mbpfan/) to install mbpfan until the package is submitted upstream. +On Fedora 30 or later: ``` -sudo dnf copr enable lyessaadi/mbpfan sudo dnf install mbpfan ``` From bc71232af281e73f9c47d1b45fcb6ae284daf480 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 23 Apr 2020 19:38:21 +0900 Subject: [PATCH 212/235] Add AskUbuntu reference Also use consistent heading. --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 654d457..f34d8b0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -mbpfan -==================== +# mbpfan [![Build Status](https://travis-ci.org/dgraziotin/mbpfan.svg?branch=master)](https://travis-ci.org/dgraziotin/mbpfan) @@ -32,6 +31,7 @@ This enhanced version assumes any number of processors and fans (max. 10). - [Run Instructions](#run-instructions) - [Starting at boot](#starting-at-boot) - [Usage](#usage) +- [References](#references) - [License](#license) - [Credits](#credits) @@ -236,6 +236,9 @@ execute the following (as root): -t Run the tests -v Be (a lot) verbose +## References + +* [mbpfan on Ask Ubuntu](https://askubuntu.com/search?q=mbpfan+is%3Aquestion) ## License From 10d6febeb485539ed4be09f584f74f861df10299 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 30 Apr 2020 19:25:56 +0900 Subject: [PATCH 213/235] Use smaller max_fan_speed in tests This should allow iMacs to complete tests successfully. Fixes #212. Fixes #215. --- mbpfan.conf.test1 | 4 ++-- src/minunit.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mbpfan.conf.test1 b/mbpfan.conf.test1 index bea4137..5f098b7 100644 --- a/mbpfan.conf.test1 +++ b/mbpfan.conf.test1 @@ -1,6 +1,6 @@ [general] -min_fan1_speed = 6200 # default is 2000 -max_fan1_speed = 6200 # default is 6200 +min_fan1_speed = 2000 # default is 2000 +max_fan1_speed = 2600 # default is 2600 low_temp = 63 # try ranges 55-63, default is 63 high_temp = 66 # try ranges 58-66, default is 66 max_temp = 86 # do not set it > 90, default is 86 diff --git a/src/minunit.c b/src/minunit.c index 5934ffb..a68908c 100644 --- a/src/minunit.c +++ b/src/minunit.c @@ -159,7 +159,8 @@ static const char *test_settings() fan->next = NULL; retrieve_settings("./mbpfan.conf.test1", fan); - mu_assert("max_fan_speed value is not 6200", fan->fan_max_speed == 6200); + // choosing the maximum for iMac mid 2011 + mu_assert("max_fan_speed value is not 2600", fan->fan_max_speed == 2600); mu_assert("polling_interval is not 2", polling_interval == 2); fan->fan_min_speed = -1; From 52d897374d25fc7fc1d34425b93958aa583a0c64 Mon Sep 17 00:00:00 2001 From: localscope <41133098+localscope@users.noreply.github.com> Date: Tue, 19 May 2020 08:06:27 -0600 Subject: [PATCH 214/235] Moved tests to separate binary (mbpfan-tests) Removed instructions for using -t flag Moved check_requirements function from main to mbpfan Moved daemonize and verbose globals from main to daemon Moved strings for program information from main to global.h Moved strings for coretemp and applesmc paths from main to mbpfan.c Updated Makefile Turned on verboseness for testing --- Makefile | 20 +++++++++---- README.md | 7 +---- mbpfan.8.gz | Bin 595 -> 587 bytes src/daemon.c | 3 ++ src/daemon.h | 2 +- src/global.h | 8 ++--- src/main.c | 61 ++------------------------------------- src/main.h | 1 - src/mbpfan.c | 44 ++++++++++++++++++++++++++++ src/mbpfan.h | 6 ++++ tests/main.c | 7 +++++ {src => tests}/minunit.c | 13 +++++---- {src => tests}/minunit.h | 0 13 files changed, 90 insertions(+), 82 deletions(-) delete mode 100644 src/main.h create mode 100644 tests/main.c rename {src => tests}/minunit.c (97%) rename {src => tests}/minunit.h (100%) diff --git a/Makefile b/Makefile index d5223ae..5c466b8 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ C = c OBJ = o OUTPUT_PATH = bin/ SOURCE_PATH = src/ +TESTS_PATH = tests/ +TESTS_BIN = bin/mbpfan-tests BIN = bin/mbpfan CONF = mbpfan.conf DOC = README.md @@ -20,24 +22,32 @@ CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_ LDFLAGS += $(LIBPATH) -g OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) +TESTS_OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(TESTS_PATH)*.$(C))) +TESTS_OBJS += $(filter-out %main.$(OBJ),$(OBJS)) + +.PHONY: all clean tests uninstall install rebuild %.$(OBJ):%.$(C) mkdir -p bin @echo Compiling $(basename $<)... $(CC) -c $(CFLAGS) $< $(OBJFLAG)$@ -all: $(BIN) +all: $(BIN) $(TESTS_BIN) $(BIN): $(OBJS) @echo Linking... $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(BIN) +$(TESTS_BIN): $(TESTS_OBJS) + @echo Linking... + $(CC) $(LDFLAGS) $^ $(LIBS) $(BINFLAG) $(TESTS_BIN) + clean: rm -rf $(SOURCE_PATH)*.$(OBJ) $(BIN) + rm -rf $(TESTS_PATH)*.$(OBJ) $(TESTS_BIN) -tests: - make install - /usr/sbin/mbpfan -f -v -t +tests: all + ./bin/mbpfan-tests uninstall: rm /usr/sbin/mbpfan @@ -46,7 +56,7 @@ uninstall: rm /usr/share/man/man8/mbpfan.8.gz rm -rf /usr/share/doc/mbpfan -install: $(BIN) +install: all install -d $(DESTDIR)/usr/sbin install -d $(DESTDIR)/etc install -d $(DESTDIR)/lib/systemd/system diff --git a/README.md b/README.md index f34d8b0..5852fbd 100644 --- a/README.md +++ b/README.md @@ -155,11 +155,7 @@ default compiler to be Clang. Tested with Clang 3.8 and 3.9. Tested with Clang Run The Tests (Optional) ------------------------ -Users may run the tests after installing the program. Please run the following command _from within the source directory_. - - sudo ./bin/mbpfan -t - -or +Users may run the tests after building the program. Please run the following command _from within the source directory_. sudo make tests @@ -233,7 +229,6 @@ execute the following (as root): -h Show the help screen -f Run in foreground - -t Run the tests -v Be (a lot) verbose ## References diff --git a/mbpfan.8.gz b/mbpfan.8.gz index 8b97c4ca3afba97ad33137bebb1a14af89e91f9d..76146d0f4c1f0f24343bf3d5fd5b2693d4f0c3d5 100644 GIT binary patch delta 534 zcmV+x0_pwJ1j_^nABzYGpJBsZkqAP69hS4RGqbDg?hFpr)aV@ePC961kMJmc$1-|4 zS<|+_mC(SC!qrlOs)knRY!TmCcD#Y@>Ttc>!6lsCI<&AAI*8uUTX_S!<#x@+4!Gk0 zOK?&RG@%iwj0SI@HriX$Lxi{iu>q=!zK7)yfwfUX@Ms^v;NPFa4smCCG->M zpel-!{0-xNjCE-e4RA11;%5ebi~0GRRFfWvuaSQXrCp|QvfX6 z9DsMM?95}5*kM0_t1Y4)-&$4#O|C~%XO)7nonldKtwnhAyTDJRQ4V&Hdo^{?2?tPx0=#E20AeA?#e-x9foV~zaQ{@+z z7nix<6`rKlv={D!H&%+CQwV(Qyn23l+#6wiI6_`f8oiFD3{9QhtJpDv< YWz0RNb@IqDUReGYX_Ds!41xmy0CiOj%m4rY delta 526 zcmV+p0`dLJ1k(fvABzYGMu^5&kqANt9%f%Nv&)fD6MqVwtZ7@|N@(Cm;c6*CRYNOu zwuo_-9dBT}I$SSza0zF(4lQhj4x)DyD{o-8+^*SJ0e2i=2~MhkCNu(-(clf#Mtf^| zh$(JBY=Cgl_plsdVr|qAJX!~Te*K0S>Q3mIk{$?>lxbR6_C3{Z>q6kf?mP`IzwP#i za#ONwHGkpoeSbJ^_Pg>&O1N5=pNmaGUga=yLA(&`goohJfz-qUT5A+>vglC_piC2p zqtFdF^wB|lr6V}0PYDxIic|GekO4bJum2MxoC}I(3H<~*sEX_)eZ#mPV_lj=102kh z_?f|Ce*Pw5(gXEtq$4YNLhjLO?77qp4oHvoS%1wR2he9bI0d2yKwUg1e< zP0Qjwcw?pLIXT0}T2Ahu<)I4Nhp7?u5%D@zZ3U*0Z|947K7W^w|A2m0T^V!FX~#VF Q7|$;M3!aNDCzk^N03R3lTL1t6 diff --git a/src/daemon.c b/src/daemon.c index 38104ac..3f933e8 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -35,6 +35,9 @@ #include "daemon.h" #include "util.h" +int daemonize = 1; +int verbose = 0; + int write_pid(int pid) { FILE *file = NULL; diff --git a/src/daemon.h b/src/daemon.h index dbbc17f..c52c4cc 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -51,4 +51,4 @@ void signal_handler(int signal); void go_daemon(void (*function)()); -#endif \ No newline at end of file +#endif diff --git a/src/global.h b/src/global.h index d524407..4c32551 100644 --- a/src/global.h +++ b/src/global.h @@ -1,13 +1,13 @@ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ +#define PROGRAM_NAME "mbpfan" +#define PROGRAM_VERSION "2.2.0" +#define PROGRAM_PID "/var/run/mbpfan.pid" + extern int daemonize; extern int verbose; -extern const char* PROGRAM_NAME; -extern const char* PROGRAM_VERSION; -extern const char* PROGRAM_PID; - struct s_sensors { FILE* file; char* path; diff --git a/src/main.c b/src/main.c index 2b797c9..41bc86f 100644 --- a/src/main.c +++ b/src/main.c @@ -23,26 +23,12 @@ #include #include #include -#include -#include #include #include "mbpfan.h" #include "daemon.h" #include "global.h" -#include "main.h" -#include "minunit.h" #include "util.h" -int daemonize = 1; -int verbose = 0; - -const char *PROGRAM_NAME = "mbpfan"; -const char *PROGRAM_VERSION = "2.2.0"; -const char *PROGRAM_PID = "/var/run/mbpfan.pid"; - -const char *CORETEMP_PATH = "/sys/devices/platform/coretemp.0"; -const char *APPLESMC_PATH = "/sys/devices/platform/applesmc.768"; - void print_usage(int argc, char *argv[]) { if (argc >=1) { @@ -50,57 +36,17 @@ void print_usage(int argc, char *argv[]) printf("Options:\n"); printf("\t-h Show this help screen\n"); printf("\t-f Run in foreground\n"); - printf("\t-t Run the tests\n"); printf("\t-v Be (a lot) verbose\n"); printf("\n"); } } -void check_requirements() -{ - - /** - * Check for root - */ - - uid_t uid=getuid(), euid=geteuid(); - - if (uid != 0 || euid != 0) { - mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - /** - * Check for coretemp and applesmc modules - */ - DIR* dir = opendir(CORETEMP_PATH); - - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); - - - dir = opendir(APPLESMC_PATH); - - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME); - exit(EXIT_FAILURE); - } - - closedir(dir); - - -} - int main(int argc, char *argv[]) { int c; - while( (c = getopt(argc, argv, "hftv|help")) != -1) { + while( (c = getopt(argc, argv, "hfv|help")) != -1) { switch(c) { case 'h': print_usage(argc, argv); @@ -111,9 +57,6 @@ int main(int argc, char *argv[]) daemonize = 0; break; - case 't': - return tests(); - case 'v': verbose = 1; break; @@ -125,7 +68,7 @@ int main(int argc, char *argv[]) } } - check_requirements(); + check_requirements(argv[0]); // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 13013e8..0000000 --- a/src/main.h +++ /dev/null @@ -1 +0,0 @@ -void check_requirements(); diff --git a/src/mbpfan.c b/src/mbpfan.c index 129c708..1916d64 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include #include "mbpfan.h" @@ -48,6 +50,9 @@ #define min(a,b) ((a) < (b) ? (a) : (b)) #define max(a,b) ((a) > (b) ? (a) : (b)) +#define CORETEMP_PATH "/sys/devices/platform/coretemp.0" +#define APPLESMC_PATH "/sys/devices/platform/applesmc.768" + /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature @@ -525,6 +530,45 @@ void retrieve_settings(const char* settings_path, t_fans* fans) } } +void check_requirements(const char* program_path) +{ + + /** + * Check for root + */ + + uid_t uid=getuid(), euid=geteuid(); + + if (uid != 0 || euid != 0) { + mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path); + exit(EXIT_FAILURE); + } + + /** + * Check for coretemp and applesmc modules + */ + DIR* dir = opendir(CORETEMP_PATH); + + if (ENOENT == errno) { + mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); + exit(EXIT_FAILURE); + } + + closedir(dir); + + + dir = opendir(APPLESMC_PATH); + + if (ENOENT == errno) { + mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path); + exit(EXIT_FAILURE); + } + + closedir(dir); + + +} + void mbpfan() { int old_temp, new_temp, fan_speed, steps; diff --git a/src/mbpfan.h b/src/mbpfan.h index ce9aa8c..f673a58 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -100,6 +100,12 @@ void set_fan_minimum_speed(t_fans* fans); */ unsigned short get_temp(t_sensors* sensors); +/** + * Check if user has proper access and that required + * kernel modules are available + */ +void check_requirements(const char* program_path); + /** * Main Program */ diff --git a/tests/main.c b/tests/main.c new file mode 100644 index 0000000..2870554 --- /dev/null +++ b/tests/main.c @@ -0,0 +1,7 @@ +#include "minunit.h" + +int main(int argc, char *argv[]) +{ + (void)argc; + tests(argv[0]); +} diff --git a/src/minunit.c b/tests/minunit.c similarity index 97% rename from src/minunit.c rename to tests/minunit.c index a68908c..5a54299 100644 --- a/src/minunit.c +++ b/tests/minunit.c @@ -6,10 +6,9 @@ #include #include #include -#include "global.h" -#include "mbpfan.h" -#include "settings.h" -#include "main.h" +#include "../src/global.h" +#include "../src/mbpfan.h" +#include "../src/settings.h" #include "minunit.h" int tests_run = 0; @@ -252,9 +251,11 @@ static const char *all_tests() return 0; } -int tests() +int tests(const char *program_path) { - check_requirements(); + verbose = 1; + + check_requirements(program_path); printf("Starting the tests..\n"); printf("It is normal for them to take a bit to finish.\n"); diff --git a/src/minunit.h b/tests/minunit.h similarity index 100% rename from src/minunit.h rename to tests/minunit.h From 814af65b363bd1b02a94e13672959ca8412ee7b4 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 28 Feb 2021 20:48:20 +0900 Subject: [PATCH 215/235] Log maximum MHz of all CPUs Useful for monitoring intel_pstate effects. --- src/mbpfan.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 1916d64..a06880c 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -569,6 +569,28 @@ void check_requirements(const char* program_path) } +int get_max_mhz(void) +{ + int max_mhz = -1; + DIR *dir = opendir("/sys/devices/system/cpu"); + if (dir == NULL) { + return -1; + } + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (strncmp(ent->d_name, "cpu", 3) != 0 || + strcmp(ent->d_name, "cpufreq") == 0 || + strcmp(ent->d_name, "cpuidle") == 0) { + continue; + } + char *path = smprintf("/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", ent->d_name); + max_mhz = max(max_mhz, read_value(path) / 1000); + free(path); + } + closedir(dir); + return max_mhz; +} + void mbpfan() { int old_temp, new_temp, fan_speed, steps; @@ -646,7 +668,7 @@ void mbpfan() } if(verbose) { - mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed); + mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", old_temp, new_temp, fan->label, fan_speed, get_max_mhz()); } set_fan_speed(fan, fan_speed); From 16f28f900c0b769baa8cf60207be867382d95150 Mon Sep 17 00:00:00 2001 From: Chow Loong Jin Date: Tue, 2 Mar 2021 19:58:50 +0800 Subject: [PATCH 216/235] Improve comments in the config around {low,high,max}_temp settings --- mbpfan.conf | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mbpfan.conf b/mbpfan.conf index 39d4b57..c2a9caf 100644 --- a/mbpfan.conf +++ b/mbpfan.conf @@ -12,7 +12,9 @@ # #min_fan1_speed = 2000 # put the *lowest* value of "cat /sys/devices/platform/applesmc.768/fan*_min" #max_fan1_speed = 6200 # put the *highest* value of "cat /sys/devices/platform/applesmc.768/fan*_max" -low_temp = 63 # try ranges 55-63, default is 63 -high_temp = 66 # try ranges 58-66, default is 66 -max_temp = 86 # take highest number returned by "cat /sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_max", divide by 1000 + +# temperature units in celcius +low_temp = 63 # if temperature is below this, fans will run at minimum speed +high_temp = 66 # if temperature is above this, fan speed will gradually increase +max_temp = 86 # if temperature is above this, fans will run at maximum speed polling_interval = 1 # default is 1 seconds From 623f44d0a58ac67c80244ca972564131cd45a7ed Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 12 Apr 2021 22:05:15 +0900 Subject: [PATCH 217/235] Reformat source code with clang-tidy Run with: clang-format -i src/* Closes #233. --- src/daemon.c | 80 ++++++------ src/daemon.h | 1 - src/global.h | 18 +-- src/main.c | 6 +- src/mbpfan.c | 323 ++++++++++++++++++++++++------------------------- src/mbpfan.h | 14 +-- src/settings.c | 38 +++--- src/settings.h | 11 +- src/strmap.c | 24 ++-- src/strmap.h | 9 +- src/util.c | 2 +- src/util.h | 2 +- 12 files changed, 255 insertions(+), 273 deletions(-) diff --git a/src/daemon.c b/src/daemon.c index 3f933e8..760f512 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -16,7 +16,6 @@ * */ - #include #include #include @@ -43,7 +42,7 @@ int write_pid(int pid) FILE *file = NULL; file = fopen(PROGRAM_PID, "w"); - if(file != NULL) { + if (file != NULL) { fprintf(file, "%d", pid); fclose(file); return 1; @@ -59,14 +58,13 @@ int read_pid() int pid = -1; file = fopen(PROGRAM_PID, "r"); - if(file != NULL) { + if (file != NULL) { fscanf(file, "%d", &pid); fclose(file); - if (kill(pid, 0) == -1 && errno == ESRCH) - { /* a process with such a pid does not exist, remove the pid file */ - if (remove(PROGRAM_PID) == 0) { - return -1; - } + if (kill(pid, 0) == -1 && errno == ESRCH) { /* a process with such a pid does not exist, remove the pid file */ + if (remove(PROGRAM_PID) == 0) { + return -1; + } } return pid; } @@ -81,40 +79,40 @@ int delete_pid() static void cleanup_and_exit(int exit_code) { - delete_pid(); - set_fans_auto(fans); - - struct s_fans *next_fan; - while (fans != NULL) { - next_fan = fans->next; - if (fans->file != NULL) { - fclose(fans->file); - } - free(fans->label); - free(fans->fan_output_path); - free(fans->fan_manual_path); - free(fans); - fans = next_fan; - } - - struct s_sensors *next_sensor; - while (sensors != NULL) { - next_sensor = sensors->next; - if (sensors->file != NULL) { - fclose(sensors->file); - } - free(sensors->path); - free(sensors); - sensors = next_sensor; - } - - exit(exit_code); + delete_pid(); + set_fans_auto(fans); + + struct s_fans *next_fan; + while (fans != NULL) { + next_fan = fans->next; + if (fans->file != NULL) { + fclose(fans->file); + } + free(fans->label); + free(fans->fan_output_path); + free(fans->fan_manual_path); + free(fans); + fans = next_fan; + } + + struct s_sensors *next_sensor; + while (sensors != NULL) { + next_sensor = sensors->next; + if (sensors->file != NULL) { + fclose(sensors->file); + } + free(sensors->path); + free(sensors); + sensors = next_sensor; + } + + exit(exit_code); } void signal_handler(int signal) { - switch(signal) { + switch (signal) { case SIGHUP: syslog(LOG_WARNING, "Received SIGHUP signal."); retrieve_settings(NULL, fans); @@ -151,7 +149,7 @@ void go_daemon(void (*fan_control)()) signal(SIGINT, signal_handler); // Setup syslog logging - see SETLOGMASK(3) - if(verbose) { + if (verbose) { setlogmask(LOG_UPTO(LOG_DEBUG)); openlog(PROGRAM_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); @@ -198,15 +196,12 @@ void go_daemon(void (*fan_control)()) exit(EXIT_FAILURE); } - - /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } - int current_pid = getpid(); if (read_pid() == -1) { @@ -229,10 +224,9 @@ void go_daemon(void (*fan_control)()) exit(EXIT_FAILURE); } - fan_control(); - if(daemonize) { + if (daemonize) { syslog(LOG_INFO, "%s daemon exiting", PROGRAM_NAME); } } diff --git a/src/daemon.h b/src/daemon.h index c52c4cc..cb26450 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -50,5 +50,4 @@ void signal_handler(int signal); */ void go_daemon(void (*function)()); - #endif diff --git a/src/global.h b/src/global.h index 4c32551..6eaea36 100644 --- a/src/global.h +++ b/src/global.h @@ -9,18 +9,18 @@ extern int daemonize; extern int verbose; struct s_sensors { - FILE* file; - char* path; + FILE *file; + char *path; unsigned int temperature; struct s_sensors *next; }; struct s_fans { - FILE* file; - char* path; // TODO: unused - char* label; - char* fan_output_path; - char* fan_manual_path; + FILE *file; + char *path; // TODO: unused + char *label; + char *fan_output_path; + char *fan_manual_path; int step_up; int step_down; int fan_id; @@ -33,7 +33,7 @@ struct s_fans { typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; -extern t_sensors* sensors; -extern t_fans* fans; +extern t_sensors *sensors; +extern t_fans *fans; #endif diff --git a/src/main.c b/src/main.c index 41bc86f..7e5f1d6 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ void print_usage(int argc, char *argv[]) { - if (argc >=1) { + if (argc >= 1) { printf("Usage: %s OPTION(S) \n", argv[0]); printf("Options:\n"); printf("\t-h Show this help screen\n"); @@ -46,8 +46,8 @@ int main(int argc, char *argv[]) int c; - while( (c = getopt(argc, argv, "hfv|help")) != -1) { - switch(c) { + while ((c = getopt(argc, argv, "hfv|help")) != -1) { + switch (c) { case 'h': print_usage(argc, argv); exit(EXIT_SUCCESS); diff --git a/src/mbpfan.c b/src/mbpfan.c index a06880c..01df2f9 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -47,8 +47,8 @@ #include "util.h" /* lazy min/max... */ -#define min(a,b) ((a) < (b) ? (a) : (b)) -#define max(a,b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) #define CORETEMP_PATH "/sys/devices/platform/coretemp.0" #define APPLESMC_PATH "/sys/devices/platform/applesmc.768" @@ -57,9 +57,9 @@ * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature * max_temp - fan will run at full speed above this temperature */ -int low_temp = 63; // try ranges 55-63 -int high_temp = 66; // try ranges 58-66 -int max_temp = 86; // do not set it > 90 +int low_temp = 63; // try ranges 55-63 +int high_temp = 66; // try ranges 58-66 +int max_temp = 86; // do not set it > 90 // maximum number of processors etc supported #define NUM_PROCESSORS 6 @@ -72,8 +72,8 @@ int max_temp = 86; // do not set it > 90 int polling_interval = 1; -t_sensors* sensors = NULL; -t_fans* fans = NULL; +t_sensors *sensors = NULL; +t_fans *fans = NULL; char *smprintf(const char *fmt, ...) { @@ -105,7 +105,7 @@ bool is_modern_sensors_path() char *str_kernel_version; str_kernel_version = strtok(kernel.release, "."); - if (atoi(str_kernel_version) < 3){ + if (atoi(str_kernel_version) < 3) { mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release); exit(EXIT_FAILURE); } @@ -127,7 +127,6 @@ bool is_modern_sensors_path() return 0; } - t_sensors *retrieve_sensors() { t_sensors *sensors_head = NULL; @@ -140,7 +139,7 @@ t_sensors *retrieve_sensors() int sensors_found = 0; if (!is_modern_sensors_path()) { - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0"); } @@ -148,82 +147,82 @@ t_sensors *retrieve_sensors() } else { - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 "); } - // loop over up to 6 processors - int processor; - for (processor = 0; processor < NUM_PROCESSORS; processor++) { + // loop over up to 6 processors + int processor; + for (processor = 0; processor < NUM_PROCESSORS; processor++) { - if (path_begin != NULL) { - free(path_begin); - } - path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); + if (path_begin != NULL) { + free(path_begin); + } + path_begin = smprintf("/sys/devices/platform/coretemp.%d/hwmon/hwmon", processor); - int counter; - for (counter = 0; counter < NUM_HWMONS; counter++) { + int counter; + for (counter = 0; counter < NUM_HWMONS; counter++) { char *hwmon_path = smprintf("%s%d", path_begin, counter); - int res = access(hwmon_path, R_OK); - if (res == 0) { + int res = access(hwmon_path, R_OK); + if (res == 0) { - free(path_begin); - path_begin = smprintf("%s/temp", hwmon_path); + free(path_begin); + path_begin = smprintf("%s/temp", hwmon_path); - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin); - } + } free(hwmon_path); - break; - } + break; + } free(hwmon_path); - } + } - int core = 0; - for(core = 0; corepath = strdup(path); - fscanf(file, "%d", &s->temperature); + if (file != NULL) { + s = (t_sensors *)malloc(sizeof(t_sensors)); + s->path = strdup(path); + fscanf(file, "%d", &s->temperature); - if (sensors_head == NULL) { - sensors_head = s; - sensors_head->next = NULL; + if (sensors_head == NULL) { + sensors_head = s; + sensors_head->next = NULL; - } else { - t_sensors *tmp = sensors_head; + } else { + t_sensors *tmp = sensors_head; - while (tmp->next != NULL) { - tmp = tmp->next; - } + while (tmp->next != NULL) { + tmp = tmp->next; + } - tmp->next = s; - tmp->next->next = NULL; - } + tmp->next = s; + tmp->next->next = NULL; + } - s->file = file; - sensors_found++; - } + s->file = file; + sensors_found++; + } - free(path); - path = NULL; - } - } + free(path); + path = NULL; + } + } } - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Found %d sensors", sensors_found); } - if (sensors_found == 0){ + if (sensors_found == 0) { mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer."); exit(EXIT_FAILURE); } @@ -280,37 +279,37 @@ t_fans *retrieve_fans() const char *path_man_end = "_manual"; const char *path_max_speed = "_max"; const char *path_min_speed = "_min"; - + int counter = 0; int fans_found = 0; - for(counter = 0; counterfan_output_path = strdup(path_output); fan->fan_manual_path = strdup(path_manual); - fan->fan_id = counter; + fan->fan_id = counter; - int fan_speed = read_value(path_fan_min); - if(fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) - fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; - else - fan->fan_min_speed = fan_speed; + int fan_speed = read_value(path_fan_min); + if (fan_speed == -1 || fan_speed < MIN_FAN_SPEED_DEFAULT) + fan->fan_min_speed = MIN_FAN_SPEED_DEFAULT; + else + fan->fan_min_speed = fan_speed; - fan_speed = read_value(path_fan_max); - if(fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) - fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; - else - fan->fan_max_speed = fan_speed; + fan_speed = read_value(path_fan_max); + if (fan_speed == -1 || fan_speed > MAX_FAN_SPEED_DEFAULT) + fan->fan_max_speed = MAX_FAN_SPEED_DEFAULT; + else + fan->fan_max_speed = fan_speed; size_t max_label_len = 64; fan->label = malloc(max_label_len); @@ -337,23 +336,23 @@ t_fans *retrieve_fans() fan->file = file; fans_found++; } - free(path_fan_min); - path_fan_min = NULL; - free(path_label); - path_label = NULL; - free(path_fan_max); - path_fan_max = NULL; + free(path_fan_min); + path_fan_min = NULL; + free(path_label); + path_label = NULL; + free(path_fan_max); + path_fan_max = NULL; free(path_output); path_output = NULL; free(path_manual); path_manual = NULL; } - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Found %d fans", fans_found); } - if (fans_found == 0){ + if (fans_found == 0) { mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer."); exit(EXIT_FAILURE); } @@ -366,10 +365,10 @@ static void set_fans_mode(t_fans *fans, int mode) t_fans *tmp = fans; FILE *file; - while(tmp != NULL) { + while (tmp != NULL) { file = fopen(tmp->fan_manual_path, "rw+"); - if(file != NULL) { + if (file != NULL) { fprintf(file, "%d", mode); fclose(file); } @@ -394,10 +393,10 @@ t_sensors *refresh_sensors(t_sensors *sensors) { t_sensors *tmp = sensors; - while(tmp != NULL) { - if(tmp->file != NULL) { + while (tmp != NULL) { + if (tmp->file != NULL) { char buf[16]; - int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/ 0); + int len = pread(fileno(tmp->file), buf, sizeof(buf), /*offset=*/0); buf[len] = '\0'; tmp->temperature = strtod(buf, NULL); } @@ -409,36 +408,36 @@ t_sensors *refresh_sensors(t_sensors *sensors) } /* Controls the speed of a fan */ -void set_fan_speed(t_fans* fan, int speed) +void set_fan_speed(t_fans *fan, int speed) { - if(fan != NULL && fan->file != NULL && fan->old_speed != speed) { - char buf[16]; - int len = snprintf(buf, sizeof(buf), "%d", speed); - int res = pwrite(fileno(fan->file), buf, len, /*offset=*/ 0); - if (res == -1) { - perror("Could not set fan speed"); - } - fan->old_speed = speed; + if (fan != NULL && fan->file != NULL && fan->old_speed != speed) { + char buf[16]; + int len = snprintf(buf, sizeof(buf), "%d", speed); + int res = pwrite(fileno(fan->file), buf, len, /*offset=*/0); + if (res == -1) { + perror("Could not set fan speed"); + } + fan->old_speed = speed; } } -void set_fan_minimum_speed(t_fans* fans) +void set_fan_minimum_speed(t_fans *fans) { - t_fans *tmp = fans; + t_fans *tmp = fans; - while(tmp != NULL) { - set_fan_speed(tmp,tmp->fan_min_speed); - tmp = tmp->next; - } + while (tmp != NULL) { + set_fan_speed(tmp, tmp->fan_min_speed); + tmp = tmp->next; + } } -unsigned short get_temp(t_sensors* sensors) +unsigned short get_temp(t_sensors *sensors) { sensors = refresh_sensors(sensors); unsigned int temp = 0; - t_sensors* tmp = sensors; + t_sensors *tmp = sensors; - while(tmp != NULL) { + while (tmp != NULL) { temp = max(temp, tmp->temperature); tmp = tmp->next; } @@ -446,7 +445,7 @@ unsigned short get_temp(t_sensors* sensors) return temp / 1000; } -void retrieve_settings(const char* settings_path, t_fans* fans) +void retrieve_settings(const char *settings_path, t_fans *fans) { Settings *settings = NULL; int result = 0; @@ -459,10 +458,9 @@ void retrieve_settings(const char* settings_path, t_fans* fans) f = fopen(settings_path, "r"); } - if (f == NULL) { /* Could not open configfile */ - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Couldn't open configfile, using defaults"); } @@ -472,34 +470,34 @@ void retrieve_settings(const char* settings_path, t_fans* fans) if (settings == NULL) { /* Could not read configfile */ - if(verbose) { + if (verbose) { mbp_log(LOG_WARNING, "Couldn't read configfile"); } } else { - - t_fans *fan = fans; - while(fan != NULL) { + t_fans *fan = fans; - char* config_key; - config_key = smprintf("min_fan%d_speed", fan->fan_id); + while (fan != NULL) { + + char *config_key; + config_key = smprintf("min_fan%d_speed", fan->fan_id); /* Read configfile values */ result = settings_get_int(settings, "general", config_key); if (result != 0) { - fan->fan_min_speed = result; + fan->fan_min_speed = result; } - free(config_key); - - config_key = smprintf("max_fan%d_speed", fan->fan_id); + free(config_key); + + config_key = smprintf("max_fan%d_speed", fan->fan_id); result = settings_get_int(settings, "general", config_key); if (result != 0) { - fan->fan_max_speed = result; + fan->fan_max_speed = result; } - free(config_key); - fan = fan->next; - } + free(config_key); + fan = fan->next; + } result = settings_get_int(settings, "general", "low_temp"); if (result != 0) { @@ -530,14 +528,14 @@ void retrieve_settings(const char* settings_path, t_fans* fans) } } -void check_requirements(const char* program_path) +void check_requirements(const char *program_path) { /** * Check for root */ - uid_t uid=getuid(), euid=geteuid(); + uid_t uid = getuid(), euid = geteuid(); if (uid != 0 || euid != 0) { mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", program_path, program_path); @@ -545,9 +543,9 @@ void check_requirements(const char* program_path) } /** - * Check for coretemp and applesmc modules - */ - DIR* dir = opendir(CORETEMP_PATH); + * Check for coretemp and applesmc modules + */ + DIR *dir = opendir(CORETEMP_PATH); if (ENOENT == errno) { mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); @@ -556,7 +554,6 @@ void check_requirements(const char* program_path) closedir(dir); - dir = opendir(APPLESMC_PATH); if (ENOENT == errno) { @@ -565,8 +562,6 @@ void check_requirements(const char* program_path) } closedir(dir); - - } int get_max_mhz(void) @@ -578,9 +573,7 @@ int get_max_mhz(void) } struct dirent *ent; while ((ent = readdir(dir)) != NULL) { - if (strncmp(ent->d_name, "cpu", 3) != 0 || - strcmp(ent->d_name, "cpufreq") == 0 || - strcmp(ent->d_name, "cpuidle") == 0) { + if (strncmp(ent->d_name, "cpu", 3) != 0 || strcmp(ent->d_name, "cpufreq") == 0 || strcmp(ent->d_name, "cpuidle") == 0) { continue; } char *path = smprintf("/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq", ent->d_name); @@ -595,20 +588,20 @@ void mbpfan() { int old_temp, new_temp, fan_speed, steps; int temp_change; - + sensors = retrieve_sensors(); fans = retrieve_fans(); retrieve_settings(NULL, fans); - - t_fans* fan = fans; - while(fan != NULL) { - + + t_fans *fan = fans; + while (fan != NULL) { + if (fan->fan_min_speed > fan->fan_max_speed) { - mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); + mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed); exit(EXIT_FAILURE); } - fan = fan->next; + fan = fan->next; } if (low_temp > high_temp || high_temp > max_temp) { @@ -622,63 +615,61 @@ void mbpfan() set_fan_minimum_speed(fans); fan = fans; - while(fan != NULL) { + while (fan != NULL) { - fan->step_up = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - high_temp ) * ( max_temp - high_temp + 1 ) / 2.0 ); + fan->step_up = (float)(fan->fan_max_speed - fan->fan_min_speed) / (float)((max_temp - high_temp) * (max_temp - high_temp + 1) / 2.0); - fan->step_down = (float)( fan->fan_max_speed - fan->fan_min_speed ) / - (float)( ( max_temp - low_temp ) * ( max_temp - low_temp + 1 ) / 2.0 ); - fan = fan->next; + fan->step_down = (float)(fan->fan_max_speed - fan->fan_min_speed) / (float)((max_temp - low_temp) * (max_temp - low_temp + 1) / 2.0); + fan = fan->next; } recalibrate: - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta"); } sleep(2); - while(1) { + while (1) { old_temp = new_temp; new_temp = get_temp(sensors); fan = fans; - while(fan != NULL) { - fan_speed = fan->old_speed; + while (fan != NULL) { + fan_speed = fan->old_speed; - if(new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { + if (new_temp >= max_temp && fan->old_speed != fan->fan_max_speed) { fan_speed = fan->fan_max_speed; } - if(new_temp <= low_temp && fan_speed != fan->fan_min_speed) { + if (new_temp <= low_temp && fan_speed != fan->fan_min_speed) { fan_speed = fan->fan_min_speed; } temp_change = new_temp - old_temp; - if(temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { - steps = ( new_temp - high_temp ) * ( new_temp - high_temp + 1 ) / 2; - fan_speed = max( fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up) ); + if (temp_change > 0 && new_temp > high_temp && new_temp < max_temp) { + steps = (new_temp - high_temp) * (new_temp - high_temp + 1) / 2; + fan_speed = max(fan_speed, ceil(fan->fan_min_speed + steps * fan->step_up)); } - if(temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { - steps = ( max_temp - new_temp ) * ( max_temp - new_temp + 1 ) / 2; - fan_speed = min( fan_speed, floor(fan->fan_max_speed - steps * fan->step_down) ); + if (temp_change < 0 && new_temp > low_temp && new_temp < max_temp) { + steps = (max_temp - new_temp) * (max_temp - new_temp + 1) / 2; + fan_speed = min(fan_speed, floor(fan->fan_max_speed - steps * fan->step_down)); } - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d Max MHz: %d", old_temp, new_temp, fan->label, fan_speed, get_max_mhz()); - } + } - set_fan_speed(fan, fan_speed); - fan = fan->next; - } + set_fan_speed(fan, fan_speed); + fan = fan->next; + } - if(verbose) { + if (verbose) { mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval); } - + time_t before_sleep = time(NULL); // call nanosleep instead of sleep to avoid rt_sigprocmask and @@ -689,7 +680,7 @@ void mbpfan() nanosleep(&ts, NULL); time_t after_sleep = time(NULL); - if(after_sleep - before_sleep > 2 * polling_interval) { + if (after_sleep - before_sleep > 2 * polling_interval) { mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval); set_fans_man(fans); goto recalibrate; diff --git a/src/mbpfan.h b/src/mbpfan.h index f673a58..d93c6d2 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -40,7 +40,7 @@ typedef struct s_sensors t_sensors; struct s_fans; typedef struct s_fans t_fans; -char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2))); +char *smprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); /** * Return true if the kernel is < 3.15.0 @@ -52,7 +52,7 @@ bool is_legacy_sensors_path(); * /etc/mbpfan.conf * If it fails, the default hardcoded settings are used */ -void retrieve_settings(const char* settings_path, t_fans *fans); +void retrieve_settings(const char *settings_path, t_fans *fans); /** * Detect the sensors in /sys/devices/platform/coretemp.0/temp @@ -71,7 +71,7 @@ t_sensors *refresh_sensors(t_sensors *sensors); * Detect the fans in /sys/devices/platform/applesmc.768/ * Associate each fan to a sensor */ -t_fans* retrieve_fans(); +t_fans *retrieve_fans(); /** * Given a list of sensors with associated fans @@ -89,22 +89,22 @@ void set_fans_auto(t_fans *fans); * Given a sensors with associated fans * Change their speed */ -void set_fan_speed(t_fans* fan, int speed); +void set_fan_speed(t_fans *fan, int speed); /** * Given a list of fans set their minumum fan speed */ -void set_fan_minimum_speed(t_fans* fans); +void set_fan_minimum_speed(t_fans *fans); /** * Return maximum CPU temp in degrees */ -unsigned short get_temp(t_sensors* sensors); +unsigned short get_temp(t_sensors *sensors); /** * Check if user has proper access and that required * kernel modules are available */ -void check_requirements(const char* program_path); +void check_requirements(const char *program_path); /** * Main Program diff --git a/src/settings.c b/src/settings.c index 20d18f3..32bdfb6 100644 --- a/src/settings.c +++ b/src/settings.c @@ -36,17 +36,17 @@ */ #include "settings.h" -#define MAX_SECTIONCHARS 256 -#define MAX_KEYCHARS 256 -#define MAX_VALUECHARS 256 -#define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10) +#define MAX_SECTIONCHARS 256 +#define MAX_KEYCHARS 256 +#define MAX_VALUECHARS 256 +#define MAX_LINECHARS (MAX_KEYCHARS + MAX_VALUECHARS + 10) -#define COMMENT_CHAR '#' -#define SECTION_START_CHAR '[' -#define SECTION_END_CHAR ']' -#define KEY_VALUE_SEPARATOR_CHAR '=' +#define COMMENT_CHAR '#' +#define SECTION_START_CHAR '[' +#define SECTION_END_CHAR ']' +#define KEY_VALUE_SEPARATOR_CHAR '=' -#define DEFAULT_STRMAP_CAPACITY 256 +#define DEFAULT_STRMAP_CAPACITY 256 typedef struct Section Section; typedef struct ParseState ParseState; @@ -83,20 +83,20 @@ static int is_comment_str(const char *str); static int is_section_str(const char *str); static int is_key_value_str(const char *str); static int is_key_without_value_str(const char *str); -static const char * get_token(char *str, char delim, char **last); +static const char *get_token(char *str, char delim, char **last); static int get_section_from_str(const char *str, char *out_buf, unsigned int out_buf_n); static int get_key_value_from_str(const char *str, char *out_buf1, unsigned int out_buf1_n, char *out_buf2, unsigned int out_buf2_n); static int get_key_without_value_from_str(const char *str, char *out_buf, unsigned int out_buf_n); static int get_converted_value(const Settings *settings, const char *section, const char *key, ConvertMode mode, void *out); static int get_converted_tuple(const Settings *settings, const char *section, const char *key, char delim, ConvertMode mode, void *out, unsigned int n_out); -static Section * get_section(Section *sections, unsigned int n, const char *name); +static Section *get_section(Section *sections, unsigned int n, const char *name); static void enum_map(const char *key, const char *value, const void *obj); -Settings * settings_new() +Settings *settings_new() { Settings *settings; - settings = (Settings*)malloc(sizeof(Settings)); + settings = (Settings *)malloc(sizeof(Settings)); if (settings == NULL) { return NULL; @@ -135,7 +135,7 @@ void settings_delete(Settings *settings) free(settings); } -Settings * settings_open(FILE *stream) +Settings *settings_open(FILE *stream) { Settings *settings; char buf[MAX_LINECHARS]; @@ -285,7 +285,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const if (s == NULL) { /* The section is not created---create it */ - s = (Section*)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); + s = (Section *)realloc(settings->sections, (settings->section_count + 1) * sizeof(Section)); if (s == NULL) { return 0; @@ -301,7 +301,7 @@ int settings_set(Settings *settings, const char *section, const char *key, const return 0; } - s->name = (char*)malloc((strlen(section) + 1) * sizeof(char)); + s->name = (char *)malloc((strlen(section) + 1) * sizeof(char)); if (s->name == NULL) { sm_delete(s->map); @@ -723,7 +723,7 @@ static int get_key_without_value_from_str(const char *str, char *out_buf, unsign * printf("token: %s", token); * } */ -static const char * get_token(char *str, char delim, char **last) +static const char *get_token(char *str, char delim, char **last) { char *s0; @@ -845,7 +845,7 @@ static int get_converted_tuple(const Settings *settings, const char *section, co /* Returns a pointer to the section or null if the named section does not * exist. */ -static Section * get_section(Section *sections, unsigned int n, const char *name) +static Section *get_section(Section *sections, unsigned int n, const char *name) { unsigned int i; Section *section; @@ -896,7 +896,7 @@ static void enum_map(const char *key, const char *value, const void *obj) /* - GNU LESSER GENERAL PUBLIC LICENSE + GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. diff --git a/src/settings.h b/src/settings.h index 4b3ada8..1698f9b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -39,8 +39,7 @@ #include "strmap.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif typedef struct Settings Settings; @@ -62,7 +61,7 @@ typedef struct Settings Settings; * * Return value: None. */ -typedef void(*settings_section_enum_func)(const char *key, const char *value, const void *obj); +typedef void (*settings_section_enum_func)(const char *key, const char *value, const void *obj); /* * Creates a settings object. @@ -70,7 +69,7 @@ typedef void(*settings_section_enum_func)(const char *key, const char *value, co * Return value: A pointer to a settings object, * or null if a new settings object could not be allocated. */ -Settings * settings_new(); +Settings *settings_new(); /* * Releases all memory held by a settings object. @@ -98,7 +97,7 @@ void settings_delete(Settings *settings); * Return value: A pointer to a settings object, * or null if an error occurred. */ -Settings * settings_open(FILE *stream); +Settings *settings_open(FILE *stream); /* * Saves the current settings object in textual form to the given stream. @@ -338,7 +337,7 @@ int settings_section_enum(const Settings *settings, const char *section, setting /* - GNU LESSER GENERAL PUBLIC LICENSE + GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. diff --git a/src/strmap.c b/src/strmap.c index 45853e3..cdbe6ff 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -54,21 +54,21 @@ struct StrMap { Bucket *buckets; }; -static Pair * get_pair(Bucket *bucket, const char *key); +static Pair *get_pair(Bucket *bucket, const char *key); static unsigned long hash(const char *str); -StrMap * sm_new(unsigned int capacity) +StrMap *sm_new(unsigned int capacity) { StrMap *map; - map = (StrMap*)malloc(sizeof(StrMap)); + map = (StrMap *)malloc(sizeof(StrMap)); if (map == NULL) { return NULL; } map->count = capacity; - map->buckets = (Bucket*)malloc(map->count * sizeof(Bucket)); + map->buckets = (Bucket *)malloc(map->count * sizeof(Bucket)); if (map->buckets == NULL) { free(map); @@ -98,7 +98,7 @@ void sm_delete(StrMap *map) pair = bucket->pairs; j = 0; - while(j < m) { + while (j < m) { free(pair->key); free(pair->value); pair++; @@ -210,7 +210,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* If the new value is larger than the old value, re-allocate * space for the new larger value. */ - tmp_value = (char*)realloc(pair->value, (value_len + 1) * sizeof(char)); + tmp_value = (char *)realloc(pair->value, (value_len + 1) * sizeof(char)); if (tmp_value == NULL) { return 0; @@ -225,13 +225,13 @@ int sm_put(StrMap *map, const char *key, const char *value) } /* Allocate space for a new key and value */ - new_key = (char*)malloc((key_len + 1) * sizeof(char)); + new_key = (char *)malloc((key_len + 1) * sizeof(char)); if (new_key == NULL) { return 0; } - new_value = (char*)malloc((value_len + 1) * sizeof(char)); + new_value = (char *)malloc((value_len + 1) * sizeof(char)); if (new_value == NULL) { free(new_key); @@ -243,7 +243,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* The bucket is empty, lazily allocate space for a single * key-value pair. */ - bucket->pairs = (Pair*)malloc(sizeof(Pair)); + bucket->pairs = (Pair *)malloc(sizeof(Pair)); if (bucket->pairs == NULL) { free(new_key); @@ -257,7 +257,7 @@ int sm_put(StrMap *map, const char *key, const char *value) /* The bucket wasn't empty but no pair existed that matches the provided * key, so create a new key-value pair. */ - tmp_pairs = (Pair*)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + tmp_pairs = (Pair *)realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); if (tmp_pairs == NULL) { free(new_key); @@ -353,7 +353,7 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj) * Returns a pair from the bucket that matches the provided key, * or null if no such pair exist. */ -static Pair * get_pair(Bucket *bucket, const char *key) +static Pair *get_pair(Bucket *bucket, const char *key) { unsigned int i, n; Pair *pair; @@ -398,7 +398,7 @@ static unsigned long hash(const char *str) /* - GNU LESSER GENERAL PUBLIC LICENSE + GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. diff --git a/src/strmap.h b/src/strmap.h index 416a1ce..5206cbb 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -37,8 +37,7 @@ #define _STRMAP_H_ #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #include @@ -63,7 +62,7 @@ typedef struct StrMap StrMap; * * Return value: None. */ -typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj); +typedef void (*sm_enum_func)(const char *key, const char *value, const void *obj); /* * Creates a string map. @@ -76,7 +75,7 @@ typedef void(*sm_enum_func)(const char *key, const char *value, const void *obj) * Return value: A pointer to a string map object, * or null if a new string map could not be allocated. */ -StrMap * sm_new(unsigned int capacity); +StrMap *sm_new(unsigned int capacity); /* * Releases all memory held by a string map object. @@ -186,7 +185,7 @@ int sm_enum(const StrMap *map, sm_enum_func enum_func, const void *obj); /* - GNU LESSER GENERAL PUBLIC LICENSE + GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. diff --git a/src/util.c b/src/util.c index 136bcc6..c66e6d7 100644 --- a/src/util.c +++ b/src/util.c @@ -16,6 +16,6 @@ void mbp_log(int level, const char *fmt, ...) va_start(args, fmt); vprintf(fmt, args); - puts(""); // trailing newline + puts(""); // trailing newline va_end(args); } diff --git a/src/util.h b/src/util.h index 9bf99ce..7f4a280 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,6 @@ #ifndef _UTIL_H_ #define _UTIL_H_ -void mbp_log(int level, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); +void mbp_log(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); #endif From 2dd04d5fb58a56111072a123e39c2ba65ed0f8a9 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sat, 26 Feb 2022 14:20:07 +0900 Subject: [PATCH 218/235] Update version to 2.3.0 Fixes #213. References #247. --- src/global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index 6eaea36..b075056 100644 --- a/src/global.h +++ b/src/global.h @@ -2,7 +2,7 @@ #define _GLOBAL_H_ #define PROGRAM_NAME "mbpfan" -#define PROGRAM_VERSION "2.2.0" +#define PROGRAM_VERSION "2.3.0" #define PROGRAM_PID "/var/run/mbpfan.pid" extern int daemonize; From 79813060f4ed62daaacb804c2af6328f5efd79ba Mon Sep 17 00:00:00 2001 From: Nathan Fritch Date: Sat, 2 Apr 2022 09:42:31 -0500 Subject: [PATCH 219/235] Create dinit service definition, update readme to include dinit instructions --- README.md | 7 +++++++ mbpfan.dinit | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 mbpfan.dinit diff --git a/README.md b/README.md index 5852fbd..2770693 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,13 @@ execute the following (as root): sudo systemctl daemon-reload sudo systemctl start mbpfan.service +**dinit** +A [dinit](https://github.com/davmac314/dinit) service definition is also included. To use it, execute the following: + + sudo cp mbpfan.dinit /etc/dinit.d/mbpfan + sudo dinitctl enable mbpfan + sudo dinitctl start mbpfan + ## Usage Usage: ./mbpfan OPTION(S) diff --git a/mbpfan.dinit b/mbpfan.dinit new file mode 100644 index 0000000..cb2c3a7 --- /dev/null +++ b/mbpfan.dinit @@ -0,0 +1,7 @@ +type = bgprocess +command = /usr/bin/mbpfan +restart = true +logfile = /var/log/dinit/mbpfan.log +waits-for = loginready +pid-file = /var/run/mbpfan.pid + From 95b6ba91b9ab522cc18362e31de420ade8b3219c Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 12 Aug 2022 09:59:30 +0800 Subject: [PATCH 220/235] Add coretemp as module dependency at startup --- Makefile | 3 +++ mbpfan.depend.conf | 1 + 2 files changed, 4 insertions(+) create mode 100644 mbpfan.depend.conf diff --git a/Makefile b/Makefile index 5c466b8..a90293d 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ TESTS_PATH = tests/ TESTS_BIN = bin/mbpfan-tests BIN = bin/mbpfan CONF = mbpfan.conf +DEPEND_MODULE = mbpfan.depend.conf DOC = README.md MAN = mbpfan.8.gz @@ -52,6 +53,7 @@ tests: all uninstall: rm /usr/sbin/mbpfan rm /etc/mbpfan.conf + rm /lib/modules-load.d/mbpfan.depend.conf rm /lib/systemd/system/mbpfan.service rm /usr/share/man/man8/mbpfan.8.gz rm -rf /usr/share/doc/mbpfan @@ -63,6 +65,7 @@ install: all install -d $(DESTDIR)/usr/share/doc/mbpfan install $(BIN) $(DESTDIR)/usr/sbin install -m644 $(CONF) $(DESTDIR)/etc + install -m644 $(DEPEND_MODULE) $(DESTDIR)/lib/modules-load.d install -m644 $(DOC) $(DESTDIR)/usr/share/doc/mbpfan install -d $(DESTDIR)/usr/share/man/man8 install -m644 $(MAN) $(DESTDIR)/usr/share/man/man8 diff --git a/mbpfan.depend.conf b/mbpfan.depend.conf new file mode 100644 index 0000000..77ce462 --- /dev/null +++ b/mbpfan.depend.conf @@ -0,0 +1 @@ +coretemp From 71d72cfc8ba2be823e5a7cc0e13469a2c3b0f509 Mon Sep 17 00:00:00 2001 From: "Ethan D. Twardy" Date: Fri, 22 Jul 2022 23:39:23 -0500 Subject: [PATCH 221/235] Fix PIDFile= reference, restart latency in systemd service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The run directory has been moved from /var/run to /run in systemd, which results in the following warning at boot: ``` /usr/lib/systemd/system/mbpfan.service:10: PIDFile= references a path below legacy directory /var/run/, updating /var/run/mbpfan.pid → /run/mbpfan.pid; please update the unit file accordingly. ``` Additionally, the service occasionally fails to start if the service is started before coretemp devices have finished probing, which results in the following error. This patch fixes this issue. ``` systemd[1]: Started A fan manager daemon for MacBook Pro. mbpfan[350]: /usr/sbin/mbpfan needs coretemp support. Please either load it or build it into the kernel. Exiting. systemd[1]: mbpfan.service: Main process exited, code=exited, status=1/FAILURE systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: mbpfan.service: Scheduled restart job, restart counter is at 1. systemd[1]: Stopped A fan manager daemon for MacBook Pro. systemd[1]: Started A fan manager daemon for MacBook Pro. mbpfan[369]: /usr/sbin/mbpfan needs coretemp support. Please either load it or build it into the kernel. Exiting. systemd[1]: mbpfan.service: Main process exited, code=exited, status=1/FAILURE systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: mbpfan.service: Scheduled restart job, restart counter is at 2. systemd[1]: Stopped A fan manager daemon for MacBook Pro. mbpfan[380]: /usr/sbin/mbpfan needs coretemp support. Please either load it or build it into the kernel. Exiting. systemd[1]: Started A fan manager daemon for MacBook Pro. systemd[1]: mbpfan.service: Main process exited, code=exited, status=1/FAILURE systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: mbpfan.service: Scheduled restart job, restart counter is at 3. mbpfan[393]: /usr/sbin/mbpfan needs coretemp support. Please either load it or build it into the kernel. Exiting. systemd[1]: Stopped A fan manager daemon for MacBook Pro. systemd[1]: Started A fan manager daemon for MacBook Pro. systemd[1]: mbpfan.service: Main process exited, code=exited, status=1/FAILURE systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: mbpfan.service: Scheduled restart job, restart counter is at 4. systemd[1]: Stopped A fan manager daemon for MacBook Pro. systemd[1]: Started A fan manager daemon for MacBook Pro. mbpfan[416]: /usr/sbin/mbpfan needs coretemp support. Please either load it or build it into the kernel. Exiting. systemd[1]: mbpfan.service: Main process exited, code=exited, status=1/FAILURE systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: mbpfan.service: Scheduled restart job, restart counter is at 5. systemd[1]: Stopped A fan manager daemon for MacBook Pro. systemd[1]: mbpfan.service: Start request repeated too quickly. systemd[1]: mbpfan.service: Failed with result 'exit-code'. systemd[1]: Failed to start A fan manager daemon for MacBook Pro. ``` --- mbpfan.service | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mbpfan.service b/mbpfan.service index a4b61e8..5ac729a 100644 --- a/mbpfan.service +++ b/mbpfan.service @@ -7,8 +7,9 @@ After=sysinit.target Type=simple ExecStart=/usr/sbin/mbpfan -f ExecReload=/usr/bin/kill -HUP $MAINPID -PIDFile=/var/run/mbpfan.pid +PIDFile=/run/mbpfan.pid Restart=always +RestartSec=1 [Install] WantedBy=sysinit.target From d4473d440b833409ff4823800dc65090cf09b6cb Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sat, 19 Nov 2022 11:22:39 -0800 Subject: [PATCH 222/235] Ensure lib/modules-load.d exists before installing there --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index a90293d..41873fa 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ install: all install -d $(DESTDIR)/etc install -d $(DESTDIR)/lib/systemd/system install -d $(DESTDIR)/usr/share/doc/mbpfan + install -d $(DESTDIR)/lib/modules-load.d install $(BIN) $(DESTDIR)/usr/sbin install -m644 $(CONF) $(DESTDIR)/etc install -m644 $(DEPEND_MODULE) $(DESTDIR)/lib/modules-load.d From 1941f45395c25de10b5dd292202caed32efd68b5 Mon Sep 17 00:00:00 2001 From: Robert Abram Date: Mon, 27 Sep 2021 09:25:57 -0500 Subject: [PATCH 223/235] Add support for alternate applesmc module device path. --- src/mbpfan.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 01df2f9..4527a25 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ #define CORETEMP_PATH "/sys/devices/platform/coretemp.0" #define APPLESMC_PATH "/sys/devices/platform/applesmc.768" +#define ALT_APPLESMC_PATH "/sys/bus/acpi/drivers/applesmc" /* temperature thresholds * low_temp - temperature below which fan speed will be at minimum @@ -74,6 +76,9 @@ int polling_interval = 1; t_sensors *sensors = NULL; t_fans *fans = NULL; +char applesmc_path[PATH_MAX]; +char applesmc_fan_path[PATH_MAX]; + char *smprintf(const char *fmt, ...) { @@ -273,7 +278,7 @@ t_fans *retrieve_fans() char *path_fan_max = NULL; char *path_fan_min = NULL; - const char *path_begin = "/sys/devices/platform/applesmc.768/fan"; + const char *path_begin = (const char *) &applesmc_fan_path; const char *path_output_end = "_output"; const char *path_label_end = "_label"; const char *path_man_end = "_manual"; @@ -553,15 +558,41 @@ void check_requirements(const char *program_path) } closedir(dir); + memset(&applesmc_path, 0, PATH_MAX); + memset(&applesmc_fan_path, 0, PATH_MAX); dir = opendir(APPLESMC_PATH); - if (ENOENT == errno) { - mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path); - exit(EXIT_FAILURE); + if (ENOENT != errno) { + strncpy((char *) &applesmc_path, APPLESMC_PATH, PATH_MAX); + } else { + /** + * Check for alternate ACPI device path for newer macbooks + */ + closedir(dir); + dir = opendir(ALT_APPLESMC_PATH); + if (ENOENT != errno) { + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (strncmp("APP", (const char *) &ent->d_name, 3) == 0) { + snprintf((char *) &applesmc_path, PATH_MAX, "%s/%s", ALT_APPLESMC_PATH, (char *) &ent->d_name); + break; + } + } + } } closedir(dir); + + if (strlen(applesmc_path) != 0) { + strncpy((char *) &applesmc_fan_path, (char *) &applesmc_path, PATH_MAX); + strcat((char *) &applesmc_fan_path, "/fan"); + if (verbose) mbp_log(LOG_INFO, "applesmc device path: %s", (char *) &applesmc_path); + + } else { + mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", program_path); + exit(EXIT_FAILURE); + } } int get_max_mhz(void) From a9c324db67470a222b20d8ddaa9b4e1e488e9771 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 2 Apr 2023 17:04:13 +0900 Subject: [PATCH 224/235] Bump version to 2.4.0 --- src/global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index b075056..1552511 100644 --- a/src/global.h +++ b/src/global.h @@ -2,7 +2,7 @@ #define _GLOBAL_H_ #define PROGRAM_NAME "mbpfan" -#define PROGRAM_VERSION "2.3.0" +#define PROGRAM_VERSION "2.4.0" #define PROGRAM_PID "/var/run/mbpfan.pid" extern int daemonize; From 98d68c15dd0e7ca8e410cebb3a55f872c1a189c9 Mon Sep 17 00:00:00 2001 From: Andrea Manzini Date: Fri, 14 Apr 2023 08:27:20 +0200 Subject: [PATCH 225/235] Update global.h to reflect `PIDFile=/run/mbpfan.pid` in the service file --- src/global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index 1552511..363d0c9 100644 --- a/src/global.h +++ b/src/global.h @@ -3,7 +3,7 @@ #define PROGRAM_NAME "mbpfan" #define PROGRAM_VERSION "2.4.0" -#define PROGRAM_PID "/var/run/mbpfan.pid" +#define PROGRAM_PID "/run/mbpfan.pid" extern int daemonize; extern int verbose; From e88d29ed4b09d01e6b8d7a45645b0d4e66c6a664 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 3 Aug 2023 23:41:14 +0900 Subject: [PATCH 226/235] Fix clang 16 warnings --- src/global.h | 2 ++ src/mbpfan.h | 10 ++-------- src/strmap.c | 3 --- tests/minunit.h | 20 ++++++++++---------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/global.h b/src/global.h index 363d0c9..42b87bc 100644 --- a/src/global.h +++ b/src/global.h @@ -30,6 +30,8 @@ struct s_fans { struct s_fans *next; }; +/** Represents a Temperature sensor + */ typedef struct s_sensors t_sensors; typedef struct s_fans t_fans; diff --git a/src/mbpfan.h b/src/mbpfan.h index d93c6d2..e09d562 100644 --- a/src/mbpfan.h +++ b/src/mbpfan.h @@ -19,6 +19,8 @@ #include +#include "global.h" + /** Temperature Thresholds * low_temp - temperature below which fan speed will be at minimum * high_temp - fan will increase speed when higher than this temperature @@ -32,14 +34,6 @@ extern int max_temp; */ extern int polling_interval; -/** Represents a Temperature sensor - */ -struct s_sensors; -typedef struct s_sensors t_sensors; - -struct s_fans; -typedef struct s_fans t_fans; - char *smprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); /** diff --git a/src/strmap.c b/src/strmap.c index cdbe6ff..f7d7dcc 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -284,7 +284,6 @@ int sm_get_count(const StrMap *map) unsigned int i, j, n, m; unsigned int count; Bucket *bucket; - Pair *pair; if (map == NULL) { return 0; @@ -296,13 +295,11 @@ int sm_get_count(const StrMap *map) count = 0; while (i < n) { - pair = bucket->pairs; m = bucket->count; j = 0; while (j < m) { count++; - pair++; j++; } diff --git a/tests/minunit.h b/tests/minunit.h index c1ab00b..ba99383 100644 --- a/tests/minunit.h +++ b/tests/minunit.h @@ -12,16 +12,16 @@ extern int tests_run; -static const char *test_sensor_paths(); -static const char *test_fan_paths(); -static const char *test_get_temp(); -static const char *test_config_file(); -static const char *test_settings(); +static const char *test_sensor_paths(void); +static const char *test_fan_paths(void); +static const char *test_get_temp(void); +static const char *test_config_file(void); +static const char *test_settings(void); static void handler(int signal); -static const char *test_sighup_receive(); -static const char *test_settings_reload(); -static const char *all_tests(); +static const char *test_sighup_receive(void); +static const char *test_settings_reload(void); +static const char *all_tests(void); -int tests(); +int tests(const char *program_path); -#endif \ No newline at end of file +#endif From 0cb3c5a6c5b770e93e4d6f9edb01b78df8b95b68 Mon Sep 17 00:00:00 2001 From: Brahmajit Das Date: Thu, 3 Aug 2023 10:07:20 +0000 Subject: [PATCH 227/235] src/util.c: Fix build with clang-16 When building with clang-16, the following build error is raised. clang -c -O2 -march=x86-64 -pipe -pipe -frecord-gcc-switches -fno-diagnostics-color -fmessage-length=0 -g -Wall -Wextra -Wno-unused-function -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 src/util.c -osrc/util.o src/util.c:13:9: error: call to undeclared function 'vsyslog'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] vsyslog(level, fmt, args); ^ src/util.c:13:9: note: did you mean 'syslog'? /usr/include/syslog.h:62:6: note: 'syslog' declared here void syslog (int, const char *, ...); This is due to vsyslog being available when _GNU_SOURCE or _BSD_SOURCE is defined [1]. This patch does that, thus fixing the error. [1]: https://linux.die.net/man/3/vsyslog Bug: https://bugs.gentoo.org/894522 Signed-off-by: Brahmajit Das --- src/util.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util.c b/src/util.c index c66e6d7..0fbe3bf 100644 --- a/src/util.c +++ b/src/util.c @@ -1,6 +1,7 @@ #include #include #define __USE_MISC +#define _GNU_SOURCE #include #include "global.h" From 8434cfb743491c1960120fda8132ac67b28eb79a Mon Sep 17 00:00:00 2001 From: Michal Kielan Date: Fri, 18 Oct 2024 14:01:52 +0200 Subject: [PATCH 228/235] Use corrent format string for unsigned int --- src/mbpfan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 4527a25..955e0d3 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -196,7 +196,7 @@ t_sensors *retrieve_sensors() if (file != NULL) { s = (t_sensors *)malloc(sizeof(t_sensors)); s->path = strdup(path); - fscanf(file, "%d", &s->temperature); + fscanf(file, "%u", &s->temperature); if (sensors_head == NULL) { sensors_head = s; From a327a854421ada07bc218eadf93cc5967b2a37fd Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Oct 2024 12:15:04 -0700 Subject: [PATCH 229/235] Add missing #include Found via clang-tidy. --- src/global.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/global.h b/src/global.h index 42b87bc..65a22a5 100644 --- a/src/global.h +++ b/src/global.h @@ -1,6 +1,8 @@ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ +#include + #define PROGRAM_NAME "mbpfan" #define PROGRAM_VERSION "2.4.0" #define PROGRAM_PID "/run/mbpfan.pid" From b4cb72a6e920975c8532b4c6a9653f2fcb9d3a9e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Oct 2024 12:15:16 -0700 Subject: [PATCH 230/235] Improve directory error checking Found via clang-tidy. --- src/mbpfan.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/mbpfan.c b/src/mbpfan.c index 955e0d3..729c7c0 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -552,7 +552,7 @@ void check_requirements(const char *program_path) */ DIR *dir = opendir(CORETEMP_PATH); - if (ENOENT == errno) { + if (dir == NULL) { mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path); exit(EXIT_FAILURE); } @@ -563,15 +563,15 @@ void check_requirements(const char *program_path) dir = opendir(APPLESMC_PATH); - if (ENOENT != errno) { + if (dir != NULL) { strncpy((char *) &applesmc_path, APPLESMC_PATH, PATH_MAX); + closedir(dir); } else { /** * Check for alternate ACPI device path for newer macbooks */ - closedir(dir); dir = opendir(ALT_APPLESMC_PATH); - if (ENOENT != errno) { + if (dir != NULL) { struct dirent *ent; while ((ent = readdir(dir)) != NULL) { if (strncmp("APP", (const char *) &ent->d_name, 3) == 0) { @@ -579,11 +579,10 @@ void check_requirements(const char *program_path) break; } } + closedir(dir); } } - closedir(dir); - if (strlen(applesmc_path) != 0) { strncpy((char *) &applesmc_fan_path, (char *) &applesmc_path, PATH_MAX); strcat((char *) &applesmc_fan_path, "/fan"); From db585f3ec977203cfda4f6e2b47b5026d70ce75c Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Oct 2024 16:37:05 -0700 Subject: [PATCH 231/235] Add configuration for clang-tidy --- .clang-tidy | 43 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 3 +++ 2 files changed, 46 insertions(+) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..5ee0d41 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,43 @@ +WarningsAsErrors: '*' +Checks: ' + *, + -altera-*, + -android-*, + -bugprone-assignment-in-if-condition, + -bugprone-branch-clone, + -bugprone-easily-swappable-parameters, + -bugprone-implicit-widening-of-multiplication-result, + -bugprone-narrowing-conversions, + -bugprone-reserved-identifier, + -bugprone-signed-char-misuse, + -cert-dcl37-c, + -cert-dcl51-cpp, + -cert-err33-c, + -cert-err34-c, + -cert-str34-c, + -clang-analyzer-core.*, + -clang-analyzer-security.*, + -clang-diagnostic-macro-redefined, + -concurrency-mt-unsafe, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-init-variables, + -cppcoreguidelines-macro-to-enum, + -cppcoreguidelines-narrowing-conversions, + -google-readability-braces-around-statements, + -google-readability-todo, + -hicpp-*, + -llvm-*, + -llvmlibc-*, + -misc-include-cleaner, + -modernize-macro-to-enum, + -readability-braces-around-statements, + -readability-else-after-return, + -readability-function-cognitive-complexity, + -readability-identifier-length, + -readability-inconsistent-declaration-parameter-name, + -readability-isolate-declaration, + -readability-magic-numbers, + -readability-math-missing-parentheses, + -readability-suspicious-call-argument, +' diff --git a/Makefile b/Makefile index 41873fa..187fb66 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,9 @@ clean: tests: all ./bin/mbpfan-tests +clang-tidy: + clang-tidy src/*.[ch] + uninstall: rm /usr/sbin/mbpfan rm /etc/mbpfan.conf From 905988c4312c54a45a4d0a967baa9b6af63fd358 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Fri, 18 Oct 2024 16:38:06 -0700 Subject: [PATCH 232/235] Do not call signal-unsafe functions in handler Found via clang-tidy. --- .clang-tidy | 1 + Makefile | 2 +- src/daemon.c | 44 +++++++++++++------------------------------- src/daemon.h | 7 +++++++ src/mbpfan.c | 12 ++++++++++++ 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5ee0d41..dae158a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -30,6 +30,7 @@ Checks: ' -llvm-*, -llvmlibc-*, -misc-include-cleaner, + -misc-unused-parameters, -modernize-macro-to-enum, -readability-braces-around-statements, -readability-else-after-return, diff --git a/Makefile b/Makefile index 187fb66..94d72ee 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ BINFLAG = -o INCLUDES = LIBS = -lm LIBPATH = -CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 +CFLAGS += $(COPT) -g $(INCLUDES) -Wall -Wextra -Wno-unused-function -Wno-unused-parameter -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=500 LDFLAGS += $(LIBPATH) -g OBJS := $(patsubst %.$(C),%.$(OBJ),$(wildcard $(SOURCE_PATH)*.$(C))) diff --git a/src/daemon.c b/src/daemon.c index 760f512..14a5b0d 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -36,6 +36,8 @@ int daemonize = 1; int verbose = 0; +sig_atomic_t do_exit = 0; +sig_atomic_t do_reload = 0; int write_pid(int pid) { @@ -77,7 +79,7 @@ int delete_pid() return remove(PROGRAM_PID); } -static void cleanup_and_exit(int exit_code) +void cleanup_and_exit(int exit_code) { delete_pid(); set_fans_auto(fans); @@ -109,44 +111,24 @@ static void cleanup_and_exit(int exit_code) exit(exit_code); } -void signal_handler(int signal) +static void sighup_handler(int signal) { + do_reload = 1; +} - switch (signal) { - case SIGHUP: - syslog(LOG_WARNING, "Received SIGHUP signal."); - retrieve_settings(NULL, fans); - break; - - case SIGTERM: - syslog(LOG_WARNING, "Received SIGTERM signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - case SIGQUIT: - syslog(LOG_WARNING, "Received SIGQUIT signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - case SIGINT: - syslog(LOG_WARNING, "Received SIGINT signal."); - cleanup_and_exit(EXIT_SUCCESS); - break; - - default: - syslog(LOG_WARNING, "Unhandled signal (%d) %s", signal, strsignal(signal)); - break; - } +static void sigterm_handler(int signal) +{ + do_exit = 1; } void go_daemon(void (*fan_control)()) { // Setup signal handling before we start - signal(SIGHUP, signal_handler); - signal(SIGTERM, signal_handler); - signal(SIGQUIT, signal_handler); - signal(SIGINT, signal_handler); + signal(SIGHUP, sighup_handler); + signal(SIGTERM, sigterm_handler); + signal(SIGQUIT, sigterm_handler); + signal(SIGINT, sigterm_handler); // Setup syslog logging - see SETLOGMASK(3) if (verbose) { diff --git a/src/daemon.h b/src/daemon.h index cb26450..b935a34 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -16,6 +16,11 @@ #ifndef _DAEMON_H_ #define _DAEMON_H_ +#include + +extern sig_atomic_t do_exit; +extern sig_atomic_t do_reload; + /** * Write the PID of the forked daemon to the * .pid file defined in char *program_pid @@ -50,4 +55,6 @@ void signal_handler(int signal); */ void go_daemon(void (*function)()); +void cleanup_and_exit(int exit_code); + #endif diff --git a/src/mbpfan.c b/src/mbpfan.c index 729c7c0..39f67c5 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -42,6 +42,7 @@ #include #include #include +#include "daemon.h" #include "mbpfan.h" #include "global.h" #include "settings.h" @@ -660,6 +661,17 @@ void mbpfan() sleep(2); while (1) { + if (do_exit) { + syslog(LOG_WARNING, "Received SIGTERM, SIGQUIT, or SIGINT signal."); + cleanup_and_exit(EXIT_SUCCESS); + } + + if (do_reload) { + syslog(LOG_WARNING, "Received SIGHUP signal."); + retrieve_settings(NULL, fans); + do_reload = 0; + } + old_temp = new_temp; new_temp = get_temp(sensors); From 5c49f522a506d14b682a0d931ccc06d964556b9d Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 3 Nov 2024 10:47:55 -0800 Subject: [PATCH 233/235] Enable clang-tidy misc-include-cleaner --- .clang-tidy | 1 - src/daemon.c | 3 ++- src/main.c | 6 ++---- src/mbpfan.c | 3 +++ src/settings.c | 4 ++++ src/settings.h | 5 ----- src/strmap.c | 2 ++ src/strmap.h | 3 --- src/util.c | 2 +- 9 files changed, 14 insertions(+), 15 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index dae158a..1175828 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -29,7 +29,6 @@ Checks: ' -hicpp-*, -llvm-*, -llvmlibc-*, - -misc-include-cleaner, -misc-unused-parameters, -modernize-macro-to-enum, -readability-braces-around-statements, diff --git a/src/daemon.c b/src/daemon.c index 14a5b0d..e604131 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -16,15 +16,16 @@ * */ +#include #include #include #include #include #include #include -#include #include #include +#include #include #include #include diff --git a/src/main.c b/src/main.c index 7e5f1d6..c043253 100644 --- a/src/main.c +++ b/src/main.c @@ -20,14 +20,12 @@ #include #include -#include +#include // NOLINT(misc-include-cleaner) #include #include -#include #include "mbpfan.h" #include "daemon.h" #include "global.h" -#include "util.h" void print_usage(int argc, char *argv[]) { @@ -46,7 +44,7 @@ int main(int argc, char *argv[]) int c; - while ((c = getopt(argc, argv, "hfv|help")) != -1) { + while ((c = getopt(argc, argv, "hfv|help")) != -1) { // NOLINT(misc-include-cleaner) switch (c) { case 'h': print_usage(argc, argv); diff --git a/src/mbpfan.c b/src/mbpfan.c index 39f67c5..b45788c 100644 --- a/src/mbpfan.c +++ b/src/mbpfan.c @@ -28,6 +28,8 @@ * Tested models: see README.md */ +#include +#include #include #include #include @@ -39,6 +41,7 @@ #include #include #include +#include #include #include #include diff --git a/src/settings.c b/src/settings.c index 32bdfb6..b89c143 100644 --- a/src/settings.c +++ b/src/settings.c @@ -34,7 +34,11 @@ * You should have received a copy of the GNU Lesser General Public License * along with settings. If not, see . */ +#include +#include +#include #include "settings.h" +#include "strmap.h" #define MAX_SECTIONCHARS 256 #define MAX_KEYCHARS 256 diff --git a/src/settings.h b/src/settings.h index 1698f9b..91266aa 100644 --- a/src/settings.h +++ b/src/settings.h @@ -31,12 +31,7 @@ #ifndef _SETTINGS_H_ #define _SETTINGS_H_ -#include -#include #include -#include - -#include "strmap.h" #ifdef __cplusplus extern "C" { diff --git a/src/strmap.c b/src/strmap.c index f7d7dcc..deebb7b 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -33,6 +33,8 @@ * You should have received a copy of the GNU Lesser General Public License * along with strmap. If not, see . */ +#include +#include #include "strmap.h" typedef struct Pair Pair; diff --git a/src/strmap.h b/src/strmap.h index 5206cbb..4f7ade1 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -40,9 +40,6 @@ extern "C" { #endif -#include -#include - typedef struct StrMap StrMap; /* diff --git a/src/util.c b/src/util.c index 0fbe3bf..df5b03f 100644 --- a/src/util.c +++ b/src/util.c @@ -11,7 +11,7 @@ void mbp_log(int level, const char *fmt, ...) va_list args; if (daemonize) { va_start(args, fmt); - vsyslog(level, fmt, args); + vsyslog(level, fmt, args); // NOLINT(misc-include-cleaner) va_end(args); } From 8fa04b914c76b6b0bde1c1bb2ef36a6feeec51d5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 3 Nov 2024 10:51:03 -0800 Subject: [PATCH 234/235] Remove some unnecessary calls to exit --- src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index c043253..14f0b7a 100644 --- a/src/main.c +++ b/src/main.c @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) switch (c) { case 'h': print_usage(argc, argv); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; break; case 'f': @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) default: print_usage(argc, argv); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; break; } } @@ -71,5 +71,5 @@ int main(int argc, char *argv[]) // pointer to mbpfan() function in mbpfan.c void (*fan_control)() = mbpfan; go_daemon(fan_control); - exit(EXIT_SUCCESS); + return EXIT_SUCCESS; } From 14e1c42109271e96fcdfe91fbdaeb5ed939764dd Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 3 Nov 2024 14:15:15 -0800 Subject: [PATCH 235/235] Enable more of clang-analyzer --- .clang-tidy | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 1175828..651b962 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -15,8 +15,9 @@ Checks: ' -cert-err33-c, -cert-err34-c, -cert-str34-c, - -clang-analyzer-core.*, - -clang-analyzer-security.*, + -clang-analyzer-core.CallAndMessage, + -clang-analyzer-security.insecureAPI.strcpy, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, -clang-diagnostic-macro-redefined, -concurrency-mt-unsafe, -cppcoreguidelines-avoid-magic-numbers,