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 void
54 mk_state_init(void)
55 {
56 	depend_file = getenv(SUNPRO_DEPENDENCIES);
57 } /* mk_state_init() */
58 
59 static void
60 prepend_str(Stritem **list, const char *str)
61 {
62 	Stritem *new;
63 	char	*newstr;
64 
65 	if (!(new = calloc(1, sizeof (Stritem)))) {
66 		perror("libmakestate.so");
67 		return;
68 	} /* if */
69 
70 	if (!(newstr = malloc(strlen(str) + 1))) {
71 		perror("libmakestate.so");
72 		return;
73 	} /* if */
74 
75 	new->str = strcpy(newstr, str);
76 	new->next = *list;
77 	*list = new;
78 
79 } /* prepend_str() */
80 
81 void
82 mk_state_collect_dep(const char *file)
83 {
84 	/*
85 	 * SUNPRO_DEPENDENCIES wasn't set, we don't collect .make.state
86 	 * information.
87 	 */
88 	if (!depend_file)
89 		return;
90 
91 	prepend_str(&list, file);
92 
93 }  /* mk_state_collect_dep() */
94 
95 void
96 mk_state_update_exit()
97 {
98 	Stritem		*cur;
99 	char		  lockfile[MAXPATHLEN],	*err, *space, *target;
100 	FILE		*ofp;
101 	extern char	*file_lock(char *, char *, int);
102 
103 	if (!depend_file)
104 		return;
105 
106 	if ((space = strchr(depend_file, ' ')) == NULL)
107 		return;
108 	*space = '\0';
109 	target = &space[1];
110 
111 	(void) sprintf(lockfile, "%s.lock", depend_file);
112 	if ((err = file_lock(depend_file, lockfile, 0))) {
113 		(void) fprintf(stderr, "%s\n", err);
114 		return;
115 	} /* if */
116 
117 	if (!(ofp = fopen(depend_file, "a")))
118 		return;
119 
120 	if (list)
121 		(void) fprintf(ofp, "%s: ", target);
122 
123 	for (cur = list; cur; cur = cur->next)
124 		(void) fprintf(ofp, " %s", cur->str);
125 
126 	(void) fputc('\n', ofp);
127 
128 	(void) fclose(ofp);
129 	(void) unlink(lockfile);
130 	*space = ' ';
131 
132 } /* mk_state_update_exit() */
133 
134 static void
135 ld_support_init()
136 {
137 	mk_state_init();
138 
139 } /* ld_support_init() */
140 
141 void
142 ld_file(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
143 {
144 	if (!((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
145 		return;
146 
147 	mk_state_collect_dep(file);
148 
149 } /* ld_file */
150 
151 void
152 ld_atexit(int exit_code)
153 {
154 	if (exit_code)
155 		return;
156 
157 	mk_state_update_exit();
158 
159 } /* ld_atexit() */
160 
161 /*
162  * Supporting 64-bit objects
163  */
164 void
165 ld_file64(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
166 {
167 	if (!((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
168 		return;
169 
170 	mk_state_collect_dep(file);
171 
172 } /* ld_file64 */
173 
174 void
175 ld_atexit64(int exit_code)
176 {
177 	if (exit_code)
178 		return;
179 
180 	mk_state_update_exit();
181 
182 } /* ld_atexit64() */
183