Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2009-2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "FrameworkClient"
     18 
     19 #include <alloca.h>
     20 #include <errno.h>
     21 #include <pthread.h>
     22 #include <sys/types.h>
     23 
     24 #include <android/log.h>
     25 #include <sysutils/FrameworkClient.h>
     26 
     27 FrameworkClient::FrameworkClient(int socket) {
     28     mSocket = socket;
     29     pthread_mutex_init(&mWriteMutex, NULL);
     30 }
     31 
     32 int FrameworkClient::sendMsg(const char *msg) {
     33     int ret;
     34     if (mSocket < 0) {
     35         errno = EHOSTUNREACH;
     36         return -1;
     37     }
     38 
     39     pthread_mutex_lock(&mWriteMutex);
     40     ret = TEMP_FAILURE_RETRY(write(mSocket, msg, strlen(msg) +1));
     41     if (ret < 0) {
     42         SLOGW("Unable to send msg '%s' (%s)", msg, strerror(errno));
     43     }
     44     pthread_mutex_unlock(&mWriteMutex);
     45     return 0;
     46 }
     47 
     48 int FrameworkClient::sendMsg(const char *msg, const char *data) {
     49     size_t bufflen = strlen(msg) + strlen(data) + 1;
     50     char *buffer = (char *) alloca(bufflen);
     51     if (!buffer) {
     52         errno = -ENOMEM;
     53         return -1;
     54     }
     55     snprintf(buffer, bufflen, "%s%s", msg, data);
     56     return sendMsg(buffer);
     57 }
     58 
     59