my_getpwnam.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/my_getpwnam.cc
  24. */
  25. #include "my_getpwnam.h"
  26. #include <atomic>
  27. #include <vector>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. namespace {
  31. std::size_t start_bufsz() {
  32. long scsz = sysconf(_SC_GETPW_R_SIZE_MAX);
  33. return (scsz == -1L ? 256 : scsz);
  34. }
  35. template <class GETPW_CLOS>
  36. PasswdValue my_getpw_(GETPW_CLOS &&getpwfunc) {
  37. passwd pwd;
  38. std::size_t bufsz = start_bufsz();
  39. std::vector<char> buf(bufsz);
  40. passwd *resptr = nullptr;
  41. while (true) {
  42. errno = getpwfunc(&pwd, &buf, &resptr);
  43. switch (errno) {
  44. case ERANGE:
  45. bufsz *= 2;
  46. buf.resize(bufsz);
  47. // fallthrough
  48. case EINTR:
  49. continue;
  50. default:
  51. break;
  52. }
  53. break;
  54. }
  55. return resptr ? PasswdValue{pwd} : PasswdValue{};
  56. }
  57. } // namespace
  58. /**
  59. Wrapper around the getpwnam_r() POSIX function which places the
  60. contents of the passwd struct into an object with value semantics
  61. and returns this.
  62. @param name Symbolic user id
  63. @retval PasswdValue representing user's passwd entry.
  64. PasswdValue::IsVoid() returns true if no such user exists or an error
  65. occured. In the latter case errno is set.
  66. */
  67. PasswdValue my_getpwnam(const char *name) {
  68. return my_getpw_(
  69. [&name](passwd *pwd, std::vector<char> *bufp, passwd **resptr) {
  70. return getpwnam_r(name, pwd, &bufp->front(), bufp->size(), resptr);
  71. });
  72. }
  73. /**
  74. Wrapper around the getpwuid_r() POSIX function which places the
  75. contents of the passwd struct into an object with value semantics
  76. and returns this.
  77. @param uid Numeric user id
  78. @retval PasswdValue representing user's passwd entry.
  79. PasswdValue::IsVoid() returns true if no such user exists or an error
  80. occured. In the latter case errno is set.
  81. */
  82. PasswdValue my_getpwuid(uid_t uid) {
  83. return my_getpw_(
  84. [uid](passwd *pwd, std::vector<char> *bufp, passwd **resptr) {
  85. return getpwuid_r(uid, pwd, &bufp->front(), bufp->size(), resptr);
  86. });
  87. }