1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <dlfcn.h>
  27 #include <locale.h>
  28 #include <string>
  29 #include <libgen.h>
  30 #include <stdio.h>
  31 #include <unistd.h>
  32 
  33 
  34 typedef bool (*start_launcher)(int argc, char* argv[]);
  35 typedef void (*stop_launcher)();
  36 
  37 #define MAX_PATH 1024
  38 
  39 std::string GetProgramPath() {
  40     ssize_t len = 0;
  41     std::string result;
  42     char buffer[MAX_PATH] = {0};
  43 
  44     if ((len = readlink("/proc/self/exe", buffer, MAX_PATH - 1)) != -1) {
  45         buffer[len] = '\0';
  46         result = buffer;
  47     }
  48 
  49     return result;
  50 }
  51 
  52 int main(int argc, char *argv[]) {
  53     int result = 1;
  54     setlocale(LC_ALL, "en_US.utf8");
  55     void* library = NULL;
  56 
  57     {
  58         std::string programPath = GetProgramPath();
  59         std::string libraryName = dirname((char*)programPath.c_str());
  60         libraryName += "/libapplauncher.so";
  61         library = dlopen(libraryName.c_str(), RTLD_LAZY);
  62 
  63         if (library == NULL) {
  64             fprintf(stderr, "dlopen failed: %s\n", dlerror());
  65             fprintf(stderr, "%s not found.\n", libraryName.c_str());
  66         }
  67     }
  68 
  69     if (library != NULL) {
  70         start_launcher start = (start_launcher)dlsym(library, "start_launcher");
  71         stop_launcher stop = (stop_launcher)dlsym(library, "stop_launcher");
  72 
  73         if (start != NULL && stop != NULL) {
  74             if (start(argc, argv) == true) {
  75                 result = 0;
  76                 stop();
  77             }
  78         } else {
  79             fprintf(stderr, "cannot find start_launcher and stop_launcher in libapplauncher.so");
  80         }
  81 
  82         dlclose(library);
  83     }
  84 
  85 
  86     return result;
  87 }