target.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Helper functions for handling target threads/cpus
  3. *
  4. * Copyright (C) 2012, LG Electronics, Namhyung Kim <namhyung.kim@lge.com>
  5. *
  6. * Released under the GPL v2.
  7. */
  8. #include "target.h"
  9. #include "debug.h"
  10. #include <pwd.h>
  11. enum perf_target_errno perf_target__validate(struct perf_target *target)
  12. {
  13. enum perf_target_errno ret = PERF_ERRNO_TARGET__SUCCESS;
  14. if (target->pid)
  15. target->tid = target->pid;
  16. /* CPU and PID are mutually exclusive */
  17. if (target->tid && target->cpu_list) {
  18. target->cpu_list = NULL;
  19. if (ret == PERF_ERRNO_TARGET__SUCCESS)
  20. ret = PERF_ERRNO_TARGET__PID_OVERRIDE_CPU;
  21. }
  22. /* UID and PID are mutually exclusive */
  23. if (target->tid && target->uid_str) {
  24. target->uid_str = NULL;
  25. if (ret == PERF_ERRNO_TARGET__SUCCESS)
  26. ret = PERF_ERRNO_TARGET__PID_OVERRIDE_UID;
  27. }
  28. /* UID and CPU are mutually exclusive */
  29. if (target->uid_str && target->cpu_list) {
  30. target->cpu_list = NULL;
  31. if (ret == PERF_ERRNO_TARGET__SUCCESS)
  32. ret = PERF_ERRNO_TARGET__UID_OVERRIDE_CPU;
  33. }
  34. /* PID and SYSTEM are mutually exclusive */
  35. if (target->tid && target->system_wide) {
  36. target->system_wide = false;
  37. if (ret == PERF_ERRNO_TARGET__SUCCESS)
  38. ret = PERF_ERRNO_TARGET__PID_OVERRIDE_SYSTEM;
  39. }
  40. /* UID and SYSTEM are mutually exclusive */
  41. if (target->uid_str && target->system_wide) {
  42. target->system_wide = false;
  43. if (ret == PERF_ERRNO_TARGET__SUCCESS)
  44. ret = PERF_ERRNO_TARGET__UID_OVERRIDE_SYSTEM;
  45. }
  46. return ret;
  47. }
  48. enum perf_target_errno perf_target__parse_uid(struct perf_target *target)
  49. {
  50. struct passwd pwd, *result;
  51. char buf[1024];
  52. const char *str = target->uid_str;
  53. target->uid = UINT_MAX;
  54. if (str == NULL)
  55. return PERF_ERRNO_TARGET__SUCCESS;
  56. /* Try user name first */
  57. getpwnam_r(str, &pwd, buf, sizeof(buf), &result);
  58. if (result == NULL) {
  59. /*
  60. * The user name not found. Maybe it's a UID number.
  61. */
  62. char *endptr;
  63. int uid = strtol(str, &endptr, 10);
  64. if (*endptr != '\0')
  65. return PERF_ERRNO_TARGET__INVALID_UID;
  66. getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
  67. if (result == NULL)
  68. return PERF_ERRNO_TARGET__USER_NOT_FOUND;
  69. }
  70. target->uid = result->pw_uid;
  71. return PERF_ERRNO_TARGET__SUCCESS;
  72. }