diff options
author | Po Lu <luangruo@yahoo.com> | 2023-08-13 09:10:07 +0800 |
---|---|---|
committer | Po Lu <luangruo@yahoo.com> | 2023-08-13 09:10:07 +0800 |
commit | d21e84d1e72988bd55d43ea991dd365b7bd713cf (patch) | |
tree | 839150acf4f8915602201c30d841dbd71eb00150 /src/android.c | |
parent | 5315e6e8d7e7233d54cce2b4c1bc8cf3b7acf4dc (diff) |
Employ careadlinkat in getProcName
* src/android.c (android_proc_name): Delete args BUFFER and
SIZE. Return buffer allocated by careadlinkat.
(NATIVE_NAME): Adjust correspondingly.
Diffstat (limited to 'src/android.c')
-rw-r--r-- | src/android.c | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/src/android.c b/src/android.c index b5b4359dcd3..70779f8ccae 100644 --- a/src/android.c +++ b/src/android.c @@ -18,31 +18,28 @@ You should have received a copy of the GNU General Public License along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ #include <config.h> +#include <allocator.h> +#include <assert.h> +#include <careadlinkat.h> +#include <errno.h> #include <fcntl.h> -#include <unistd.h> -#include <pthread.h> +#include <fingerprint.h> +#include <intprops.h> +#include <libgen.h> #include <limits.h> -#include <signal.h> -#include <semaphore.h> -#include <dlfcn.h> -#include <errno.h> #include <math.h> -#include <string.h> +#include <pthread.h> +#include <semaphore.h> +#include <signal.h> #include <stdckdint.h> -#include <intprops.h> -#include <timespec.h> -#include <libgen.h> - -#include <sys/stat.h> -#include <sys/mman.h> +#include <string.h> #include <sys/param.h> +#include <timespec.h> +#include <unistd.h> /* Old NDK versions lack MIN and MAX. */ #include <minmax.h> -#include <assert.h> -#include <fingerprint.h> - #include "android.h" #include "androidgui.h" @@ -1157,24 +1154,23 @@ android_get_home_directory (void) } /* Return the name of the file behind a file descriptor FD by reading - /proc/self/fd/. Place the name in BUFFER, which should be able to - hold size bytes. Value is 0 upon success, and 1 upon failure. */ + /proc/self/fd/. Value is allocated memory holding the file name + upon success, and 0 upon failure. */ -static int -android_proc_name (int fd, char *buffer, size_t size) +static char * +android_proc_name (int fd) { char format[sizeof "/proc/self/fd/" + INT_STRLEN_BOUND (int)]; - ssize_t read; + static struct allocator allocator = { + /* Fill the allocator with C library malloc functions. xmalloc + and so aren't thread safe. */ + malloc, realloc, free, NULL, + }; sprintf (format, "/proc/self/fd/%d", fd); - read = readlink (format, buffer, size - 1); - - if (read == -1) - return 1; - - buffer[read] = '\0'; - return 0; + return careadlinkat (AT_FDCWD, format, NULL, 0, + &allocator, readlinkat); } /* Try to guarantee the existence of the `lib' directory within the @@ -1465,11 +1461,12 @@ NATIVE_NAME (getProcName) (JNIEnv *env, jobject object, jint fd) { JNI_STACK_ALIGNMENT_PROLOGUE; - char buffer[PATH_MAX + 1]; + char *buffer; size_t length; jbyteArray array; - if (android_proc_name (fd, buffer, PATH_MAX + 1)) + buffer = android_proc_name (fd); + if (!buffer) return NULL; /* Return a byte array, as Java strings cannot always encode file @@ -1477,11 +1474,13 @@ NATIVE_NAME (getProcName) (JNIEnv *env, jobject object, jint fd) length = strlen (buffer); array = (*env)->NewByteArray (env, length); if (!array) - return NULL; + goto finish; (*env)->SetByteArrayRegion (env, array, 0, length, (jbyte *) buffer); + finish: + free (buffer); return array; } |