4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1998,2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <rpc/nettype.h>
42 * To use debugging facility, compile with * -DMALLOC_DEBUG.
43 * You can do this by setting the environment variable
44 * MALLOC_DEBUG to "-DMALLOC_DEBUG"
46 * To make automountd dump trace records (i.e. make it call check_leaks),
58 struct alloc_list
*next
;
61 static struct alloc_list
*halist
= NULL
;
62 static mutex_t alloc_list_lock
= DEFAULTMUTEX
;
65 add_alloc(char *type
, void *addr
, size_t size
, const char *file
, int line
)
67 struct alloc_list
*alist
= NULL
;
69 /* allocate the list item */
70 alist
= (struct alloc_list
*)malloc(sizeof (*alist
));
72 syslog(LOG_ERR
, "add_alloc: out of memory\n");
75 strcpy(alist
->type
, type
);
78 strcpy(alist
->file
, file
);
81 /* add it to the head of the list */
91 drop_alloc(const char *type
, void *addr
, const char *file
, int line
)
93 struct alloc_list
*alist
, *alist_prev
;
96 while (alist
!= NULL
) {
97 if (addr
== alist
->addr
) {
99 halist
= halist
->next
;
101 alist_prev
->next
= alist
->next
;
110 syslog(LOG_ERR
, "*** POSSIBLE CORRUPTION ****\n");
111 syslog(LOG_ERR
, "\tduplicate free, type %s, at %p in %s/%d\n",
112 type
, addr
, file
, line
);
119 my_malloc(size_t size
, const char *file
, int line
)
123 addr
= (void *)malloc(size
);
126 mutex_lock(&alloc_list_lock
);
127 add_alloc("MALLOC", addr
, size
, file
, line
);
128 mutex_unlock(&alloc_list_lock
);
133 my_realloc(void *addr
, size_t size
, const char *file
, int line
)
137 ptr
= (void *)realloc(addr
, size
);
140 mutex_lock(&alloc_list_lock
);
141 drop_alloc("MALLOC", addr
, file
, line
);
142 add_alloc("MALLOC", ptr
, size
, file
, line
);
143 mutex_unlock(&alloc_list_lock
);
149 my_free(void *addr
, const char *file
, int line
)
151 mutex_lock(&alloc_list_lock
);
152 drop_alloc("MALLOC", addr
, file
, line
);
153 mutex_unlock(&alloc_list_lock
);
158 my_strdup(const char *straddr
, const char *file
, int line
)
163 addr
= strdup(straddr
);
166 size
= strlen(straddr
);
167 mutex_lock(&alloc_list_lock
);
168 add_alloc("STRDUP", addr
, size
, file
, line
);
169 mutex_unlock(&alloc_list_lock
);
171 return ((char *)addr
);
175 my_sethostent(int stay
, const char *file
, int line
)
177 (void) sethostent(stay
);
178 mutex_lock(&alloc_list_lock
);
179 add_alloc("SETHOSTENT", NULL
, 0, file
, line
);
180 mutex_unlock(&alloc_list_lock
);
185 my_endhostent(const char *file
, int line
)
192 mutex_lock(&alloc_list_lock
);
193 drop_alloc("SETHOSTENT", NULL
, file
, line
);
194 mutex_unlock(&alloc_list_lock
);
199 my_setnetconfig(const char *file
, int line
)
203 nconf
= setnetconfig();
206 mutex_lock(&alloc_list_lock
);
207 add_alloc("SETNETCONFIG", nconf
, 0, file
, line
);
208 mutex_unlock(&alloc_list_lock
);
213 my_endnetconfig(void *nconf
, const char *file
, int line
)
217 res
= endnetconfig(nconf
);
220 mutex_lock(&alloc_list_lock
);
221 drop_alloc("SETNETCONFIG", nconf
, file
, line
);
222 mutex_unlock(&alloc_list_lock
);
227 my_setnetpath(const char *file
, int line
)
231 npath
= setnetpath();
234 mutex_lock(&alloc_list_lock
);
235 add_alloc("SETNETPATH", npath
, 0, file
, line
);
236 mutex_unlock(&alloc_list_lock
);
241 my_endnetpath(void *npath
, const char *file
, int line
)
245 res
= endnetpath(npath
);
248 mutex_lock(&alloc_list_lock
);
249 drop_alloc("SETNETPATH", npath
, file
, line
);
250 mutex_unlock(&alloc_list_lock
);
256 struct netconfig
*tp
,
257 struct nd_hostserv
*serv
,
258 struct nd_addrlist
**addrs
,
264 res
= netdir_getbyname(tp
, serv
, addrs
);
267 mutex_lock(&alloc_list_lock
);
268 add_alloc("NETDIR_GETBYNAME", *addrs
, 0, file
, line
);
269 mutex_unlock(&alloc_list_lock
);
274 my_netdir_free(void *ptr
, int type
, const char *file
, int line
)
276 netdir_free(ptr
, type
);
277 mutex_lock(&alloc_list_lock
);
278 drop_alloc("NETDIR_GETBYNAME", ptr
, file
, line
);
279 mutex_unlock(&alloc_list_lock
);
293 res
= getipnodebyname(name
, af
, flags
, error_num
);
296 mutex_lock(&alloc_list_lock
);
297 add_alloc("GETIPNODEBYNAME", res
, 0, file
, line
);
298 mutex_unlock(&alloc_list_lock
);
303 my_freehostent(struct hostent
*hent
, char *file
, int line
)
306 mutex_lock(&alloc_list_lock
);
307 drop_alloc("GETIPNODEBYNAME", hent
, file
, line
);
308 mutex_unlock(&alloc_list_lock
);
312 my_getnetconfigent(char *netid
, char *file
, int line
)
314 struct netconfig
*res
;
316 res
= getnetconfigent(netid
);
319 mutex_lock(&alloc_list_lock
);
320 add_alloc("GETNETCONFIGENT", res
, 0, file
, line
);
321 mutex_unlock(&alloc_list_lock
);
326 my_freenetconfigent(struct netconfig
*netp
, char *file
, int line
)
328 freenetconfigent(netp
);
329 mutex_lock(&alloc_list_lock
);
330 drop_alloc("GETNETCONFIGENT", netp
, file
, line
);
331 mutex_unlock(&alloc_list_lock
);
335 my__rpc_setconf(char *nettype
, char *file
, int line
)
339 res
= __rpc_setconf(nettype
);
342 mutex_lock(&alloc_list_lock
);
343 add_alloc("RPC_SETCONF", res
, 0, file
, line
);
344 mutex_unlock(&alloc_list_lock
);
349 my__rpc_endconf(void *vhandle
, char *file
, int line
)
351 __rpc_endconf(vhandle
);
352 mutex_lock(&alloc_list_lock
);
353 drop_alloc("RPC_SETCONF", vhandle
, file
, line
);
354 mutex_unlock(&alloc_list_lock
);
357 extern void flush_caches();
362 #pragma weak flush_caches = _flush_caches
365 check_leaks(char *filename
)
367 struct alloc_list
*alist
;
370 fp
= fopen(filename
, "a");
372 syslog(LOG_ERR
, "check_leaks, could not open file: %s",
378 fprintf(fp
, "*** POSSIBLE LEAKS ****\n");
379 mutex_lock(&alloc_list_lock
);
381 while (alist
!= NULL
) {
382 fprintf(fp
, "\t%s: %d bytes at %p in %s/%d\n",
383 alist
->type
, alist
->size
, alist
->addr
,
384 alist
->file
, alist
->line
);
387 mutex_unlock(&alloc_list_lock
);
393 * To prevent a compiler warning.