LibreOffice Module odk (master) 1
unx/unoapploader.c
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <stdlib.h>
21#include <unistd.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/stat.h>
25
26#ifdef LINUX
27#define __USE_GNU
28#endif
29#include <dlfcn.h>
30
32#include <rtl/string.h>
33#include <sal/types.h>
34
35static char* getPath(void);
36static char* createCommandName( char* argv0 );
37
38static const int SEPARATOR = '/';
39static const char* PATHSEPARATOR = ":";
40
41
42/*
43 * The main function implements a loader for applications which use UNO.
44 *
45 * <p>This code runs on the Unix/Linux platforms only.</p>
46 *
47 * <p>The main function detects a UNO installation on the system and adds the
48 * relevant directories of the installation to the LD_LIBRARY_PATH environment
49 * variable. After that, the application process is loaded and started, whereby
50 * the new process inherits the environment of the calling process, including
51 * the modified LD_LIBRARY_PATH environment variable. The application's
52 * executable name must be the same as the name of this executable, prefixed
53 * by '_'.</p>
54 * <p>On MACOSX DYLD_LIBRARY_PATH is used instead of LD_LIBRARY_PATH!<p>
55 *
56 * <p>A UNO installation can be specified by the user by setting the UNO_PATH
57 * environment variable to the program directory of the UNO installation.
58 * If no installation is specified by the user, the default installation on
59 * the system will be taken. The default installation is found from the
60 * PATH environment variable. This requires that the 'soffice' executable or
61 * a symbolic link is in one of the directories listed in the PATH environment
62 * variable.</p>
63 */
64int main( int argc, char *argv[] )
65{
66 char* path;
67 char* cmdname;
68
69 (void) argc; /* avoid warning about unused parameter */
70
71 /* get the path of the UNO installation */
72 path = getPath();
73
74 if ( path != NULL )
75 {
76#if defined(MACOSX)
77 static const char* ENVVARNAME = "DYLD_LIBRARY_PATH";
78#else
79 static const char* ENVVARNAME = "LD_LIBRARY_PATH";
80#endif
81 char* libpath;
82 char* value;
83 char* envstr;
84 int size;
85
86 size_t pathlen = strlen(path);
87 struct stat stats;
88 int ret;
89
90 static char const unoinfoSuffix[] = "/unoinfo";
91 char * unoinfo = malloc(
92 pathlen + RTL_CONSTASCII_LENGTH(unoinfoSuffix) + 1);
93 /*TODO: overflow */
94 if (unoinfo == NULL) {
95 free(path);
96 fprintf(stderr, "Error: out of memory!\n");
97 exit(EXIT_FAILURE);
98 }
99 strcpy(unoinfo, path);
100 strcpy(
101 unoinfo + pathlen,
102 unoinfoSuffix + (pathlen == 0 || path[pathlen - 1] != '/' ? 0 : 1));
103 ret = lstat(unoinfo, &stats);
104 free(unoinfo);
105
106 if (ret == 0) {
107 char * cmd = malloc(
108 2 * pathlen + RTL_CONSTASCII_LENGTH("/unoinfo c++") + 1);
109 /*TODO: overflow */
110 char const * p;
111 char * q;
112 FILE * f;
113 size_t n = 1000;
114 size_t old = 0;
115 if (cmd == NULL) {
116 fprintf(stderr, "Error: out of memory!\n");
117 exit(EXIT_FAILURE);
118 }
119 p = path;
120 q = cmd;
121 while (*p != '\0') {
122 *q++ = '\\';
123 *q++ = *p++;
124 }
125 if (p == path || p[-1] != '/') {
126 *q++ = '/';
127 }
128 strcpy(q, "unoinfo c++");
129 f = popen(cmd, "r");
130 free(cmd);
131 if (f == NULL)
132 {
133 fprintf(stderr, "Error: calling unoinfo failed!\n");
134 exit(EXIT_FAILURE);
135 }
136 libpath = NULL;
137 for (;;) {
138 size_t m;
139 libpath = realloc(libpath, n);
140 if (libpath == NULL) {
141 fprintf(
142 stderr,
143 "Error: out of memory reading unoinfo output!\n");
144 exit(EXIT_FAILURE);
145 }
146 m = fread(libpath + old, 1, n - old - 1, f);
147 if (m != n - old - 1) {
148 if (ferror(f)) {
149 fprintf(stderr, "Error: cannot read unoinfo output!\n");
150 exit(EXIT_FAILURE);
151 }
152 libpath[old + m] = '\0';
153 break;
154 }
155 if (n >= SAL_MAX_SIZE / 2) {
156 fprintf(
157 stderr,
158 "Error: out of memory reading unoinfo output!\n");
159 exit(EXIT_FAILURE);
160 }
161 old = n - 1;
162 n *= 2;
163 }
164 if (pclose(f) != 0) {
165 fprintf(stderr, "Error: executing unoinfo failed!\n");
166 exit(EXIT_FAILURE);
167 }
168 free(path);
169 }
170 else
171 {
172 /* Assume an old OOo 2.x installation without unoinfo: */
173 libpath = path;
174 }
175
176 value = getenv( ENVVARNAME );
177
178 // workaround for finding wrong libsqlite3.dylib in the office installation
179 // For MacOS > 10.6 nss uses the system lib -> unresolved symbol _sqlite3_wal_checkpoint
180#ifdef MACOSX
181 size = strlen( ENVVARNAME ) + strlen( "=/usr/lib:" ) + strlen( libpath ) + 1;
182#else
183 size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( libpath ) + 1;
184#endif
185 if ( value != NULL )
186 size += strlen( PATHSEPARATOR ) + strlen( value );
187 envstr = (char*) malloc( size );
188 strcpy( envstr, ENVVARNAME );
189#ifdef MACOSX
190 strcat( envstr, "=/usr/lib:" );
191#else
192 strcat( envstr, "=" );
193#endif
194 strcat( envstr, libpath );
195 free( libpath );
196 if ( value != NULL )
197 {
198 strcat( envstr, PATHSEPARATOR );
199 strcat( envstr, value );
200 }
201 /* coverity[tainted_data : FALSE] */
202 putenv( envstr );
203 }
204 else
205 {
206 fprintf( stderr, "Warning: no office installation found!\n" );
207 fflush( stderr );
208 }
209
210 /* set the executable name for the application process */
211 cmdname = createCommandName( argv[0] );
212 argv[0] = cmdname;
213
214 /*
215 * create the application process;
216 * if successful, execvp doesn't return to the calling process
217 */
218 /* coverity[tainted_string] - createCommandName creates a safe string */
219 execvp( cmdname, argv );
220 fprintf( stderr, "Error: execvp failed!\n" );
221 fflush( stderr );
222
223 return 0;
224}
225
226/*
227 * Gets the path of a UNO installation.
228 *
229 * @return the installation path or NULL, if no installation was specified or
230 * found, or if an error occurred.
231 * Returned pointer must be released with free()
232 */
233char* getPath(void)
234{
236
237 if ( path == NULL )
238 {
239 fprintf( stderr, "Warning: getting path from PATH environment "
240 "variable failed!\n" );
241 fflush( stderr );
242 }
243
244 return path;
245}
246
247/*
248 * Creates the application's executable file name.
249 *
250 * <p>The application's executable file name is the name of this executable
251 * prefixed by '_'.</p>
252 *
253 * @param argv0 specifies the argv[0] parameter of the main function
254 *
255 * @return the application's executable file name or NULL, if an error occurred
256 */
257char* createCommandName( char* argv0 )
258{
259 const char* CMDPREFIX = "_";
260 const char* prgname = NULL;
261
262 char* cmdname = NULL;
263 char* sep = NULL;
264 Dl_info dl_info;
265
266 /* get the executable file name from argv0 */
267 prgname = argv0;
268
269 /*
270 * if argv0 doesn't contain an absolute path name, try to get the absolute
271 * path name from dladdr; note that this only works for Solaris, not for
272 * Linux
273 */
274 if ( argv0 != NULL && *argv0 != SEPARATOR &&
275 dladdr( (void*) &createCommandName, &dl_info ) &&
276 dl_info.dli_fname != NULL && *dl_info.dli_fname == SEPARATOR )
277 {
278 prgname = dl_info.dli_fname;
279 }
280
281 /* prefix the executable file name by '_' */
282 if ( prgname != NULL )
283 {
284 cmdname = (char*) malloc( strlen( prgname ) + strlen( CMDPREFIX ) + 1 );
285 sep = strrchr( prgname, SEPARATOR );
286 if ( sep != NULL )
287 {
288 int pos = ++sep - prgname;
289 strncpy( cmdname, prgname, pos );
290 cmdname[ pos ] = '\0';
291 strcat( cmdname, CMDPREFIX );
292 strcat( cmdname, sep );
293 }
294 else
295 {
296 strcpy( cmdname, CMDPREFIX );
297 strcat( cmdname, prgname );
298 }
299 }
300
301 return cmdname;
302}
303
304/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Any value
char * cppuhelper_detail_findSofficePath(void)
void * p
sal_Int64 n
return NULL
size
m
int main(int argc, char *argv[])
static char * getPath(void)
static char * createCommandName(char *argv0)
static const int SEPARATOR
static const char * PATHSEPARATOR
size_t pos