Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / salinit.cxx
blob07c0955a36fd769699f7920d9f98ab275a303fdf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
22 #include "sal/config.h"
24 #if defined MACOSX
25 #include <cassert>
26 #include <limits>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #endif
31 #include "osl/process.h"
32 #include "sal/main.h"
33 #include "sal/types.h"
35 #if HAVE_SYSLOG_H
36 #include <string.h>
37 #include <syslog.h>
38 // from sal/osl/all/log.cxx:
39 extern bool sal_use_syslog;
40 #endif
42 extern "C" {
44 //From time.c
45 void sal_initGlobalTimer();
47 void sal_detail_initialize(int argc, char ** argv) {
48 #if defined MACOSX && !HAVE_FEATURE_MACOSX_SANDBOX
49 // On OS X when not sandboxed, soffice can restart itself via exec (see
50 // restartOnMac in desktop/source/app/app.cxx), which leaves all file
51 // descriptors open, which in turn can have unwanted effects (see
52 // <https://bugs.libreoffice.org/show_bug.cgi?id=50603> "Unable to update
53 // LibreOffice without resetting user profile"). But closing fds in
54 // restartOnMac before calling exec does not work, as additional threads
55 // might still be running then, which can still use those fds and cause
56 // crashes. Therefore, the simplest solution is to close fds at process
57 // start (as early as possible, so that no other threads have been created
58 // yet that might already have opened some fds); this is done for all kinds
59 // of processes here, not just soffice, but hopefully none of our processes
60 // rely on being spawned with certain fds already open. Unfortunately, Mac
61 // OS X appears to have no better interface to close all fds (like
62 // closefrom):
63 long openMax = sysconf(_SC_OPEN_MAX);
64 if (openMax == -1) {
65 // Some random value, but hopefully sysconf never returns -1 anyway:
66 openMax = 1024;
68 assert(openMax >= 0 && openMax <= std::numeric_limits< int >::max());
69 for (int fd = 3; fd < openMax; ++fd) {
70 struct stat s;
71 if (fstat(fd, &s) != -1 && S_ISREG(s.st_mode))
72 close(fd);
74 #endif
75 sal_initGlobalTimer();
76 #if HAVE_SYSLOG_H
77 const char *use_syslog = getenv("SAL_LOG_SYSLOG");
78 sal_use_syslog = use_syslog != NULL && !strcmp(use_syslog, "1");
79 if (sal_use_syslog)
80 openlog("libreoffice", 0, LOG_USER);
81 #endif
83 osl_setCommandArgs(argc, argv);
86 void sal_detail_deinitialize() {}
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */