SysInfo.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/SysInfo.h"
  17. #include "util/Seccomp.h"
  18. #include "util/CString.h"
  19. #include "util/Bits.h"
  20. #include "util/Defined.h"
  21. #include <stdio.h>
  22. struct SysInfo SysInfo_detect(void)
  23. {
  24. struct SysInfo out = { .os = 0 };
  25. if (Defined(linux)) {
  26. out.os = SysInfo_Os_LINUX;
  27. } else if (Defined(sunos)) {
  28. out.os = SysInfo_Os_SUNOS;
  29. } else if (Defined(darwin)) {
  30. out.os = SysInfo_Os_DARWIN;
  31. } else if (Defined(freebsd)) {
  32. out.os = SysInfo_Os_FREEBSD;
  33. } else if (Defined(win32)) {
  34. out.os = SysInfo_Os_WIN32;
  35. } else {
  36. out.os = SysInfo_Os_UNKNOWN;
  37. }
  38. out.seccomp = Seccomp_exists();
  39. return out;
  40. }
  41. static char* getName(enum SysInfo_Os os)
  42. {
  43. switch (os) {
  44. case SysInfo_Os_LINUX: return "linux";
  45. case SysInfo_Os_SUNOS: return "sunos";
  46. case SysInfo_Os_DARWIN: return "darwin";
  47. case SysInfo_Os_FREEBSD: return "freebsd";
  48. case SysInfo_Os_WIN32: return "win32";
  49. default: return "os_unknown";
  50. }
  51. }
  52. #define BUFF_SZ 1024
  53. char* SysInfo_describe(struct SysInfo si, struct Allocator* alloc)
  54. {
  55. uint8_t buff[BUFF_SZ];
  56. snprintf(buff, BUFF_SZ, "%s%s",
  57. getName(si.os),
  58. (si.seccomp) ? " +seccomp" : "");
  59. int len = CString_strlen(buff)+1;
  60. char* out = Allocator_malloc(alloc, len);
  61. Bits_memcpy(out, buff, len);
  62. return out;
  63. }