xref: /illumos-gate/usr/src/cmd/make/lib/makestate/ld_file.c (revision 856f710c9dc323b39da5935194d7928ffb99b67f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 1998 Sun Microsystems, Inc. All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma init(ld_support_init)
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 #define	SUNPRO_DEPENDENCIES	"SUNPRO_DEPENDENCIES"
37 
38 /*
39  * Linked list of strings - used to keep lists of names
40  * of directories or files.
41  */
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 
54 void mk_state_init()
55 {
56 	depend_file = getenv(SUNPRO_DEPENDENCIES);
57 } /* mk_state_init() */
58 
59 
60 
61 static void
62 prepend_str(Stritem **list, const char * str)
63 {
64 	Stritem * new;
65 	char 	* newstr;
66 
67 	if (!(new = calloc(1, sizeof (Stritem)))) {
68 		perror("libmakestate.so");
69 		return;
70 	} /* if */
71 
72 	if (!(newstr = malloc(strlen(str) + 1))) {
73 		perror("libmakestate.so");
74 		return;
75 	} /* if */
76 
77 	new->str = strcpy(newstr, str);
78 	new->next = *list;
79 	*list = new;
80 
81 } /* prepend_str() */
82 
83 
84 void
85 mk_state_collect_dep(const char * file)
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 	prepend_str(&list, file);
95 
96 }  /* mk_state_collect_dep() */
97 
98 
99 void
100 mk_state_update_exit()
101 {
102 	Stritem 	* cur;
103 	char		  lockfile[MAXPATHLEN],	* err, * space, * target;
104 	FILE		* ofp;
105 	extern char 	* file_lock(char *, char *, int);
106 
107 	if (!depend_file)
108 		return;
109 
110 	if ((space = strchr(depend_file, ' ')) == NULL)
111 		return;
112 	*space = '\0';
113 	target = &space[1];
114 
115 	(void) sprintf(lockfile, "%s.lock", depend_file);
116 	if ((err = file_lock(depend_file, lockfile, 0))) {
117 		(void) fprintf(stderr, "%s\n", err);
118 		return;
119 	} /* if */
120 
121 	if (!(ofp = fopen(depend_file, "a")))
122 		return;
123 
124 	if (list)
125 		(void) fprintf(ofp, "%s: ", target);
126 
127 	for (cur = list; cur; cur = cur->next)
128 		(void) fprintf(ofp, " %s", cur->str);
129 
130 	(void) fputc('\n', ofp);
131 
132 	(void) fclose(ofp);
133 	(void) unlink(lockfile);
134 	*space = ' ';
135 
136 } /* mk_state_update_exit() */
137 
138 static void
139 /* LINTED static unused */
140 ld_support_init()
141 {
142 	mk_state_init();
143 
144 } /* ld_support_init() */
145 
146 /* ARGSUSED */
147 void
148 ld_file(const char * file, const Elf_Kind ekind, int flags, Elf *elf)
149 {
150 	if(! ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
151 		return;
152 
153 	mk_state_collect_dep(file);
154 
155 } /* ld_file */
156 
157 void
158 ld_atexit(int exit_code)
159 {
160 	if (exit_code)
161 	   return;
162 
163 	mk_state_update_exit();
164 
165 } /* ld_atexit() */
166 
167 /*
168  * Supporting 64-bit objects
169  */
170 void
171 ld_file64(const char * file, const Elf_Kind ekind, int flags, Elf *elf)
172 {
173 	if(! ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
174 		return;
175 
176 	mk_state_collect_dep(file);
177 
178 } /* ld_file64 */
179 
180 void
181 ld_atexit64(int exit_code)
182 {
183 	if (exit_code)
184 	   return;
185 
186 	mk_state_update_exit();
187 
188 } /* ld_atexit64() */
189