callback_log.c 750 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * callback_log.c
  3. *
  4. * Callback hook to log users's commands
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <syslog.h>
  9. #include <sys/types.h>
  10. #include <pwd.h>
  11. #include "internal.h"
  12. #define SYSLOG_IDENT "klish"
  13. /*--------------------------------------------------------- */
  14. int clish_log_callback(clish_context_t *context, const char *line,
  15. int retcode)
  16. {
  17. clish_shell_t *this = context->shell;
  18. struct passwd *user = NULL;
  19. char *uname = "unknown";
  20. /* Initialization */
  21. if (!line) {
  22. openlog(SYSLOG_IDENT, LOG_PID,
  23. clish_shell__get_facility(this));
  24. return 0;
  25. }
  26. /* Log the given line */
  27. if ((user = clish_shell__get_user(this)))
  28. uname = user->pw_name;
  29. syslog(LOG_INFO, "(%s) %s : %d", uname, line, retcode);
  30. return 0;
  31. }