summaryrefslogtreecommitdiffstats
path: root/src/doit.c
blob: 2208ece503ccd027d87cec7b4590f2520a47a9ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (C) 2001, 2003, 2004, 2005 Red Hat, Inc.
   Written by Jakub Jelinek <jakub@redhat.com>, 2001.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <config.h>
#include <alloca.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "prelinktab.h"

struct collect_ents
  {
    struct prelink_entry **ents;
    int nents;
  };

static int
find_ents (void **p, void *info)
{
  struct collect_ents *l = (struct collect_ents *) info;
  struct prelink_entry *e = * (struct prelink_entry **) p;

  if ((e->type == ET_DYN && e->done == 1)
      || (e->type == ET_EXEC && e->done == 0 && ! libs_only))
    l->ents[l->nents++] = e;

  return 1;
}

static void
clear_ent_marks (struct prelink_entry *ent)
{
  int i;

  ent->u.tmp = 0;
  for (i = 0; i < ent->ndepends; ++i)
    clear_ent_marks (ent->depends[i]);
}

static struct prelink_entry *
find_unlisted_dependency (struct prelink_entry *ent)
{
  int i;
  struct prelink_entry *ret;

  if (ent->u.tmp == 0)
    return ent;
  for (i = 0; i < ent->ndepends; ++i)
    if ((ret = find_unlisted_dependency (ent->depends[i])) != NULL)
      return ret;
  return NULL;
}

static void
prelink_ent (struct prelink_entry *ent)
{
  int i;
  DSO *dso;
  struct stat64 st;
  struct prelink_link *hardlink;
  char *move = NULL;
  size_t movelen = 0;
  struct prelink_entry *dep;

  for (i = 0; i < ent->ndepends; ++i)
    if (ent->depends[i]->done == 1)
      prelink_ent (ent->depends[i]);

  for (i = 0; i < ent->ndepends; ++i)
    if (ent->depends[i]->done != 2)
      {
	ent->done = 0;
	if (! undo)
	  ent->type = ET_UNPRELINKABLE;
	if (verbose)
	  error (0, 0, "Could not prelink %s because its dependency %s could not be prelinked",
		 ent->filename, ent->depends[i]->filename);
	return;
      }
    else
      clear_ent_marks (ent->depends[i]);

  ent->u.tmp = 1;
  for (i = 0; i < ent->ndepends; ++i)
    ent->depends[i]->u.tmp = 1;

  if ((dep = find_unlisted_dependency (ent)) != NULL)
    {
      ent->done = 0;
      if (! undo)
	ent->type = ET_UNPRELINKABLE;
      if (verbose)
	error (0, 0, "Could not prelink %s because it doesn't use %s, but one of its dependencies has been prelinked against it",
	       ent->filename, dep->filename);
      return;
    }

  if (verbose)
    {
      if (dry_run)
	printf ("Would prelink %s\n", ent->canon_filename);
      else
	printf ("Prelinking %s\n", ent->canon_filename);
    }

  dso = open_dso (ent->canon_filename);
  if (dso == NULL)
    goto error_out;

  if (fstat64 (dso->fd, &st) < 0)
    {
      error (0, errno, "%s changed during prelinking", ent->filename);
      goto error_out;
    }

  if (st.st_dev != ent->dev || st.st_ino != ent->ino)
    {
      error (0, 0, "%s changed during prelinking", ent->filename);
      goto error_out;
    }

  if (dry_run)
    close_dso (dso);
  else
    {
      if (prelink_prepare (dso))
	goto make_unprelinkable;
      if (ent->type == ET_DYN && relocate_dso (dso, ent->base))
	goto make_unprelinkable;
      if (prelink (dso, ent))
	goto make_unprelinkable;
      if (update_dso (dso, NULL))
	{
	  dso = NULL;
	  goto error_out;
	}
    }
  ent->done = 2;
  ent->flags |= PCF_PRELINKED;

  /* Redo hardlinks.  */
  for (hardlink = ent->hardlink; hardlink; hardlink = hardlink->next)
    {
      size_t len;

      if (wrap_lstat64 (hardlink->canon_filename, &st) < 0)
	{
	  error (0, 0, "Could not stat %s (former hardlink to %s)",
		 hardlink->canon_filename, ent->canon_filename);
	  continue;
	}

      if (st.st_dev != ent->dev || st.st_ino != ent->ino)
	{
	  error (0, 0, "%s is no longer hardlink to %s",
		 hardlink->canon_filename, ent->canon_filename);
	  continue;
	}

      if (verbose)
	{
	  if (dry_run)
	    printf ("Would link %s to %s\n", hardlink->canon_filename,
		    ent->canon_filename);
	  else
	    printf ("Linking %s to %s\n", hardlink->canon_filename,
		    ent->canon_filename);
	}

      if (dry_run)
	continue;

      len = strlen (hardlink->canon_filename);
      if (len + sizeof (".#prelink#") > movelen)
	{
	  movelen = len + sizeof (".#prelink#");
	  move = realloc (move, movelen);
	  if (move == NULL)
	    {
	      error (0, ENOMEM, "Could not hardlink %s to %s",
		     hardlink->canon_filename, ent->canon_filename);
	      movelen = 0;
	      continue;
	    }
	}

      memcpy (mempcpy (move, hardlink->canon_filename, len), ".#prelink#",
	      sizeof (".#prelink#"));
      if (wrap_rename (hardlink->canon_filename, move) < 0)
	{
	  error (0, errno, "Could not hardlink %s to %s",
		 hardlink->canon_filename, ent->canon_filename);
	  continue;
	}

      if (wrap_link (ent->canon_filename, hardlink->canon_filename) < 0)
	{
	  error (0, errno, "Could not hardlink %s to %s",
		 hardlink->canon_filename, ent->canon_filename);

	  if (wrap_rename (move, hardlink->canon_filename) < 0)
	    {
	      error (0, errno, "Could not rename %s back to %s",
		     move, hardlink->canon_filename);
	    }
	  continue;
	}

      if (wrap_unlink (move) < 0)
	{
	  error (0, errno, "Could not unlink %s", move);
	  continue;
	}
    }
  free (move);

  if (! dry_run && wrap_stat64 (ent->canon_filename, &st) >= 0)
    {
      ent->dev = st.st_dev;
      ent->ino = st.st_ino;
      ent->ctime = st.st_ctime;
      ent->mtime = st.st_mtime;
    }
  return;

make_unprelinkable:
  if (! undo)
    ent->type = ET_UNPRELINKABLE;
error_out:
  ent->done = 0;
  if (dso)
    close_dso (dso);
  return;
}

void
prelink_all (void)
{
  struct collect_ents l;
  int i;

  l.ents =
    (struct prelink_entry **) alloca (prelink_entry_count
				      * sizeof (struct prelink_entry *));
  l.nents = 0;
  htab_traverse (prelink_filename_htab, find_ents, &l);

  for (i = 0; i < l.nents; ++i)
    if (l.ents[i]->done == 1
	|| (l.ents[i]->done == 0 && l.ents[i]->type == ET_EXEC))
      prelink_ent (l.ents[i]);
}