PageRenderTime 71ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/top/username.c

http://github.com/davshao/dflygsocdrm
C | 150 lines | 68 code | 26 blank | 56 comment | 7 complexity | c1dfc350cb85d1e558f10e0222b036d3 MD5 | raw file
Possible License(s): AGPL-1.0, CC-BY-SA-3.0, LGPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception, 0BSD, BSD-3-Clause, GPL-2.0
  1. /*
  2. * Copyright (c) 1984 through 2008, William LeFebvre
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * * Neither the name of William LeFebvre nor the names of other
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * Top users/processes display for Unix
  34. * Version 3
  35. */
  36. /*
  37. * Username translation code for top.
  38. *
  39. * These routines handle uid to username mapping. They use a hash table to
  40. * reduce reading overhead. Entries are refreshed every EXPIRETIME seconds.
  41. *
  42. * The old ad-hoc hash functions have been replaced with something a little
  43. * more formal and (hopefully) more robust (found in hash.c)
  44. */
  45. #include "os.h"
  46. #include <pwd.h>
  47. #include "top.h"
  48. #include "utils.h"
  49. #include "hash.h"
  50. #define EXPIRETIME (60 * 5)
  51. /* we need some sort of idea how long usernames can be */
  52. #ifndef MAXLOGNAME
  53. #ifdef _POSIX_LOGIN_NAME_MAX
  54. #define MAXLOGNAME _POSIX_LOGIN_NAME_MAX
  55. #else
  56. #define MAXLOGNAME 9
  57. #endif
  58. #endif
  59. struct hash_data {
  60. int uid;
  61. char name[MAXLOGNAME]; /* big enough? */
  62. time_t expire;
  63. };
  64. hash_table *userhash;
  65. void
  66. init_username()
  67. {
  68. userhash = hash_create(211);
  69. }
  70. char *
  71. username(int uid)
  72. {
  73. struct hash_data *data;
  74. struct passwd *pw;
  75. time_t now;
  76. /* what time is it? */
  77. now = time(NULL);
  78. /* get whatever is in the cache */
  79. data = hash_lookup_uint(userhash, (unsigned int)uid);
  80. /* if we had a cache miss, then create space for a new entry */
  81. if (data == NULL)
  82. {
  83. /* make space */
  84. data = (struct hash_data *)malloc(sizeof(struct hash_data));
  85. /* fill in some data, including an already expired time */
  86. data->uid = uid;
  87. data->expire = (time_t)0;
  88. /* add it to the hash: the rest gets filled in later */
  89. hash_add_uint(userhash, uid, data);
  90. }
  91. /* Now data points to the correct hash entry for "uid". If this is
  92. a new entry, then expire is 0 and the next test will be true. */
  93. if (data->expire <= now)
  94. {
  95. if ((pw = getpwuid(uid)) != NULL)
  96. {
  97. strncpy(data->name, pw->pw_name, MAXLOGNAME-1);
  98. data->expire = now + EXPIRETIME;
  99. dprintf("username: updating %d with %s, expires %d\n",
  100. data->uid, data->name, data->expire);
  101. }
  102. else
  103. {
  104. /* username doesnt exist ... so invent one */
  105. snprintf(data->name, sizeof(data->name), "%d", uid);
  106. data->expire = now + EXPIRETIME;
  107. dprintf("username: updating %d with %s, expires %d\n",
  108. data->uid, data->name, data->expire);
  109. }
  110. }
  111. /* return what we have */
  112. return data->name;
  113. }
  114. int
  115. userid(char *username)
  116. {
  117. struct passwd *pwd;
  118. if ((pwd = getpwnam(username)) == NULL)
  119. {
  120. return(-1);
  121. }
  122. /* return our result */
  123. return(pwd->pw_uid);
  124. }