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 <algorithm>
23 #include <string>
24 #include <vector>
25
26 #include <android-base/stringprintf.h>
27
28 #if defined(__linux__)
29 #include <grp.h>
30 #include <pwd.h>
31 #endif
32
33 static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
34
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).
GetUdevProblem()37 static std::string GetUdevProblem() {
38 #if defined(__linux__) && !defined(__BIONIC__)
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.
47 return "";
48 }
49
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) {
69 // The user is in plugdev so the problem is likely with the udev rules.
70 return "missing udev rules? user is in the plugdev group";
71 }
72 passwd* pwd = getpwuid(getuid());
73 return android::base::StringPrintf("user %s is not in the plugdev group",
74 pwd ? pwd->pw_name : "?");
75 #else
76 return "";
77 #endif
78 }
79
80 // Short help text must be a single line, and will look something like:
81 //
82 // no permissions (reason); see [URL]
UsbNoPermissionsShortHelpText()83 std::string UsbNoPermissionsShortHelpText() {
84 std::string help_text = "no permissions";
85
86 std::string problem(GetUdevProblem());
87 if (!problem.empty()) help_text += " (" + problem + ")";
88
89 return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
90 }
91
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
UsbNoPermissionsLongHelpText()96 std::string UsbNoPermissionsLongHelpText() {
97 std::string header = "insufficient permissions for device";
98
99 std::string problem(GetUdevProblem());
100 if (!problem.empty()) header += ": " + problem;
101
102 return android::base::StringPrintf("%s\nSee [%s] for more information", header.c_str(),
103 kPermissionsHelpUrl);
104 }
105