SysInfo.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <http://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 <stdio.h>
  21. struct SysInfo SysInfo_detect()
  22. {
  23. struct SysInfo out = { .os = 0 };
  24. #ifdef linux
  25. out.os = SysInfo_Os_LINUX;
  26. #elif sunos
  27. out.os = SysInfo_Os_SUNOS;
  28. #elif darwin
  29. out.os = SysInfo_Os_DARWIN;
  30. #elif freebsd
  31. out.os = SysInfo_Os_FREEBSD;
  32. #elif win32
  33. out.os = SysInfo_Os_WIN32;
  34. #endif
  35. out.seccomp = Seccomp_exists();
  36. return out;
  37. }
  38. char* getName(enum SysInfo_Os os)
  39. {
  40. switch (os) {
  41. case SysInfo_Os_LINUX: return "linux";
  42. case SysInfo_Os_SUNOS: return "sunos";
  43. case SysInfo_Os_DARWIN: return "darwin";
  44. case SysInfo_Os_FREEBSD: return "freebsd";
  45. case SysInfo_Os_WIN32: return "win32";
  46. default: return "os_unknown";
  47. }
  48. }
  49. #define BUFF_SZ 1024
  50. char* SysInfo_describe(struct SysInfo si, struct Allocator* alloc)
  51. {
  52. uint8_t buff[BUFF_SZ];
  53. snprintf(buff, BUFF_SZ, "%s%s",
  54. getName(si.os),
  55. (si.seccomp) ? " +seccomp" : "");
  56. int len = CString_strlen(buff)+1;
  57. char* out = Allocator_malloc(alloc, len);
  58. Bits_memcpy(out, buff, len);
  59. return out;
  60. }