| 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 |  | 
 | 22 | #include <string> | 
 | 23 |  | 
 | 24 | #include <android-base/stringprintf.h> | 
 | 25 |  | 
 | 26 | #if defined(__linux__) | 
 | 27 | #include <grp.h> | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 28 | #include <pwd.h> | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 29 | #endif | 
 | 30 |  | 
 | 31 | static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html"; | 
 | 32 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 33 | // Returns a message describing any potential problems we find with udev, or an empty string if we | 
 | 34 | // can't find plugdev information (i.e. udev is not installed). | 
 | 35 | static std::string GetUdevProblem() { | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 36 | #if defined(__linux__) | 
 | 37 |     errno = 0; | 
 | 38 |     group* plugdev_group = getgrnam("plugdev"); | 
 | 39 |  | 
 | 40 |     if (plugdev_group == nullptr) { | 
 | 41 |         if (errno != 0) { | 
 | 42 |             perror("failed to read plugdev group info"); | 
 | 43 |         } | 
 | 44 |         // 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] | 45 |         return ""; | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 46 |     } | 
 | 47 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 48 |     // getgroups(2) indicates that the GNU group_member(3) may not check the egid so we check it | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 49 |     // additionally just to be sure. | 
 | 50 |     if (group_member(plugdev_group->gr_gid) || getegid() == plugdev_group->gr_gid) { | 
 | 51 |         // The user is in plugdev so the problem is likely with the udev rules. | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 52 |         return "user in plugdev group; are your udev rules wrong?"; | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 53 |     } | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 54 |     passwd* pwd = getpwuid(getuid()); | 
 | 55 |     return android::base::StringPrintf("user %s is not in the plugdev group", | 
 | 56 |                                        pwd ? pwd->pw_name : "?"); | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 57 | #else | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 58 |     return ""; | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 59 | #endif | 
 | 60 | } | 
 | 61 |  | 
 | 62 | // 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] | 63 | // | 
 | 64 | //   no permissions (reason); see [URL] | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 65 | std::string UsbNoPermissionsShortHelpText() { | 
 | 66 |     std::string help_text = "no permissions"; | 
 | 67 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 68 |     std::string problem(GetUdevProblem()); | 
 | 69 |     if (!problem.empty()) help_text += " (" + problem + ")"; | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 70 |  | 
 | 71 |     return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl); | 
 | 72 | } | 
 | 73 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 74 | // Long help text can span multiple lines but doesn't currently provide more detailed information: | 
 | 75 | // | 
 | 76 | //   insufficient permissions for device: reason | 
 | 77 | //   See [URL] for more information | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 78 | std::string UsbNoPermissionsLongHelpText() { | 
 | 79 |     std::string header = "insufficient permissions for device"; | 
 | 80 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 81 |     std::string problem(GetUdevProblem()); | 
 | 82 |     if (!problem.empty()) header += ": " + problem; | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 83 |  | 
| Elliott Hughes | cd35664 | 2017-05-03 17:25:34 -0700 | [diff] [blame] | 84 |     return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(), | 
 | 85 |                                        kPermissionsHelpUrl); | 
| Elliott Hughes | 1b708d3 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 86 | } |