"Fossies" - the Fresh Open Source Software Archive 
Member "schily-2021-09-18/sunpro/Make/lib/makestate/src/ld_file.c" (19 Aug 2021, 5185 Bytes) of package /linux/privat/schily-2021-09-18.tar.bz2:
1 /*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may use this file only in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.opensource.org/licenses/cddl1.txt
12 * See the License for the specific language governing permissions
13 * and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
20 *
21 * CDDL HEADER END
22 */
23 /*
24 * Copyright 1998 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27 /*
28 * @(#)ld_file.c 1.7 06/12/12
29 */
30 #pragma ident "@(#)ld_file.c 1.7 06/12/12"
31
32 /*
33 * Copyright 2017-2021 J. Schilling
34 *
35 * @(#)ld_file.c 1.8 21/08/19 2017-2021 J. Schilling
36 */
37 #include <schily/mconfig.h>
38 #ifndef lint
39 static UConst char sccsid[] =
40 "@(#)ld_file.c 1.8 21/08/19 2017-2021 J. Schilling";
41 #endif
42
43 #pragma init(ld_support_init)
44
45 #if defined(SCHILY_BUILD) || defined(SCHILY_INCLUDES)
46 #include <schily/stdio.h>
47 #include <schily/unistd.h>
48 #include <schily/stdlib.h>
49 #include <schily/string.h>
50 #ifdef HAVE_LIBELF_H
51 #include <libelf.h>
52 #endif
53 #include <schily/param.h>
54 #ifdef HAVE_LINK_H
55 #include <link.h>
56 #endif
57 #else
58 #include <stdio.h>
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #ifdef HAVE_LIBELF_H
63 #include <libelf.h>
64 #endif
65 #include <sys/param.h>
66 #ifdef HAVE_LINK_H
67 #include <link.h>
68 #endif
69 #endif
70
71 #if defined(HAVE_LIBELF_H) && defined(LD_SUP_DERIVED)
72 /*
73 * FreeBSD has libelf.h and link.h but link.h does not
74 * #define LD_SUP_DERIVED.
75 */
76 #define SUNPRO_DEPENDENCIES "SUNPRO_DEPENDENCIES"
77
78 /*
79 * Linked list of strings - used to keep lists of names
80 * of directories or files.
81 */
82
83 struct Stritem {
84 char *str;
85 void *next;
86 };
87
88 typedef struct Stritem Stritem;
89
90 static char *depend_file = NULL;
91 static Stritem *list = NULL;
92
93 void mk_state_init __PR((void));
94 static void prepend_str __PR((Stritem **listpp,
95 const char *str));
96 void mk_state_collect_dep __PR((const char *file));
97 void mk_state_update_exit __PR((void));
98 static void ld_support_init __PR((void));
99 void ld_file __PR((const char *file,
100 const Elf_Kind ekind,
101 int flags, Elf *elf));
102 void ld_atexit __PR((int exit_code));
103 void ld_file64 __PR((const char *file,
104 const Elf_Kind ekind,
105 int flags, Elf *elf));
106 void ld_atexit64 __PR((int exit_code));
107
108 void
109 mk_state_init()
110 {
111 depend_file = getenv(SUNPRO_DEPENDENCIES);
112 } /* mk_state_init() */
113
114
115
116 static void
117 prepend_str(listpp, str)
118 Stritem **listpp;
119 const char *str;
120 {
121 Stritem *new;
122 char *newstr;
123
124 if (!(new = calloc(1, sizeof (Stritem)))) {
125 perror("libmakestate.so");
126 return;
127 } /* if */
128
129 if (!(newstr = malloc(strlen(str) + 1))) {
130 perror("libmakestate.so");
131 return;
132 } /* if */
133
134 new->str = strcpy(newstr, str);
135 new->next = *listpp;
136 *listpp = new;
137
138 } /* prepend_str() */
139
140
141 void
142 mk_state_collect_dep(file)
143 const char *file;
144 {
145 /*
146 * SUNPRO_DEPENDENCIES wasn't set, we don't collect .make.state
147 * information.
148 */
149 if (!depend_file)
150 return;
151
152 prepend_str(&list, file);
153
154 } /* mk_state_collect_dep() */
155
156
157 void
158 mk_state_update_exit()
159 {
160 Stritem *cur;
161 char lockfile[MAXPATHLEN], *err, *space, *target;
162 FILE *ofp;
163 extern char *file_lock(char *, char *, int);
164
165 if (!depend_file)
166 return;
167
168 if ((space = strchr(depend_file, ' ')) == NULL)
169 return;
170 *space = '\0';
171 target = &space[1];
172
173 (void) sprintf(lockfile, "%s.lock", depend_file);
174 if ((err = file_lock(depend_file, lockfile, 0))) {
175 (void) fprintf(stderr, "%s\n", err);
176 return;
177 } /* if */
178
179 if (!(ofp = fopen(depend_file, "a")))
180 return;
181
182 if (list)
183 (void) fprintf(ofp, "%s: ", target);
184
185 for (cur = list; cur; cur = cur->next)
186 (void) fprintf(ofp, " %s", cur->str);
187
188 (void) fputc('\n', ofp);
189
190 (void) fclose(ofp);
191 (void) unlink(lockfile);
192 *space = ' ';
193
194 } /* mk_state_update_exit() */
195
196 static void
197 /* LINTED static unused */
198 ld_support_init()
199 {
200 mk_state_init();
201
202 } /* ld_support_init() */
203
204 /* ARGSUSED */
205 void
206 ld_file(file, ekind, flags, elf)
207 const char *file;
208 const Elf_Kind ekind;
209 int flags;
210 Elf *elf;
211 {
212 if (! ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
213 return;
214
215 mk_state_collect_dep(file);
216
217 } /* ld_file */
218
219 void
220 ld_atexit(exit_code)
221 int exit_code;
222 {
223 if (exit_code)
224 return;
225
226 mk_state_update_exit();
227
228 } /* ld_atexit() */
229
230 /*
231 * Supporting 64-bit objects
232 */
233 void
234 ld_file64(file, ekind, flags, elf)
235 const char *file;
236 const Elf_Kind ekind;
237 int flags;
238 Elf *elf;
239 {
240 if (! ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED)))
241 return;
242
243 mk_state_collect_dep(file);
244
245 } /* ld_file64 */
246
247 void
248 ld_atexit64(exit_code)
249 int exit_code;
250 {
251 if (exit_code)
252 return;
253
254 mk_state_update_exit();
255
256 } /* ld_atexit64() */
257 #endif /* HAVE_LIBELF_H */