Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client Fixes #27

Merged
merged 9 commits into from
Dec 3, 2021
Merged

client Fixes #27

merged 9 commits into from
Dec 3, 2021

Conversation

rofl0r
Copy link
Contributor

@rofl0r rofl0r commented Nov 15, 2021

No description provided.

client.c: In function 'get_fan_status':
client.c:301:11: warning: format '%d' expects argument of type 'int', but argument 8 has type 'nxjson_s64 {aka const long int}' [-Wformat=]
           "Fan Display Name         : %s\n"
instead of allocating a fixed-size buffer.
additionally, even if malloc returns a usable allocation, it will
not be zero-initialized.
1e13 is 10000000000000, which doesn't even fit into an int.
instead, use a stack buffer of 128 kbyte which should be plenty,
then strdup the actual string on return.
note that this entire function is horribly inefficient as the
string has to be scanned over and over.
an efficient implementation would use

    len = snprintf(buf, sizeof buf "[%s]", s)

instead of

    strcat(s, "["), strcat(s, t), strcat(s, "]")

, and then add len to the start pointer.

while at it, also rename the confusing IF_... macro which suggests an
if block starts, and use a function-like macro.
recursive make considered harmful:
https://embeddedartistry.com/blog/2017/04/10/recursive-make-considered-harmful/

the debug and release targets were removed.
debug/release builds being provided by the makefile is CMake-think,
a dangerous virus infecting the minds of the innocent.

the canonical way to make a release build is:
make CFLAGS=-O3 LDFLAGS-s

and a debug build:
make CFLAGS="-O0 -g3"

for someone who's too lazy to type that out these oneliners can be put
into debug.sh and release.sh, respectively.
not everyone uses systemd.
this allows to selectively use the targets one is interested in.
@rofl0r
Copy link
Contributor Author

rofl0r commented Nov 15, 2021

i also took the liberty to improve the makefile according to the guide i've written:
https://sabotage-linux.neocities.org/blog/5/

  • remove recursive make (this fixes issues with duplicate var names, and paralell build jobs)
  • proper use of DESTDIR. since e.g. bindir was unconditionally including $(DESTDIR), the destdir would end up in the hardcoded config location.
  • add "all" target as is custom.
  • allow user to set CPPFLAGS, CFLAGS, LDFLAGS.
  • use install command instead of cp, this creates directories as needed.
  • remove redundant release/debug target which grew out of a misunderstanding that make should work like CMake, the windows lusers favorite toy.
  • split install target so users can can selectively install stuff. not everyone is interested in systemd stuff, or zsh completion.

@JoshIles
Copy link
Contributor

Thanks for this! I've submitted my own PR to your branch which resolves a couple of issues that I found with the new /usr/local prefix in its current implementation. I have also adjusted the Makefile so that the default behaviour is in line with how it was before, while hopefully still allowing for the customization improvements that you have made.

@rofl0r
Copy link
Contributor Author

rofl0r commented Nov 22, 2021

thanks. i've seen your stuff is in https://github.com/JoshIles/nbfc-linux/commits/fixes but didn't make it here.

the prefix adjustements i made are in line with autoconf and other makefile-only build systems, where prefix always defaults to /usr/local. the intention is that a user compiling stuff from source wants it in /usr/local in order to not collide with distro-provided packages, and someone who knows what he's doing or a distro packager can just run make PREFIX=/usr sysconfdir=/etc.
however i wasn't aware that the locations are actually hardcoded into both systemd service files and python client. having the right prefix in systemd files is easy:
just rename e.g. "foo.service" to "foo.service.in" and replace /usr/local/ with @PREFIX@. Makefile then gets a rule like:

foo.service: foo.service.in
    sed 's/@PREFIX@/'$(PREFIX)'/' < $< >$@

and install-systemd-files then gets a dependency on foo.service.
same could be done for the python script, but if it's renamed to nbfc.py.in editor syntax highlighting will no longer kick in while working on the source tree.
so it might be solved by importing a module nbfc-client-config.py which has justs the paths in it and is created in the same manner.

@rofl0r
Copy link
Contributor Author

rofl0r commented Nov 22, 2021

as for CMake-style build vs release targets, the conventional way to do this in makefile-based build system is to run make CFLAGS=-O3 LDFLAGS=-s vs make CFLAGS="-O0 -g3".
in my own projects i add to all Makefiles the line -include config.mak and have e.g. CFLAGS=-O3 in that file.

- added -lm as a requirement.
- added install-c target which installs nbfc-linux with the C client, so users no longer need to manually edit the Makefile.
- changed uninstall target so that it force removes files, ensuring that it doesn't exit with Error in the slim (but real) chance that one of the files were manually removed.
- added new targets to make the python client and systemd service files compatibile with the user-defined path PREFIX, and updated those files accordingly.
@rofl0r
Copy link
Contributor Author

rofl0r commented Nov 23, 2021

nicely done!

@JoshIles
Copy link
Contributor

Thanks, and thank you for the much more robust solution with regard to the prefix. I have chosen to leave the conditional to allow 'make BUILD=debug' for a debug build, simply because it preserves the previous defaults, and advanced users can override it if they wish, so it shouldn't be an issue. But, I by no means have the final say on this matter.

I'd welcome some more input from anyone on this PR, since there is still currently no way of updating the documentation dynamically based on the user-definable prefix. Currently, @braph's argparse-tool is not part of the source tree, which was used to generate some of this stuff, as can be seen in the Makefile. In addition, completion/ec_probe.py, completion/nbfc_service.py, and nbfc.py.in would need to be dynamically edited, as they still contain hard-coded prefix paths and from which argparse-tool does its job. I also am yet to figure out how nbfc_service.help.h was generated.

- nbfc config --set auto now works correctly.
- couple of formatting improvements.
Additionally
- corrected a couple of typos.
- new entries in .gitignore.
@JoshIles
Copy link
Contributor

client.c

nbfc config --set auto now works correctly thanks to the following changes:

  • removed early return from main which prevented setting automatic config in a later code block.
  • made change to main to ensure that the nbfc config directory exists (in line with the Python client), avoiding potential related errors when running nbfc start or nbfc config set.
  • made change to sep so that it no longer incorrectly replaces the last word with NULL, ensuring that all words of concern within config names and the notebook model retrieved via dmidecode are compared when attempting to set a config automatically.
  • made change to compare_configs so that it no longer produces inconsistent behaviour across compiles, particularly for notebook names that are only a partial match with the predefined configs.

Note: I never knew there were issues with setting configs via the C client before because I always had a leftover config file from the Python client. The changes to the uninstall rule in the Makefile now removes this, which made these issues apparent. It's no wonder they were never caught before.

Additionally

  • a change was made to fs_sensors.c so that nbfc only reports that it is using a temperature source if no error was encountered when trying to read it.
  • couple of formatting improvements.
  • corrected a couple of typos.
  • added nbfc.py and nbfc_service.service to /.gitignore (now generated via make).
  • added .vscode/ to /.gitignore for developers that use vscode.
  • added nbfc to src/.gitignore (C client generated via make).

src/client.c Show resolved Hide resolved
@braph braph merged commit 3360e35 into nbfc-linux:main Dec 3, 2021
@BachoSeven BachoSeven mentioned this pull request Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants