diff options
author | Hwankyu Jhun <h.jhun@samsung.com> | 2018-08-17 17:23:47 +0900 |
---|---|---|
committer | Hwankyu Jhun <h.jhun@samsung.com> | 2018-08-17 17:23:47 +0900 |
commit | 08378ea88078451d1823de5b13caedfbcf45d27a (patch) | |
tree | c1a30caf6790ec8d65eb25d74d80adeef0e42ca1 | |
parent | cebf9087ba6f62f5726cf09a4507af24f12cb434 (diff) | |
download | aul-1-08378ea88078451d1823de5b13caedfbcf45d27a.tar.gz aul-1-08378ea88078451d1823de5b13caedfbcf45d27a.tar.bz2 aul-1-08378ea88078451d1823de5b13caedfbcf45d27a.zip |
Initial commitsandbox/hjhun/devel2
This is Application Utility Library v2.
Change-Id: I8ee116c3497d89ad2806711c805bfe1013761e0a
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
-rw-r--r-- | CMakeLists.txt | 53 | ||||
-rw-r--r-- | LICENSE | 204 | ||||
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | aul2.pc.in | 14 | ||||
-rwxr-xr-x | build.sh | 52 | ||||
-rw-r--r-- | inc/aul.h | 28 | ||||
-rw-r--r-- | packaging/aul2.manifest | 5 | ||||
-rw-r--r-- | packaging/aul2.spec | 60 | ||||
-rw-r--r-- | src/aul.cc | 17 | ||||
-rw-r--r-- | src/bundle-internal.cc | 52 | ||||
-rw-r--r-- | src/bundle-internal.h | 46 | ||||
-rw-r--r-- | src/dlog-internal.h | 27 | ||||
-rw-r--r-- | src/socket-internal.cc | 192 | ||||
-rw-r--r-- | src/socket-internal.h | 78 |
14 files changed, 837 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..9802e898 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,53 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) + +PROJECT(aul2 CXX) + +IF(LIB_INSTALL_DIR) + MESSAGE(STATUS "LIB INSTALL DIR: ${LIB_INSTALL_DIR}") +ELSE(LIB_INSTALL_DIR) + SET(LIB_INSTALL_DIR "/usr/lib") +ENDIF(LIB_INSTALL_DIR) + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc) + +SET(aul2_requires "glib-2.0 bundle") + +INCLUDE(FindPkgConfig) +pkg_check_modules(aul2 REQUIRED ${aul2_requires}) + +FOREACH(flag ${aul2_CLFAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall" ) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror -fvisibility=hidden") +SET(CMAKE_C_FLAGS_DEBUG "-00 -g") + +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -fvisibility=hidden -std=c++11") + +AUX_SOURCE_DIRECTORY(src SOURCES) +ADD_LIBRARY(aul2 SHARED ${SOURCES}) + +SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed -Wl,--no-undefined -Wl,--rpath=${LIB_INSTALL_DIR}") + +TARGET_LINK_LIBRARIES(aul2 ${aul2_LDFLAGS}) + +SET_TARGET_PROPERTIES(aul2 PROPERTIES + VERSION ${MAJORVER} + SOVERSION ${FULLVER} + CLEAN_DIRECT_OUTPUT 1) + +SET(PC_NAME aul2) +SET(VERSION 0.1.0) +SET(PC_REQUIRED ${aul2_requires}) + +INSTALL(TARGETS aul2 DESTINATION ${LIB_INSTALL_DIR}) + +CONFIGURE_FILE(aul2.pc.in aul2.pc @ONLY) + +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/aul2.pc + DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/inc/aul.h + DESTINATION include/aul2) diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..7726e771 --- /dev/null +++ b/LICENSE @@ -0,0 +1,204 @@ +Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + diff --git a/README.md b/README.md new file mode 100644 index 00000000..6ac0d25c --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# AUL-2 + +# The following commands are for using build.sh file. +Usage) $ ./build.sh <command> +Commands: + - build Build the package + - clean Clean all artifacts + - clean_build Clean & Build the package + diff --git a/aul2.pc.in b/aul2.pc.in new file mode 100644 index 00000000..fcfb2994 --- /dev/null +++ b/aul2.pc.in @@ -0,0 +1,14 @@ +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=@EXEC_PREFIX@ +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@ + +Name: libaul2 +Description: Application Utility Library v2 +Version: @VERSION@ +Requires: +Libs: -L${libdir} -laul2 +Cflags: -I${includedir} -I${includedir}/aul2 + diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..af8bd110 --- /dev/null +++ b/build.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +#--------------------------------------------------------# +# Application Utility Library v2 # +#--------------------------------------------------------# + +PATH=/bin:/usr/bin:/sbin:/usr/sbin + +SCRIPT_FILE=$(readlink -f $0) +SCRIPT_DIR=$(dirname $SCRIPT_FILE) +BUILD_DIR=build + +usage() { + echo "Usage: $0 [command]" + echo "Commands:" + echo " build Build the package" + echo " clean Clean all artifacts" + echo " clean_build Clean & Build the package" +} + +cmd_build() { + echo "[AUL2] Build the package" + mkdir -p $SCRIPT_DIR/$BUILD_DIR + cd $SCRIPT_DIR/$BUILD_DIR + VER=`cat ../packaging/aul2.spec | grep 'Version' | sed 's/Version://' | tr -d ' \r'` + MAJORVER=`echo $VER | awk 'BEGIN {FS="."}{print $1}'` + cmake .. -DFULLVER=$VER -DMAJORVER=$MAJORVER + make clean + make +} + +cmd_clean() { + echo "[AUL2] Clean all artifacts" + cd $SCRIPT_DIR/$BUILD_DIR + make clean + cd .. + rm -rf $SCRIPT_DIR/$BUILD_DIR +} + +cmd_clean_build() { + cmd_clean + cmd_build +} + +cmd=$1; shift; +case "$cmd" in + build|--build|-b) cmd_build $@ ;; + clean|--clean|-c) cmd_clean $@ ;; + clean_build |--clean_build |-cb) cmd_clean_build $@ ;; + *) usage ;; + +esac diff --git a/inc/aul.h b/inc/aul.h new file mode 100644 index 00000000..0b03b3f7 --- /dev/null +++ b/inc/aul.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 __AUL_H__ +#define __AUL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __AUL_H__ */ diff --git a/packaging/aul2.manifest b/packaging/aul2.manifest new file mode 100644 index 00000000..97e8c313 --- /dev/null +++ b/packaging/aul2.manifest @@ -0,0 +1,5 @@ +<manifest> + <request> + <domain name="_"/> + </request> +</manifest> diff --git a/packaging/aul2.spec b/packaging/aul2.spec new file mode 100644 index 00000000..95f5a58d --- /dev/null +++ b/packaging/aul2.spec @@ -0,0 +1,60 @@ +Name: aul2 +Summary: Application Utility Library v2 +Version: 0.1.0 +Release: 1 +Group: Application Framework/Libraries +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +BuildRequires: cmake +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(bundle) + +Requires(post): /sbin/ldconfig + +%description +Application Utility Library v2 + +%package devel +Summary: Application Utility Library v2 (devel) +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Application Utility Library v2 (devel) + +%prep +%setup -q +cp %{SOURCE1} . + +%build +MAJOR_VER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +%cmake -DFULL_VER=%{version} \ + -DMAJOR_VER=${MAJOR_VER} \ + . + +%__make %{?_smp_mflags} + +%install +rm -rf %{buildroot} +%make_install + +%preun + +%post +/sbin/ldconfig + +%postun +/sbin/ldconfig + + +%files +%license LICENSE +%manifest %{name}.manifest +%attr(0644,root,root) %{_libdir}/libaul2.so.* + +%files devel +%{_includedir}/aul2/*.h +%{_libdir}/pkgconfig/%{name}.pc +%{_libdir}/*.so diff --git a/src/aul.cc b/src/aul.cc new file mode 100644 index 00000000..66e76481 --- /dev/null +++ b/src/aul.cc @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 "aul.h" diff --git a/src/bundle-internal.cc b/src/bundle-internal.cc new file mode 100644 index 00000000..ddb1977b --- /dev/null +++ b/src/bundle-internal.cc @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <string.h> +#include <unistd.h> + +#include <bundle.h> + +#include "bundle-internal.h" + +namespace aul { +namespace internal { + +Bundle::Bundle(bundle* b_raw) : + b_raw_(b_raw) { +} + +Bundle::Bundle(const unsigned char* raw) { + b_raw_ = bundle_decode(raw, strlen((const char*)raw)); +} + +Bundle::~Bundle() { + if (b_raw_ != nullptr) + bundle_free(b_raw_); +} + +unsigned char* Bundle::Encode() { + bundle_raw* raw = NULL; + int len = 0; + bundle_encode(b_raw_, &raw, &len); + return (unsigned char*)raw; +} + +} // namespace internal +} // namespace aul diff --git a/src/bundle-internal.h b/src/bundle-internal.h new file mode 100644 index 00000000..f9cac755 --- /dev/null +++ b/src/bundle-internal.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 BUNDLE_INTERNAL_H_ +#define BUNDLE_INTERNAL_H_ + +#include <bundle_internal.h> + +#include <string> + +namespace aul { +namespace internal { + +class Bundle { + public: + explicit Bundle(bundle* b_raw); + explicit Bundle(const unsigned char* raw); + ~Bundle(); + + public: + unsigned char* Encode(); + bundle* GetRaw() const { + return b_raw_; + } + + private: + bundle* b_raw_; +}; + +} // namespace internal +} // namespace aul + +#endif // BUNDLE_INTERNAL_H_ diff --git a/src/dlog-internal.h b/src/dlog-internal.h new file mode 100644 index 00000000..ef417fd4 --- /dev/null +++ b/src/dlog-internal.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 DLOG_INTERNAL_H_ +#define DLOG_INTERNAL_H_ + +#ifdef __DLOG_H__ +#else +#define LOGD printf +#define LOGW printf +#define LOGE printf +#endif + +#endif // DLOG_INTERNAL_H_ diff --git a/src/socket-internal.cc b/src/socket-internal.cc new file mode 100644 index 00000000..25f7cb5b --- /dev/null +++ b/src/socket-internal.cc @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <errno.h> + +#include "socket-internal.h" +#include "dlog-internal.h" + +#define MAX_RETRY 2 +#define MIN_REGULAR_UID 5000 + +namespace aul { +namespace internal { + +Socket::Socket(pid_t pid, uid_t uid, bool server) : + pid_(pid), uid_(uid) { + int r = Create(); + if (r != 0) { + LOGE("Failed to create socket"); + throw r; + } +} + +Socket::Socket(int fd) : + fd_(fd) { +} + +Socket::~Socket() { + if (fd_ > 0) + close(fd_); +} + +int Socket::Recv(void* buf, unsigned int size) { + unsigned int len = size; + int retry = MAX_RETRY; + char* buffer = static_cast<char*>(buf); + int r; + + while (len > 0 && retry) { + r = recv(fd_, buffer, len, 0); + if (r <= 0) { + if (errno == EINTR || errno == EAGAIN) { + LOGW("retry(%d), fd(%d), errno(%d)", retry, fd_, errno); + retry--; + continue; + } + + LOGE("Failed to receive data. fd(%d), errno(%d)", fd_, errno); + return -ECOMM; + } + + len -= r; + buffer += r; + } + + if (len != 0) { + LOGE("Failed to receive data. fd(%d), len(%u:%u)", fd_, len, size); + return -ECOMM; + } + + return size; +} + +int Socket::Send(const void* buf, unsigned int size) { + const char* buffer = static_cast<const char*>(buf); + int sent = 0; + int len; + + while (static_cast<unsigned int>(sent) != size) { + len = send(fd_, buffer + sent, size - sent, MSG_NOSIGNAL); + if (len <= 0) { + LOGE("Failed to send data. fd(%d), errno(%d)", fd_, errno); + return -ECOMM; + } + sent += len; + } + return size; +} + +int Socket::Create() { + int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); + if (fd < 0) { + if (errno == EINVAL) { + LOGD("Second chance"); + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + LOGE("Failed to create socket. errno(%d)", errno); + return -1; + } + } else { + LOGE("Failed to create socket. errno(%d)", errno); + return -1; + } + } + + fd_ = fd; + return 0; +} + +int Socket::Prepare() { + return 0; +} + +int Socket::Connect() { + return 0; +} + +int Socket::Listen() { + return 0; +} + +SocketHelper::SocketHelper(pid_t pid, uid_t uid) : + pid_(pid), uid_(uid) { +} + +std::string SocketHelper::CreateSocketDirPathString(pid_t pid, uid_t uid) { + std::string path; + if (uid < MIN_REGULAR_UID) { + path = std::string("/run/aul/daemons/") + std::to_string(uid); + } else { + path = std::string("/run/aul/apps/") + std::to_string(uid) + + std::to_string(pid_); + } + return path; +} + +std::string SocketHelper::CreateSocketPathString(pid_t pid, uid_t uid) { + std::string path; + if (uid < MIN_REGULAR_UID) { + path = std::string("/run/aul/daemons/") + std::to_string(uid) + + std::string("/.app-sock-") + std::to_string(pid); + } else { + path = std::string("/run/aul/apps/") + std::to_string(uid) + + std::to_string(pid) + std::string("/.app-sock"); + } + return path; +} + +int SocketHelper::CreateSocketDir(std::string path) { + int r = mkdir(path.c_str(), 0700); + if (r != 0) { + if (errno == EEXIST) { + r = access(path.c_str(), R_OK); + if (r != 0) { + LOGE("Failed to access %s directory", path.c_str()); + return -1; + } + } + + LOGE("Failed to create %s directory", path.c_str()); + return -1; + } + + return 0; +} + +void SocketHelper::CreateSocketLink(std::string old_path, + std::string new_path) { + int r = link(old_path.c_str(), new_path.c_str()); + if (r != 0) { + if (errno == EEXIST) { + LOGW("path(%s) - already exists", new_path.c_str()); + } else { + LOGE("path(%s) - unknown create error", new_path.c_str()); + } + } +} + +} // namespace internal +} // namespace aul diff --git a/src/socket-internal.h b/src/socket-internal.h new file mode 100644 index 00000000..d7145c8c --- /dev/null +++ b/src/socket-internal.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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 SOCKET_INTERNAL_H_ +#define SOCKET_INTERNAL_H_ + +#include <thread> +#include <mutex> +#include <string> +#include <memory> + +namespace aul { +namespace internal { + +class Socket { + public: + Socket(pid_t pid, uid_t uid, bool server = false); + explicit Socket(int fd); + virtual ~Socket(); + + public: + int Recv(void* buf, unsigned int size); + int Send(const void* buf, unsigned int size); + + int GetFd() const { + return fd_; + } + + std::recursive_mutex& GetMutex() const { + return mutex_; + } + + private: + int Create(); + int Prepare(); + int Listen(); + int Connect(); + + private: + int fd_ = 0; + pid_t pid_ = 0; + uid_t uid_ = 0; + mutable std::recursive_mutex mutex_; +}; + +class SocketHelper { + public: + SocketHelper(pid_t pid, uid_t uid); + virtual ~SocketHelper() = default; + + public: + std::string CreateSocketDirPathString(pid_t pid, uid_t uid); + std::string CreateSocketPathString(pid_t pid, uid_t uid); + int CreateSocketDir(std::string path); + void CreateSocketLink(std::string old_path, std::string new_path); + + private: + pid_t pid_; + uid_t uid_; +}; + +} // namespace internal +} // namespace aul + +#endif // SOCKET_INTERNAL_H_ |