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