Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "diagnose_usb.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <unistd.h> |
| 21 | |
Colin Cross | 9f0e649 | 2021-12-16 13:30:28 -0800 | [diff] [blame] | 22 | #include <algorithm> |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 23 | #include <string> |
Colin Cross | 9f0e649 | 2021-12-16 13:30:28 -0800 | [diff] [blame] | 24 | #include <vector> |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 25 | |
| 26 | #include <android-base/stringprintf.h> |
| 27 | |
| 28 | #if defined(__linux__) |
| 29 | #include <grp.h> |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 30 | #include <pwd.h> |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html"; |
| 34 | |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 35 | // Returns a message describing any potential problems we find with udev, or an empty string if we |
| 36 | // can't find plugdev information (i.e. udev is not installed). |
| 37 | static std::string GetUdevProblem() { |
Josh Gao | 2776845 | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 38 | #if defined(__linux__) && !defined(__BIONIC__) |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 39 | errno = 0; |
| 40 | group* plugdev_group = getgrnam("plugdev"); |
| 41 | |
| 42 | if (plugdev_group == nullptr) { |
| 43 | if (errno != 0) { |
| 44 | perror("failed to read plugdev group info"); |
| 45 | } |
| 46 | // We can't give any generally useful advice here, just let the caller print the help URL. |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 47 | return ""; |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Colin Cross | 9f0e649 | 2021-12-16 13:30:28 -0800 | [diff] [blame] | 50 | int ngroups = getgroups(0, nullptr); |
| 51 | if (ngroups < 0) { |
| 52 | perror("failed to get groups list size"); |
| 53 | return ""; |
| 54 | } |
| 55 | |
| 56 | std::vector<gid_t> groups(ngroups); |
| 57 | ngroups = getgroups(groups.size(), groups.data()); |
| 58 | if (ngroups < 0) { |
| 59 | perror("failed to get groups list"); |
| 60 | return ""; |
| 61 | } |
| 62 | |
| 63 | groups.resize(ngroups); |
| 64 | |
| 65 | // getgroups(2) indicates that the egid may not be included so we check it additionally just |
| 66 | // to be sure. |
| 67 | if (std::find(groups.begin(), groups.end(), plugdev_group->gr_gid) != groups.end() || |
| 68 | getegid() == plugdev_group->gr_gid) { |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 69 | // The user is in plugdev so the problem is likely with the udev rules. |
Elliott Hughes | e7edfbc | 2020-07-31 15:09:35 -0700 | [diff] [blame] | 70 | return "missing udev rules? user is in the plugdev group"; |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 71 | } |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 72 | passwd* pwd = getpwuid(getuid()); |
| 73 | return android::base::StringPrintf("user %s is not in the plugdev group", |
| 74 | pwd ? pwd->pw_name : "?"); |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 75 | #else |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 76 | return ""; |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 77 | #endif |
| 78 | } |
| 79 | |
| 80 | // Short help text must be a single line, and will look something like: |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 81 | // |
| 82 | // no permissions (reason); see [URL] |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 83 | std::string UsbNoPermissionsShortHelpText() { |
| 84 | std::string help_text = "no permissions"; |
| 85 | |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 86 | std::string problem(GetUdevProblem()); |
| 87 | if (!problem.empty()) help_text += " (" + problem + ")"; |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 88 | |
| 89 | return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl); |
| 90 | } |
| 91 | |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 92 | // Long help text can span multiple lines but doesn't currently provide more detailed information: |
| 93 | // |
| 94 | // insufficient permissions for device: reason |
| 95 | // See [URL] for more information |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 96 | std::string UsbNoPermissionsLongHelpText() { |
| 97 | std::string header = "insufficient permissions for device"; |
| 98 | |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 99 | std::string problem(GetUdevProblem()); |
| 100 | if (!problem.empty()) header += ": " + problem; |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 101 | |
Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 102 | return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(), |
| 103 | kPermissionsHelpUrl); |
Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 104 | } |