Commit 92e646fc authored by Roumen Petrov's avatar Roumen Petrov

add command to extract application info

Imported from "SecureBox Pro".
parent bd987552
/build
# Android Studio < 3.5
/.externalNativeBuild
# Android Studio >= 3.5
/.cxx
......@@ -12,6 +12,17 @@ android {
versionCode 311
versionName '3.1.1'
externalNativeBuild {
cmake {
arguments "-DAPPLICATION_ID:STRING=${applicationId}"
}
}
}
externalNativeBuild {
cmake {
path "src/CMakeLists.txt"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
......
##
# Copyright (C) 2019 Roumen Petrov. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
cmake_minimum_required(VERSION 2.4)
add_executable(
libexec-t1plus.so
main/jni/appcmd.c
main/jni/socket.c
main/jni/atomicio.c
)
target_compile_definitions(
libexec-t1plus.so
PRIVATE
PACKAGE_NAME="${APPLICATION_ID}"
)
target_link_libraries(
libexec-t1plus.so
)
/*
* Copyright (C) 2019 Roumen Petrov. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "appinfo.h"
static int/*bool*/
get_info(const char *info) {
int ret = 0;
char sockname[PATH_MAX + 1];
char msg[1024];
char buf[4096];
int sock;
size_t len, res;
if (snprintf(sockname, sizeof(sockname), SOCKET_PREFIX "%ld", (long) getuid())
>= sizeof(sockname))
return 0;
if (snprintf(msg, sizeof(msg), "get %s\n", info) >= sizeof(msg))
return 0;
sock = open_socket(sockname);
if (sock == -1) return 0;
len = strlen(msg);
res = atomicio(vwrite, sock, (void *) msg, len);
if (res != len) goto done;
while (1) {
int read_errno;
errno = 0;
len = atomicio(read, sock, buf, sizeof(buf));
read_errno = errno;
if (len > 0) {
errno = 0;
res = atomicio(vwrite, STDOUT_FILENO, buf, len);
if (res != len) goto done;
}
if (read_errno == EPIPE) break;
}
(void) fsync(STDOUT_FILENO);
ret = 1;
done:
close(sock);
return ret;
}
#include <sysexits.h>
int
main(int argc, char *argv[]) {
int ret;
if (argc != 2) exit(EX_USAGE);
ret = get_info(argv[1]);
return ret ? 0 : EX_SOFTWARE;
}
#ifndef TERMONEPLUS_APPINFO_H
#define TERMONEPLUS_APPINFO_H
/*
* Copyright (C) 2019 Roumen Petrov. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PACKAGE_NAME
# error "package name is not defined"
#endif
#define SOCKET_PREFIX PACKAGE_NAME "-app_paths-"
int open_socket(const char *name);
typedef ssize_t (*atomicio_f)(int fd, void *buf, size_t count);
#define vwrite (atomicio_f)write
size_t atomicio(atomicio_f f, int fd, void *buf, size_t count);
#endif /*TERMONEPLUS_APPINFO_H*/
/*
* Copyright (C) 2019 Roumen Petrov. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include "appinfo.h"
/* Ensure all of data on socket comes through. f==read || f==vwrite */
size_t
atomicio(atomicio_f f, int fd, void *_buf, size_t n) {
char *buf = _buf;
size_t pos = 0;
ssize_t res;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = (short) ((f == read) ? POLLIN : POLLOUT);
while (n > pos) {
res = (f)(fd, buf + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR) {
/* possible SIGALARM? */
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
(void) poll(&pfd, 1, -1);
continue;
}
return 0;
case 0:
errno = EPIPE;
return pos;
default:
pos += (size_t) res;
}
}
return pos;
}
/*
* Copyright (C) 2019 Roumen Petrov. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include "appinfo.h"
int
open_socket(const char *name) {
int sock;
struct sockaddr_un addr;
size_t namelen;
socklen_t addrlen;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
namelen = strlen(name);
if (namelen >= sizeof(addr.sun_path))
namelen = sizeof(addr.sun_path) - 1;
strncpy(addr.sun_path + 1, name, namelen);
addrlen = (socklen_t) offsetof(struct sockaddr_un, sun_path) + 1 + (socklen_t) namelen;
if (connect(sock, (const struct sockaddr *) &addr, addrlen) == -1) {
close(sock);
return -1;
}
return sock;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment