android: Reuse launcher icon in activities
[LibreOffice.git] / sal / osl / unx / salinit.cxx
blobc55cb2667be3a8c8acf811f11f6ac4f310ea4b16
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 <config_global.h>
32 #include <osl/process.h>
33 #include <sal/main.h>
34 #include <sal/types.h>
36 #include "saltime.hxx"
37 #include "soffice.hxx"
38 #include <salusesyslog.hxx>
40 #if HAVE_SYSLOG_H
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
44 #endif
46 extern "C" {
48 void sal_detail_initialize(int argc, char ** argv) {
49 if (argc == sal::detail::InitializeSoffice)
51 sal::detail::setSoffice();
52 return;
54 #if defined MACOSX && !HAVE_FEATURE_MACOSX_SANDBOX
55 // On macOS when not sandboxed, soffice can restart itself via exec (see
56 // restartOnMac in desktop/source/app/app.cxx), which leaves all file
57 // descriptors open, which in turn can have unwanted effects (see
58 // <https://bugs.libreoffice.org/show_bug.cgi?id=50603> "Unable to update
59 // LibreOffice without resetting user profile"). But closing fds in
60 // restartOnMac before calling exec does not work, as additional threads
61 // might still be running then, which can still use those fds and cause
62 // crashes. Therefore, the simplest solution is to close fds at process
63 // start (as early as possible, so that no other threads have been created
64 // yet that might already have opened some fds); this is done for all kinds
65 // of processes here, not just soffice, but hopefully none of our processes
66 // rely on being spawned with certain fds already open. Unfortunately, Mac
67 // macOS appears to have no better interface to close all fds (like
68 // closefrom):
69 long openMax = sysconf(_SC_OPEN_MAX);
70 // When LibreOffice restarts itself on macOS 11 beta on arm64, for
71 // some reason sysconf(_SC_OPEN_MAX) returns 0x7FFFFFFFFFFFFFFF,
72 // so use a sanity limit here.
73 if (openMax == -1 || openMax == std::numeric_limits<long>::max()) {
74 openMax = 100000;
76 assert(openMax >= 0 && openMax <= std::numeric_limits< int >::max());
77 for (int fd = 3; fd < int(openMax); ++fd) {
78 struct stat s;
79 if (fstat(fd, &s) != -1 && S_ISREG(s.st_mode))
80 close(fd);
82 #endif
83 sal_initGlobalTimer();
84 #if HAVE_SYSLOG_H
85 const char *use_syslog = getenv("SAL_LOG_SYSLOG");
86 sal_use_syslog = use_syslog != nullptr && !strcmp(use_syslog, "1");
87 if (sal_use_syslog)
88 openlog("libreoffice", 0, LOG_USER);
89 #endif
91 osl_setCommandArgs(argc, argv);
94 void sal_detail_deinitialize() {}
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */