PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/usr.bin/top/username.c

https://bitbucket.org/pythonos/pythonos
C | 144 lines | 60 code | 22 blank | 62 comment | 11 complexity | ac9dc14fbdb30b794c6bae6c2a6a47b7 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0, MIT, ISC, LGPL-2.1, GPL-2.0, BSD-3-Clause, 0BSD
  1. /* $OpenBSD: username.c,v 1.16 2009/07/22 15:27:52 deraadt Exp $ */
  2. /*
  3. * Top users/processes display for Unix
  4. * Version 3
  5. *
  6. * Copyright (c) 1984, 1989, William LeFebvre, Rice University
  7. * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * Username translation code for top.
  31. *
  32. * These routines handle uid to username mapping.
  33. * They use a hashing table scheme to reduce reading overhead.
  34. * For the time being, these are very straightforward hashing routines.
  35. * Maybe someday I'll put in something better. But with the advent of
  36. * "random access" password files, it might not be worth the effort.
  37. *
  38. * Changes to these have been provided by John Gilmore (gnu@toad.com).
  39. *
  40. * The hash has been simplified in this release, to avoid the
  41. * table overflow problems of previous releases. If the value
  42. * at the initial hash location is not right, it is replaced
  43. * by the right value. Collisions will cause us to call getpw*
  44. * but hey, this is a cache, not the Library of Congress.
  45. * This makes the table size independent of the passwd file size.
  46. */
  47. #include <sys/types.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <pwd.h>
  51. #include "top.local.h"
  52. #include "top.h"
  53. #include "utils.h"
  54. struct hash_el {
  55. uid_t uid;
  56. char name[_PW_NAME_LEN + 1];
  57. };
  58. static int enter_user(uid_t, char *);
  59. static int get_user(uid_t);
  60. #define is_empty_hash(x) (hash_table[x].name[0] == 0)
  61. /*
  62. * Simple minded hashing function, assumes i is unsigned.
  63. */
  64. #define hashit(i) (i % Table_size)
  65. struct hash_el hash_table[Table_size];
  66. char *
  67. username(uid_t uid)
  68. {
  69. int hashindex;
  70. hashindex = hashit(uid);
  71. if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) {
  72. /* not here or not right -- get it out of passwd */
  73. hashindex = get_user(uid);
  74. }
  75. return (hash_table[hashindex].name);
  76. }
  77. uid_t
  78. userid(char *username)
  79. {
  80. struct passwd *pwd;
  81. /*
  82. * Eventually we want this to enter everything in the hash table, but
  83. * for now we just do it simply and remember just the result.
  84. */
  85. if ((pwd = getpwnam(username)) == NULL)
  86. return ((uid_t)-1);
  87. /* enter the result in the hash table */
  88. enter_user(pwd->pw_uid, username);
  89. /* return our result */
  90. return (pwd->pw_uid);
  91. }
  92. static int
  93. enter_user(uid_t uid, char *name)
  94. {
  95. int hashindex;
  96. #ifdef DEBUG
  97. fprintf(stderr, "enter_hash(%u, %s)\n", uid, name);
  98. #endif
  99. hashindex = hashit(uid);
  100. if (!is_empty_hash(hashindex)) {
  101. if (hash_table[hashindex].uid == uid)
  102. return (hashindex); /* Fortuitous find */
  103. }
  104. /* empty or wrong slot -- fill it with new value */
  105. hash_table[hashindex].uid = uid;
  106. (void) strlcpy(hash_table[hashindex].name, name,
  107. sizeof(hash_table[hashindex].name));
  108. return (hashindex);
  109. }
  110. /*
  111. * Get a userid->name mapping from the system.
  112. */
  113. static int
  114. get_user(uid_t uid)
  115. {
  116. struct passwd *pwd;
  117. /* no performance penalty for using getpwuid makes it easy */
  118. if ((pwd = getpwuid(uid)) != NULL)
  119. return (enter_user(pwd->pw_uid, pwd->pw_name));
  120. /* if we can't find the name at all, then use the uid as the name */
  121. return (enter_user(uid, format_uid(uid)));
  122. }