The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use POSIX thread-safe getgrnam_r, getgrgid_r, getpwnam_r, getpwuid_r

- https://pubs.opengroup.org/onlinepubs/9799919799/functions/getgrnam_r.html
- https://pubs.opengroup.org/onlinepubs/9799919799/functions/getgrgid_r.html
- https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwnam_r.html
- https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwuid_r.html

+177 -29
+23
configure
··· 715 715 #endif" 716 716 717 717 ac_header_c_list= 718 + ac_func_c_list= 718 719 ac_subst_vars='LTLIBOBJS 719 720 LIBOBJS 720 721 DIFF ··· 3168 3169 as_fn_append ac_header_c_list " sys/stat.h sys_stat_h HAVE_SYS_STAT_H" 3169 3170 as_fn_append ac_header_c_list " sys/types.h sys_types_h HAVE_SYS_TYPES_H" 3170 3171 as_fn_append ac_header_c_list " unistd.h unistd_h HAVE_UNISTD_H" 3172 + as_fn_append ac_func_c_list " getgrnam_r HAVE_GETGRNAM_R" 3173 + as_fn_append ac_func_c_list " getgrgid_r HAVE_GETGRGID_R" 3174 + as_fn_append ac_func_c_list " getpwnam_r HAVE_GETPWNAM_R" 3175 + as_fn_append ac_func_c_list " getpwuid_r HAVE_GETPWUID_R" 3171 3176 3172 3177 # Auxiliary files required by this configure script. 3173 3178 ac_aux_files="install-sh ltmain.sh config.guess config.sub" ··· 21124 21129 21125 21130 21126 21131 fi 21132 + 21133 + 21134 + ac_func= 21135 + for ac_item in $ac_func_c_list 21136 + do 21137 + if test $ac_func; then 21138 + ac_fn_c_check_func "$LINENO" $ac_func ac_cv_func_$ac_func 21139 + if eval test \"x\$ac_cv_func_$ac_func\" = xyes; then 21140 + echo "#define $ac_item 1" >> confdefs.h 21141 + fi 21142 + ac_func= 21143 + else 21144 + ac_func=$ac_item 21145 + fi 21146 + done 21147 + 21148 + 21149 + 21127 21150 21128 21151 21129 21152 ## getgroups
+2
configure.ac
··· 2362 2362 AC_DEFINE([HAS_WAIT4], [1]) 2363 2363 ]) 2364 2364 2365 + AC_CHECK_FUNCS_ONCE([getgrnam_r getgrgid_r getpwnam_r getpwuid_r]) 2366 + 2365 2367 ## getgroups 2366 2368 AC_CHECK_FUNC([getgroups], [AC_DEFINE([HAS_GETGROUPS], [1])]) 2367 2369
+73 -14
otherlibs/unix/getgr.c
··· 18 18 #include <caml/alloc.h> 19 19 #include <caml/memory.h> 20 20 #include "caml/unixsupport.h" 21 + #include <unistd.h> 21 22 #include <errno.h> 22 - #include <stdio.h> 23 23 #include <grp.h> 24 24 25 - static value alloc_group_entry(struct group *entry) 25 + static value alloc_group_entry(const struct group *entry) 26 26 { 27 27 CAMLparam0(); 28 28 CAMLlocal3(name, pass, mem); ··· 41 41 CAMLreturn(res); 42 42 } 43 43 44 + /* Arbitrary limit to prevent allocating too much memory */ 45 + #define CAML_GETGR_R_SIZE_MAX (1024 * 64) 46 + 44 47 CAMLprim value caml_unix_getgrnam(value name) 45 48 { 49 + value res; 46 50 struct group * entry; 47 51 if (! caml_string_is_c_safe(name)) caml_raise_not_found(); 52 + 53 + #ifdef HAVE_GETGRNAM_R 54 + long initlen = sysconf(_SC_GETGR_R_SIZE_MAX); 55 + size_t len = initlen <= 0 ? /* default */ 1024 : (size_t) initlen; 56 + struct group result; 57 + char *buffer = caml_stat_alloc_noexc(len); 58 + if (buffer == NULL) 59 + caml_unix_error(ENOMEM, "getgrnam", Nothing); 60 + int e; 61 + while ((e = getgrnam_r(String_val(name), &result, buffer, len, &entry)) 62 + == ERANGE) { 63 + len *= 2; 64 + char *newbuffer; 65 + if (len > CAML_GETGR_R_SIZE_MAX || 66 + (newbuffer = caml_stat_resize_noexc(buffer, len)) == NULL) { 67 + caml_stat_free(buffer); 68 + caml_unix_error(ENOMEM, "getgrnam", Nothing); 69 + } 70 + buffer = newbuffer; 71 + } 72 + if (e != 0) { 73 + caml_stat_free(buffer); 74 + if (e == EINTR) 75 + #else 48 76 errno = 0; 49 77 entry = getgrnam(String_val(name)); 50 78 if (entry == NULL) { 51 - if (errno == EINTR) { 52 - caml_uerror("getgrnam", Nothing); 53 - } else { 54 - caml_raise_not_found(); 55 - } 79 + if (errno == EINTR) 80 + #endif 81 + caml_unix_error(EINTR, "getgrnam", Nothing); 82 + caml_raise_not_found(); 56 83 } 57 - return alloc_group_entry(entry); 84 + res = alloc_group_entry(entry); 85 + #if HAVE_GETGRNAM_R 86 + caml_stat_free(buffer); 87 + #endif 88 + return res; 58 89 } 59 90 60 91 CAMLprim value caml_unix_getgrgid(value gid) 61 92 { 93 + value res; 62 94 struct group * entry; 95 + 96 + #ifdef HAVE_GETGRGID_R 97 + long initlen = sysconf(_SC_GETGR_R_SIZE_MAX); 98 + size_t len = initlen <= 0 ? /* default */ 1024 : (size_t) initlen; 99 + struct group result; 100 + char *buffer = caml_stat_alloc_noexc(len); 101 + if (buffer == NULL) 102 + caml_unix_error(ENOMEM, "getgrid", Nothing); 103 + int e; 104 + while ((e = getgrgid_r(Int_val(gid), &result, buffer, len, &entry)) 105 + == ERANGE) { 106 + len *= 2; 107 + char *newbuffer; 108 + if (len > CAML_GETGR_R_SIZE_MAX || 109 + (newbuffer = caml_stat_resize_noexc(buffer, len)) == NULL) { 110 + caml_stat_free(buffer); 111 + caml_unix_error(ENOMEM, "getgrid", Nothing); 112 + } 113 + buffer = newbuffer; 114 + } 115 + if (e != 0) { 116 + caml_stat_free(buffer); 117 + if (e == EINTR) 118 + #else 63 119 errno = 0; 64 120 entry = getgrgid(Int_val(gid)); 65 121 if (entry == NULL) { 66 - if (errno == EINTR) { 67 - caml_uerror("getgrgid", Nothing); 68 - } else { 69 - caml_raise_not_found(); 70 - } 122 + if (errno == EINTR) 123 + #endif 124 + caml_unix_error(EINTR, "getgrgid", Nothing); 125 + caml_raise_not_found(); 71 126 } 72 - return alloc_group_entry(entry); 127 + res = alloc_group_entry(entry); 128 + #if HAVE_GETGRGID_R 129 + caml_stat_free(buffer); 130 + #endif 131 + return res; 73 132 }
+74 -15
otherlibs/unix/getpw.c
··· 21 21 #include <errno.h> 22 22 #include <pwd.h> 23 23 24 - static value alloc_passwd_entry(struct passwd *entry) 24 + static value alloc_passwd_entry(const struct passwd *entry) 25 25 { 26 26 CAMLparam0(); 27 27 CAMLlocal5(name, passwd, gecos, dir, shell); ··· 47 47 CAMLreturn(res); 48 48 } 49 49 50 + /* Arbitrary limit to prevent allocating too much memory */ 51 + #define CAML_GETPW_R_SIZE_MAX (1024 * 64) 52 + 50 53 CAMLprim value caml_unix_getpwnam(value name) 51 54 { 55 + value res; 52 56 struct passwd * entry; 53 57 if (! caml_string_is_c_safe(name)) caml_raise_not_found(); 58 + 59 + #ifdef HAVE_GETPWNAM_R 60 + long initlen = sysconf(_SC_GETPW_R_SIZE_MAX); 61 + size_t len = initlen <= 0 ? /* default */ 1024 : (size_t) initlen; 62 + struct passwd result; 63 + char *buffer = caml_stat_alloc_noexc(len); 64 + if (buffer == NULL) 65 + caml_unix_error(ENOMEM, "getpwnam", Nothing); 66 + int e; 67 + while ((e = getpwnam_r(String_val(name), &result, buffer, len, &entry)) 68 + == ERANGE) { 69 + len *= 2; 70 + char *newbuffer; 71 + if (len > CAML_GETPW_R_SIZE_MAX || 72 + (newbuffer = caml_stat_resize_noexc(buffer, len)) == NULL) { 73 + caml_stat_free(buffer); 74 + caml_unix_error(ENOMEM, "getpwnam", Nothing); 75 + } 76 + buffer = newbuffer; 77 + } 78 + if (e != 0) { 79 + caml_stat_free(buffer); 80 + if (e == EINTR) 81 + #else 54 82 errno = 0; 55 83 entry = getpwnam(String_val(name)); 56 - if (entry == (struct passwd *) NULL) { 57 - if (errno == EINTR) { 58 - caml_uerror("getpwnam", Nothing); 59 - } else { 60 - caml_raise_not_found(); 61 - } 84 + if (entry == NULL) { 85 + if (errno == EINTR) 86 + #endif 87 + caml_unix_error(EINTR, "getpwnam", Nothing); 88 + caml_raise_not_found(); 62 89 } 63 - return alloc_passwd_entry(entry); 90 + res = alloc_passwd_entry(entry); 91 + #if HAVE_GETPWNAM_R 92 + caml_stat_free(buffer); 93 + #endif 94 + return res; 64 95 } 65 96 66 97 CAMLprim value caml_unix_getpwuid(value uid) 67 98 { 99 + value res; 68 100 struct passwd * entry; 101 + 102 + #ifdef HAVE_GETPWUID_R 103 + long initlen = sysconf(_SC_GETPW_R_SIZE_MAX); 104 + size_t len = initlen <= 0 ? /* default */ 1024 : (size_t) initlen; 105 + struct passwd result; 106 + char *buffer = caml_stat_alloc(len); 107 + if (buffer == NULL) 108 + caml_unix_error(ENOMEM, "getpwuid", Nothing); 109 + int e; 110 + while ((e = getpwuid_r(Int_val(uid), &result, buffer, len, &entry)) 111 + == ERANGE) { 112 + len *= 2; 113 + char *newbuffer; 114 + if (len > CAML_GETPW_R_SIZE_MAX || 115 + (newbuffer = caml_stat_resize_noexc(buffer, len)) == NULL) { 116 + caml_stat_free(buffer); 117 + caml_unix_error(ENOMEM, "getpwuid", Nothing); 118 + } 119 + buffer = newbuffer; 120 + } 121 + if (e != 0) { 122 + caml_stat_free(buffer); 123 + if (e == EINTR) 124 + #else 69 125 errno = 0; 70 126 entry = getpwuid(Int_val(uid)); 71 - if (entry == (struct passwd *) NULL) { 72 - if (errno == EINTR) { 73 - caml_uerror("getpwuid", Nothing); 74 - } else { 75 - caml_raise_not_found(); 76 - } 127 + if (entry == NULL) { 128 + if (errno == EINTR) 129 + #endif 130 + caml_unix_error(EINTR, "getpwuid", Nothing); 131 + caml_raise_not_found(); 77 132 } 78 - return alloc_passwd_entry(entry); 133 + res = alloc_passwd_entry(entry); 134 + #if HAVE_GETPWUID_R 135 + caml_stat_free(buffer); 136 + #endif 137 + return res; 79 138 }
+5
runtime/caml/s.h.in
··· 324 324 325 325 #undef HAVE_MAX_ALIGN_T 326 326 327 + #undef HAVE_GETGRNAM_R 328 + #undef HAVE_GETGRGID_R 329 + #undef HAVE_GETPWNAM_R 330 + #undef HAVE_GETPWUID_R 331 + 327 332 /* 3. Language extensions. */ 328 333 329 334 /* Define if the C compiler supports the labels as values extension. */