The Pedigree Project  0.1
preloadd/main.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sched.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/klog.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 const char *g_FilesToPreload[] = {
33  "/applications/bash",
34  "/applications/ls",
35  "/applications/cat",
36  "/applications/make",
37  "/applications/gcc",
38  "/applications/ld",
39  "/applications/as",
40  "/applications/cpp",
41  "/support/gcc/libexec/gcc/x86_64-pedigree/4.8.2/cc1",
42  "/support/gcc/libexec/gcc/x86_64-pedigree/4.8.2/cc1plus",
43  "/support/gcc/libexec/gcc/x86_64-pedigree/4.8.2/collect2",
44  "/libraries/libc.so",
45  "/libraries/libm.so",
46  "/libraries/libpthread.so",
47  "/libraries/libpedigree-c.so",
48  "/libraries/libpedigree.so",
49  "/libraries/libui.so",
50  "/libraries/libgmp.so",
51  "/libraries/libmpfr.so",
52  "/libraries/libmpc.so",
53  0};
54 
55 #define BLOCK_READ_SIZE 0x100000
56 
57 int main(int argc, char **argv)
58 {
59  pid_t f = fork();
60  if (f != 0)
61  {
62  klog(LOG_INFO, "preloadd: forked, daemon is pid %d...", f);
63  return 0;
64  }
65 
66  klog(LOG_INFO, "preloadd: daemon starting...");
67 
68  size_t n = 0;
69  const char *s = g_FilesToPreload[n++];
70  char *buf = (char *) malloc(BLOCK_READ_SIZE);
71  do
72  {
73  struct stat st;
74  int e = stat(s, &st);
75  if (e == 0)
76  {
77  klog(LOG_INFO, "preloadd: preloading %s...", s);
78  FILE *fp = fopen(s, "rb");
79  for (off_t off = 0; off < st.st_size; off += BLOCK_READ_SIZE)
80  fread(buf, BLOCK_READ_SIZE, 1, fp);
81  fclose(fp);
82  klog(LOG_INFO, "preloadd: preloading %s complete!", s);
83  }
84  else
85  {
86  klog(LOG_INFO, "preloadd: %s probably does not exist", s);
87  }
88  s = g_FilesToPreload[n++];
89  } while (s);
90 
91  free(buf);
92 
93  return 0;
94 }