Skip to content
Snippets Groups Projects

Add inspect-library helper

Merged Ludovico de Nittis requested to merge wip/denittis/inspect-library into master
2 files
+ 112
8
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 91
0
/*
* Copyright © 2019 Collabora Ltd.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This helper takes a SONAME as an argument and outputs a parsable
* JSON with the path and the dependencies of the requested library.
*/
#include <glib.h>
#include <dlfcn.h>
#include <link.h>
int
main (int argc,
char **argv)
{
const char *soname;
void *handle = NULL;
/* 4096 should be the standard PATH_MAX */
char library_path[4096];
struct link_map *dep_map = NULL;
if (argc != 2)
{
g_warning ("Expected exactly one SONAME as argument.\n");
return 1;
}
soname = argv[1];
g_print ("{\n");
g_print (" \"%s\": {\n", soname);
handle = dlopen (soname, RTLD_LAZY);
if (handle == NULL)
{
g_warning ("Unable to find the library: %s\n", dlerror ());
g_print ("}\n");
return 1;
}
if (dlinfo (handle, RTLD_DI_ORIGIN, library_path) != 0)
{
g_warning ("Unable to obtain the path: %s\n", dlerror ());
g_print ("}\n");
return 1;
}
g_print (" \"path\": \"%s\",\n", library_path);
if (dlinfo (handle, RTLD_DI_LINKMAP, &dep_map) != 0)
{
g_warning ("Unable to obtain the dependencies list: %s\n", dlerror ());
g_print ("}\n");
return 1;
}
g_print (" \"dependencies\": [");
if (dep_map != NULL)
{
g_print ("\n \"%s\"", dep_map->l_name);
dep_map = dep_map->l_next;
}
for (; dep_map != NULL; dep_map = dep_map->l_next)
{
g_print (",\n \"%s\"", dep_map->l_name);
}
g_print ("\n ]\n }\n}\n");
return 0;
}
Loading