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>
28 #endif
29 
30 static const char kPermissionsHelpUrl[] = "http://developer.android.com/tools/device.html";
31 
32 // Returns a message describing any potential problems we find with udev, or nullptr if we can't
33 // find plugdev information (i.e. udev is not installed).
GetUdevProblem()34 static const char* GetUdevProblem() {
35 #if defined(__linux__)
36     errno = 0;
37     group* plugdev_group = getgrnam("plugdev");
38 
39     if (plugdev_group == nullptr) {
40         if (errno != 0) {
41             perror("failed to read plugdev group info");
42         }
43         // We can't give any generally useful advice here, just let the caller print the help URL.
44         return nullptr;
45     }
46 
47     // getgroups(2) indicates that the group_member() may not check the egid so we check it
48     // additionally just to be sure.
49     if (group_member(plugdev_group->gr_gid) || getegid() == plugdev_group->gr_gid) {
50         // The user is in plugdev so the problem is likely with the udev rules.
51         return "verify udev rules";
52     }
53     return "udev requires plugdev group membership";
54 #else
55     return nullptr;
56 #endif
57 }
58 
59 // Short help text must be a single line, and will look something like:
60 //   no permissions (reason); see <URL>
UsbNoPermissionsShortHelpText()61 std::string UsbNoPermissionsShortHelpText() {
62     std::string help_text = "no permissions";
63 
64     const char* problem = GetUdevProblem();
65     if (problem != nullptr) {
66         help_text += android::base::StringPrintf(" (%s)", problem);
67     }
68 
69     return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
70 }
71 
72 // Long help text can span multiple lines and should provide more detailed information.
UsbNoPermissionsLongHelpText()73 std::string UsbNoPermissionsLongHelpText() {
74     std::string header = "insufficient permissions for device";
75 
76     const char* problem = GetUdevProblem();
77     if (problem != nullptr) {
78         header += android::base::StringPrintf(": %s", problem);
79     }
80 
81     return android::base::StringPrintf("%s.\nSee [%s] for more information.",
82                                        header.c_str(), kPermissionsHelpUrl);
83 }
84