xref: /illumos-gate/usr/src/cmd/sgs/libldmake/common/ld_file.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <libelf.h>
33 #include <sys/param.h>
34 #include <link.h>
35 
36 #pragma init(ld_support_init)
37 
38 #define	SUNPRO_DEPENDENCIES	"SUNPRO_DEPENDENCIES"
39 
40 /*
41  * Linked list of strings - used to keep lists of names
42  * of directories or files.
43  */
44 struct Stritem {
45 	char	*str;
46 	void	*next;
47 };
48 
49 typedef struct Stritem 	Stritem;
50 
51 static char 	*depend_file = NULL;
52 static Stritem	*list = NULL;
53 
54 void
55 ld_support_init()
56 {
57 	depend_file = getenv(SUNPRO_DEPENDENCIES);
58 }
59 
60 static void
61 prepend_str(Stritem **list, const char *str)
62 {
63 	Stritem		*new;
64 	char		*newstr;
65 	const char	*lib = "libldmake.so";
66 
67 	if (!(new = calloc(1, sizeof (Stritem)))) {
68 		perror(lib);
69 		return;
70 	}
71 
72 	if (!(newstr = malloc(strlen(str) + 1))) {
73 		perror(lib);
74 		return;
75 	}
76 
77 	new->str = strcpy(newstr, str);
78 	new->next = *list;
79 	*list = new;
80 
81 }
82 
83 /* ARGSUSED */
84 void
85 ld_file(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
86 {
87 	/*
88 	 * SUNPRO_DEPENDENCIES wasn't set, we don't collect .make.state
89 	 * information.
90 	 */
91 	if (!depend_file)
92 		return;
93 
94 	if ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED))
95 		prepend_str(&list, file);
96 }
97 
98 void
99 ld_file64(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
100 {
101 	ld_file(file, ekind, flags, elf);
102 }
103 
104 void
105 ld_atexit(int exit_code)
106 {
107 	Stritem 	*cur;
108 	char		lockfile[MAXPATHLEN], *err, *space, *target;
109 	FILE		*ofp;
110 	extern char 	*file_lock(char *, char *, int);
111 
112 	if (!depend_file || exit_code)
113 		return;
114 
115 	if ((space = strchr(depend_file, ' ')) == NULL)
116 		return;
117 	*space = '\0';
118 	target = &space[1];
119 
120 	(void) snprintf(lockfile, MAXPATHLEN, "%s.lock", depend_file);
121 	if ((err = file_lock(depend_file, lockfile, 0))) {
122 		(void) fprintf(stderr, "%s\n", err);
123 		return;
124 	}
125 
126 	if (!(ofp = fopen(depend_file, "a")))
127 		return;
128 
129 	if (list)
130 		(void) fprintf(ofp, "%s: ", target);
131 
132 	for (cur = list; cur; cur = cur->next)
133 		(void) fprintf(ofp, " %s", cur->str);
134 
135 	(void) fputc('\n', ofp);
136 
137 	(void) fclose(ofp);
138 	(void) unlink(lockfile);
139 	*space = ' ';
140 }
141 
142 void
143 ld_atexit64(int exit_code)
144 {
145 	ld_atexit(exit_code);
146 }
147