log.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. *
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/utsname.h>
  12. #include <syslog.h>
  13. #include <faux/str.h>
  14. #include <faux/list.h>
  15. #include <faux/sysdb.h>
  16. #include <klish/kcontext.h>
  17. #include <klish/ksession.h>
  18. #include <klish/kexec.h>
  19. #include <klish/kpath.h>
  20. int klish_syslog(kcontext_t *context)
  21. {
  22. const kcontext_t *parent_context = NULL;
  23. const ksession_t *session = NULL;
  24. const kexec_t *parent_exec = NULL;
  25. char *log_full_line = NULL;
  26. assert(context);
  27. parent_context = kcontext_parent_context(context);
  28. if (!parent_context)
  29. return -1;
  30. session = kcontext_session(context);
  31. if (!session)
  32. return -1;
  33. parent_exec = kcontext_parent_exec(context);
  34. if (parent_exec && (kexec_contexts_len(parent_exec) > 1)) {
  35. log_full_line = faux_str_sprintf(", (%s)#%u",
  36. kexec_line(parent_exec),
  37. kcontext_pipeline_stage(parent_context));
  38. }
  39. syslog(LOG_INFO, "%u(%s) %s : %d%s",
  40. ksession_uid(session), ksession_user(session),
  41. kcontext_line(parent_context), kcontext_retcode(parent_context),
  42. log_full_line ? log_full_line : "");
  43. faux_str_free(log_full_line);
  44. return 0;
  45. }