vcgencmd: Apply ASLR
[platform/adaptation/broadcom/libomxil-vc4.git] / interface / vmcs_host / linux / vcfiled / vcfiled.c
1 /*
2 Copyright (c) 2012, Broadcom Europe Ltd
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in the
11       documentation and/or other materials provided with the distribution.
12     * Neither the name of the copyright holder nor the
13       names of its contributors may be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /**
29   * @file
30   *
31   * Daemon serving files to VideoCore from the host filing system.
32   *
33   */
34
35 #include <stdio.h>
36 #include <limits.h>
37 #include <stdarg.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <getopt.h>
43 #include <syslog.h>
44 #include <fcntl.h>
45 #ifdef ANDROID
46 #include <android/log.h>
47 #endif
48
49 #include "vchiq.h"
50 #include "interface/vchi/vchi.h"
51 #include "interface/vcos/vcos.h"
52 #include "interface/vmcs_host/vc_vchi_filesys.h"
53 #include "vchost.h"
54 #include "vcfiled_check.h"
55
56 static int log_stderr;                /** Debug output to stderr? */
57 static int foreground;                /** Don't fork - run in foreground? */
58 static const char *work_dir = "/";    /** Working directory */
59 static const char *lock_prefix = "";  /** Lock file prefix */
60 static int lock_prefix_set = 0;
61 static const char *progname;
62
63 VCHI_INSTANCE_T global_initialise_instance;
64 VCHI_CONNECTION_T *global_connection;
65
66 void vc_host_get_vchi_state(VCHI_INSTANCE_T *initialise_instance, VCHI_CONNECTION_T **connection)
67 {
68    *initialise_instance = global_initialise_instance;
69    *connection = global_connection;
70 }
71
72 static void logmsg(int level, const char *fmt, ...)
73 {
74    va_list ap;
75    va_start(ap, fmt);
76
77    if (log_stderr)
78    {
79       vfprintf(stderr, fmt, ap);
80    }
81    else
82    {
83 #ifdef ANDROID
84       __android_log_vprint(level, "vcfiled", fmt, ap);
85 #else
86       vsyslog(level | LOG_DAEMON, fmt, ap);
87 #endif
88    }
89    va_end(ap);
90 }
91
92 static void usage(const char *progname)
93 {
94
95    fprintf(stderr,"usage: %s [-debug] [-foreground] [-root dir] [-lock prefix]\n",
96            progname);
97    fprintf(stderr,"  --debug       - debug to stderr rather than syslog\n");
98    fprintf(stderr,"  --foreground  - do not fork, stay in foreground\n");
99    fprintf(stderr,"  --root dir    - chdir to this directory first\n");
100    fprintf(stderr,"  --lock prefix - prefix to append to VCFILED_LOCKFILE\n");
101 }
102
103 enum { OPT_DEBUG, OPT_FG, OPT_ROOT, OPT_LOCK };
104
105 static void parse_args(int argc, char *const *argv)
106 {
107    int finished = 0;
108    static struct option long_options[] =
109    {
110       {"debug", no_argument, &log_stderr, 1},
111       {"foreground", no_argument, &foreground, 1},
112       {"root", required_argument, NULL, OPT_ROOT},
113       {"lock", required_argument, NULL, OPT_LOCK},
114       {0, 0, 0, 0},
115    };
116    while (!finished)
117    {
118       int option_index = 0;
119       int c = getopt_long_only(argc, argv, "", long_options, &option_index);
120
121       switch (c)
122       {
123       case 0:
124          // debug or foreground options, just sets flags directly
125          break;
126       case OPT_ROOT:
127          work_dir = optarg;
128          // sanity check directory
129          if (chdir(work_dir) < 0)
130          {
131             fprintf(stderr,"cannot chdir to %s: %s\n", work_dir, strerror(errno));
132             exit(-1);
133          }
134          break;
135       case OPT_LOCK:
136          lock_prefix = optarg;
137          lock_prefix_set = 1;
138          break;
139
140       default:
141       case '?':
142          usage(argv[0]);
143          exit(-1);
144          break;
145
146       case -1:
147          finished = 1;
148          break;
149       }
150
151    }
152 }
153
154 int main(int argc, char *const *argv)
155 {
156    VCHIQ_INSTANCE_T vchiq_instance;
157    int success;
158    char lockfile[128];
159
160    progname = argv[0];
161    const char *sep = strrchr(progname, '/');
162    if (sep)
163       progname = sep+1;
164
165    parse_args(argc, argv);
166
167    if (!foreground)
168    {
169       if ( daemon( 0, 1 ) != 0 )
170       {
171          fprintf( stderr, "Failed to daemonize: errno = %d", errno );
172          return -1;
173       }
174       log_stderr = 0;
175    }
176    if ( lock_prefix_set )
177    {
178       vcos_safe_sprintf( lockfile, sizeof(lockfile), 0, "%s/%s", lock_prefix, VCFILED_LOCKFILE );
179    }
180    else
181    {
182       vcos_safe_sprintf( lockfile, sizeof(lockfile), 0, "%s", VCFILED_LOCKFILE );
183    }
184    success = vcfiled_lock(lockfile, logmsg);
185    if (success != 0)
186    {
187       return -1;
188    }
189
190    logmsg( LOG_INFO, "vcfiled started" );
191
192    vcos_init();
193
194    if (vchiq_initialise(&vchiq_instance) != VCHIQ_SUCCESS)
195    {
196       logmsg(LOG_ERR, "%s: failed to open vchiq instance\n", argv[0]);
197       return -1;
198    }
199
200    success = vchi_initialise( &global_initialise_instance);
201    vcos_assert(success == 0);
202    vchiq_instance = (VCHIQ_INSTANCE_T)global_initialise_instance;
203
204    global_connection = vchi_create_connection(single_get_func_table(),
205                                               vchi_mphi_message_driver_func_table());
206
207    vchi_connect(&global_connection, 1, global_initialise_instance);
208   
209    vc_vchi_filesys_init (global_initialise_instance, &global_connection, 1);
210
211    for (;;)
212    {
213       sleep(INT_MAX);
214    }
215
216    vcos_deinit ();
217
218    return 0;
219 }
220