Initialize __progname correctly.

setprogname() does a basename, but we were initializing __progname
directly. Stop doing that, and add some tests.

Test: treehugger
Change-Id: I06f306ade4161b2f0c7e314a3b1b30c9420117b7
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index d64a6bd..b3f4f3d 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -94,7 +94,7 @@
   // Initialize various globals.
   environ = __libc_shared_globals()->init_environ;
   errno = 0;
-  __progname = __libc_shared_globals()->init_progname ?: "<unknown>";
+  setprogname(__libc_shared_globals()->init_progname ?: "<unknown>");
 
 #if !defined(__LP64__)
   __check_max_thread_id();
diff --git a/tests/dl_test.cpp b/tests/dl_test.cpp
index 05bba05..2b963c7 100644
--- a/tests/dl_test.cpp
+++ b/tests/dl_test.cpp
@@ -135,7 +135,7 @@
   std::string expected_output =
       "ctor: argc=1 argv[0]=" + helper + "\n" +
       "main: argc=1 argv[0]=" + helper + "\n" +
-      "__progname=" + helper + "\n" +
+      "__progname=exec_linker_helper\n" +
       "helper_func called\n";
   ExecTestHelper eth;
   eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
@@ -151,7 +151,7 @@
   std::string expected_output =
       "ctor: argc=1 argv[0]=" + helper + "\n" +
       "main: argc=1 argv[0]=" + helper + "\n" +
-      "__progname=" + helper + "\n" +
+      "__progname=exec_linker_helper\n" +
       "helper_func called\n";
   ExecTestHelper eth;
   eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index ff4cb71..c12ae6b 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -871,3 +871,24 @@
   ASSERT_TRUE(fabs(expected[1] - load[1]) < 0.5) << expected[1] << ' ' << load[1];
   ASSERT_TRUE(fabs(expected[2] - load[2]) < 0.5) << expected[2] << ' ' << load[2];
 }
+
+TEST(stdlib, getprogname) {
+#if defined(__GLIBC__)
+  GTEST_SKIP() << "glibc doesn't have getprogname()";
+#else
+  // You should always have a name.
+  ASSERT_TRUE(getprogname() != nullptr);
+  // The name should never have a slash in it.
+  ASSERT_TRUE(strchr(getprogname(), '/') == nullptr);
+#endif
+}
+
+TEST(stdlib, setprogname) {
+#if defined(__GLIBC__)
+  GTEST_SKIP() << "glibc doesn't have setprogname()";
+#else
+  // setprogname() only takes the basename of what you give it.
+  setprogname("/usr/bin/muppet");
+  ASSERT_STREQ("muppet", getprogname());
+#endif
+}