Remove sys_dl*() - stubs are already provided by libreplace.
[ira/wip.git] / source3 / torture / nsstest.c
index d2b17f0f6352d9f8f69788acda669906a5aeb754..352b3fa33c08b56dada14458e51aae6af6f23eee 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -29,25 +28,29 @@ static int total_errors;
 
 static void *find_fn(const char *name)
 {
-       pstring s;
+       char *s;
        static void *h;
        void *res;
 
-       pstr_sprintf(s, "_nss_%s_%s", nss_name, name);
+       if (asprintf(&s, "_nss_%s_%s", nss_name, name) < 0) {
+               exit(1);
+       }
 
        if (!h) {
-               h = sys_dlopen(so_path, RTLD_LAZY);
+               h = dlopen(so_path, RTLD_LAZY);
        }
        if (!h) {
                printf("Can't open shared library %s\n", so_path);
                exit(1);
        }
-       res = sys_dlsym(h, s);
+       res = dlsym(h, s);
        if (!res) {
                printf("Can't find function %s\n", s);
                total_errors++;
+               SAFE_FREE(s);
                return NULL;
        }
+       SAFE_FREE(s);
        return res;
 }
 
@@ -62,7 +65,9 @@ static void report_nss_error(const char *who, NSS_STATUS status)
 static struct passwd *nss_getpwent(void)
 {
        NSS_STATUS (*_nss_getpwent_r)(struct passwd *, char *, 
-                                     size_t , int *) = find_fn("getpwent_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(struct passwd *, char *,
+                               size_t, int *))find_fn("getpwent_r");
        static struct passwd pwd;
        static char buf[1000];
        NSS_STATUS status;
@@ -84,7 +89,9 @@ static struct passwd *nss_getpwent(void)
 static struct passwd *nss_getpwnam(const char *name)
 {
        NSS_STATUS (*_nss_getpwnam_r)(const char *, struct passwd *, char *, 
-                                     size_t , int *) = find_fn("getpwnam_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(const char *, struct passwd *, char *,
+                               size_t, int *))find_fn("getpwnam_r");
        static struct passwd pwd;
        static char buf[1000];
        NSS_STATUS status;
@@ -106,7 +113,9 @@ static struct passwd *nss_getpwnam(const char *name)
 static struct passwd *nss_getpwuid(uid_t uid)
 {
        NSS_STATUS (*_nss_getpwuid_r)(uid_t , struct passwd *, char *, 
-                                     size_t , int *) = find_fn("getpwuid_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(uid_t, struct passwd *, char *,
+                               size_t, int *))find_fn("getpwuid_r");
        static struct passwd pwd;
        static char buf[1000];
        NSS_STATUS status;
@@ -127,7 +136,8 @@ static struct passwd *nss_getpwuid(uid_t uid)
 
 static void nss_setpwent(void)
 {
-       NSS_STATUS (*_nss_setpwent)(void) = find_fn("setpwent");
+       NSS_STATUS (*_nss_setpwent)(void) =
+               (NSS_STATUS(*)(void))find_fn("setpwent");
        NSS_STATUS status;
        
        if (!_nss_setpwent)
@@ -141,7 +151,8 @@ static void nss_setpwent(void)
 
 static void nss_endpwent(void)
 {
-       NSS_STATUS (*_nss_endpwent)(void) = find_fn("endpwent");
+       NSS_STATUS (*_nss_endpwent)(void) =
+               (NSS_STATUS (*)(void))find_fn("endpwent");
        NSS_STATUS status;
 
        if (!_nss_endpwent)
@@ -157,7 +168,9 @@ static void nss_endpwent(void)
 static struct group *nss_getgrent(void)
 {
        NSS_STATUS (*_nss_getgrent_r)(struct group *, char *, 
-                                     size_t , int *) = find_fn("getgrent_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(struct group *, char *,
+                               size_t, int *))find_fn("getgrent_r");
        static struct group grp;
        static char *buf;
        static int buflen = 1024;
@@ -167,23 +180,25 @@ static struct group *nss_getgrent(void)
                return NULL;
 
        if (!buf) 
-               buf = SMB_MALLOC(buflen);
+               buf = SMB_MALLOC_ARRAY(char, buflen);
 
 again: 
        status = _nss_getgrent_r(&grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
                buflen *= 2;
-               buf = SMB_REALLOC(buf, buflen);
+               buf = SMB_REALLOC_ARRAY(buf, char, buflen);
                if (!buf) {
                        return NULL;
                }
                goto again;
        }
        if (status == NSS_STATUS_NOTFOUND) {
+               SAFE_FREE(buf);
                return NULL;
        }
        if (status != NSS_STATUS_SUCCESS) {
                report_nss_error("getgrent", status);
+               SAFE_FREE(buf);
                return NULL;
        }
        return &grp;
@@ -192,7 +207,9 @@ again:
 static struct group *nss_getgrnam(const char *name)
 {
        NSS_STATUS (*_nss_getgrnam_r)(const char *, struct group *, char *, 
-                                     size_t , int *) = find_fn("getgrnam_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(const char *, struct group *, char *,
+                               size_t, int *))find_fn("getgrnam_r");
        static struct group grp;
        static char *buf;
        static int buflen = 1000;
@@ -202,22 +219,24 @@ static struct group *nss_getgrnam(const char *name)
                return NULL;
 
        if (!buf) 
-               buf = SMB_MALLOC(buflen);
+               buf = SMB_MALLOC_ARRAY(char, buflen);
 again: 
        status = _nss_getgrnam_r(name, &grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
                buflen *= 2;
-               buf = SMB_REALLOC(buf, buflen);
+               buf = SMB_REALLOC_ARRAY(buf, char, buflen);
                if (!buf) {
                        return NULL;
                }
                goto again;
        }
        if (status == NSS_STATUS_NOTFOUND) {
+               SAFE_FREE(buf);
                return NULL;
        }
        if (status != NSS_STATUS_SUCCESS) {
                report_nss_error("getgrnam", status);
+               SAFE_FREE(buf);
                return NULL;
        }
        return &grp;
@@ -226,7 +245,9 @@ again:
 static struct group *nss_getgrgid(gid_t gid)
 {
        NSS_STATUS (*_nss_getgrgid_r)(gid_t , struct group *, char *, 
-                                     size_t , int *) = find_fn("getgrgid_r");
+                                     size_t , int *) =
+               (NSS_STATUS (*)(gid_t, struct group *, char *,
+                               size_t, int *))find_fn("getgrgid_r");
        static struct group grp;
        static char *buf;
        static int buflen = 1000;
@@ -236,23 +257,25 @@ static struct group *nss_getgrgid(gid_t gid)
                return NULL;
 
        if (!buf) 
-               buf = SMB_MALLOC(buflen);
+               buf = SMB_MALLOC_ARRAY(char, buflen);
 
 again: 
        status = _nss_getgrgid_r(gid, &grp, buf, buflen, &nss_errno);
        if (status == NSS_STATUS_TRYAGAIN) {
                buflen *= 2;
-               buf = SMB_REALLOC(buf, buflen);
+               buf = SMB_REALLOC_ARRAY(buf, char, buflen);
                if (!buf) {
                        return NULL;
                }
                goto again;
        }
        if (status == NSS_STATUS_NOTFOUND) {
+               SAFE_FREE(buf);
                return NULL;
        }
        if (status != NSS_STATUS_SUCCESS) {
                report_nss_error("getgrgid", status);
+               SAFE_FREE(buf);
                return NULL;
        }
        return &grp;
@@ -260,7 +283,8 @@ again:
 
 static void nss_setgrent(void)
 {
-       NSS_STATUS (*_nss_setgrent)(void) = find_fn("setgrent");
+       NSS_STATUS (*_nss_setgrent)(void) =
+               (NSS_STATUS (*)(void))find_fn("setgrent");
        NSS_STATUS status;
 
        if (!_nss_setgrent)
@@ -274,7 +298,8 @@ static void nss_setgrent(void)
 
 static void nss_endgrent(void)
 {
-       NSS_STATUS (*_nss_endgrent)(void) = find_fn("endgrent");
+       NSS_STATUS (*_nss_endgrent)(void) =
+               (NSS_STATUS (*)(void))find_fn("endgrent");
        NSS_STATUS status;
 
        if (!_nss_endgrent)
@@ -290,7 +315,9 @@ static int nss_initgroups(char *user, gid_t group, gid_t **groups, long int *sta
 {
        NSS_STATUS (*_nss_initgroups)(char *, gid_t , long int *,
                                      long int *, gid_t **, long int , int *) = 
-               find_fn("initgroups_dyn");
+               (NSS_STATUS (*)(char *, gid_t, long int *,
+                               long int *, gid_t **,
+                               long int, int *))find_fn("initgroups_dyn");
        NSS_STATUS status;
 
        if (!_nss_initgroups)