forked from sveinbjornt/Sloth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlothController.m
1194 lines (1005 loc) · 45.6 KB
/
SlothController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2004-2023, Sveinbjorn Thordarson <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import "Common.h"
#import "SlothController.h"
#import "Alerts.h"
#import "NSString+RegexConvenience.h"
#import "InfoPanelController.h"
#import "PrefsController.h"
#import "ProcessUtils.h"
#import "IconUtils.h"
#import "FSUtils.h"
#import "NSWorkspace+Additions.h"
#import "STPrivilegedTask.h"
#import "LsofTask.h"
#import "Item.h"
@interface SlothController ()
{
IBOutlet NSWindow *window;
IBOutlet NSMenu *itemContextualMenu;
IBOutlet NSMenu *sortMenu;
IBOutlet NSMenu *interfaceSizeSubmenu;
IBOutlet NSMenu *accessModeSubmenu;
IBOutlet NSMenu *filterMenu;
IBOutlet NSMenu *openWithMenu;
IBOutlet NSMenu *refreshIntervalMenu;
IBOutlet NSPopUpButton *volumesPopupButton;
IBOutlet NSMenuItem *volumesMenuItem;
IBOutlet NSProgressIndicator *progressIndicator;
IBOutlet NSTextField *filterTextField;
IBOutlet NSTextField *numItemsTextField;
IBOutlet NSButton *revealButton;
IBOutlet NSButton *killButton;
IBOutlet NSButton *getInfoButton;
IBOutlet NSButton *authenticateButton;
IBOutlet NSMenuItem *authenticateMenuItem;
IBOutlet NSButton *refreshButton;
IBOutlet NSMenuItem *refreshIntervalMenuItem;
IBOutlet NSButton *disclosureButton;
IBOutlet NSTextField *disclosureTextField;
IBOutlet NSOutlineView *outlineView;
AuthorizationRef authRef;
BOOL authenticated;
BOOL isRefreshing;
NSTimer *filterTimer;
NSTimer *updateTimer;
InfoPanelController *infoPanelController;
PrefsController *prefsController;
}
@property int totalFileCount;
@property (strong) IBOutlet NSMutableArray *content;
@property (strong) NSMutableArray *unfilteredContent;
@property (retain, nonatomic) NSArray *sortDescriptors;
@end
@implementation SlothController
- (instancetype)init {
if ((self = [super init])) {
_content = [[NSMutableArray alloc] init];
}
return self;
}
+ (void)initialize {
NSString *defaultsPath = [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"];
[DEFAULTS registerDefaults:[NSDictionary dictionaryWithContentsOfFile:defaultsPath]];
}
#pragma mark - NSApplicationDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Make sure lsof exists on the system
if ([FILEMGR fileExistsAtPath:LSOF_PATH] == NO) {
[Alerts fatalAlert:@"System corrupt" subTextFormat:@"No binary at path %@", LSOF_PATH];
}
// Put application icon in window title bar
[window setRepresentedURL:[NSURL URLWithString:@""]]; // Not representing a URL
[[window standardWindowButton:NSWindowDocumentIconButton] setImage:[NSApp applicationIconImage]];
// Hide Authenticate button & menu item if AuthorizationExecuteWithPrivileges
// function is not available in this version of macOS
if ([STPrivilegedTask authorizationFunctionAvailable]) {
NSImage *lockIcon = [IconUtils imageNamed:@"Locked"];
[authenticateButton setImage:lockIcon];
[authenticateMenuItem setImage:lockIcon];
} else {
// Hide/disable all authentication-related controls
[authenticateButton setHidden:YES];
[authenticateMenuItem setAction:nil];
}
// These menus are available in both menu bar and popup button.
// Why create two identical menus when the same one can be used?
[volumesMenuItem setSubmenu:[volumesPopupButton menu]];
[refreshIntervalMenuItem setSubmenu:refreshIntervalMenu];
// Set reveal button icon
NSImage *revealImg = [NSImage imageNamed:@"NSRevealFreestandingTemplate"];
[revealImg setSize:NSMakeSize(12,12)];
[revealButton setImage:revealImg];
// For some reason, Interface Builder isn't respecting image
// template settings so we have to do this manually
[[NSImage imageNamed:@"Kill"] setTemplate:YES];
[[NSImage imageNamed:@"Kill"] setSize:NSMakeSize(20, 20)];
[[NSImage imageNamed:@"Info"] setTemplate:YES];
[[NSImage imageNamed:@"Info"] setSize:NSMakeSize(20, 20)];
// Manually check the appropriate menu items for these submenus
// on launch since we (infuriatingly) can't use bindings for it!
[self checkItemWithTitle:[DEFAULTS stringForKey:@"interfaceSize"] inMenu:interfaceSizeSubmenu];
[self checkItemWithTitle:[DEFAULTS stringForKey:@"accessMode"] inMenu:accessModeSubmenu];
// Set icons for items in Filter menu
NSArray<NSMenuItem *> *items = [filterMenu itemArray];
int idx = 0;
for (NSMenuItem *i in items) {
idx += 1;
if (idx < 2) { // Skip first menu item (Show All)
continue;
}
NSString *type = [i toolTip];
if (type) {
NSImage *img = [IconUtils imageNamed:type];
[i setImage:img];
}
}
// Start observing defaults
for (NSString *key in @[@"showCharacterDevices",
@"showDirectories",
@"showIPSockets",
@"showRegularFiles",
@"showUnixSockets",
@"showPipes",
@"showApplicationsOnly",
@"showHomeFolderOnly",
@"accessMode",
@"interfaceSize",
@"searchFilterCaseSensitive",
@"searchFilterRegex",
@"updateInterval"
]) {
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
forKeyPath:VALUES_KEYPATH(key)
options:NSKeyValueObservingOptionNew
context:NULL];
}
// Configure outline view
[outlineView setDoubleAction:@selector(rowDoubleClicked:)];
[outlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:NO];
[outlineView.outlineTableColumn setWidth:outlineView.bounds.size.width];
[self updateDiscloseControl];
[self updateSorting];
if ([DEFAULTS boolForKey:@"authenticateOnLaunch"]) {
[self toggleAuthentication:self]; // Triggers refresh
} else {
// Refresh immediately when app is launched
[self refresh:self];
}
[self setUpdateTimerFromDefaults]; // If period update has been set in defaults
}
- (nullable NSMenu *)applicationDockMenu:(NSApplication *)sender {
NSMenu *menu = [NSMenu new];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Refresh" action:@selector(refresh:) keyEquivalent:@""];
[item setTarget:self];
[menu addItem:item];
return menu;
}
- (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu {
// Prevent popup menu when window icon/title is cmd-clicked
return NO;
}
- (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard {
// Prevent dragging of title bar icon
return NO;
}
- (BOOL)windowShouldClose:(NSWindow *)sender {
[[NSApplication sharedApplication] terminate:self];
return YES;
}
#pragma mark - Run lsof task
- (IBAction)refresh:(id)sender {
if (isRefreshing) {
return;
}
isRefreshing = YES;
[numItemsTextField setStringValue:@"Refreshing..."];
[outlineView deselectAll:self];
// Disable controls
[refreshButton setEnabled:NO];
[outlineView setEnabled:NO];
[outlineView setAlphaValue:0.5];
[authenticateButton setEnabled:NO];
// Center progress indicator and set it off
CGFloat x = (NSWidth([window.contentView bounds]) - NSWidth([progressIndicator frame])) / 2;
CGFloat y = (NSHeight([window.contentView bounds]) - NSHeight([progressIndicator frame])) / 2;
[progressIndicator setFrameOrigin:NSMakePoint(x,y)];
[progressIndicator setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin];
[progressIndicator setUsesThreadedAnimation:TRUE];
[progressIndicator startAnimation:self];
// Run lsof asynchronously in the background, so interface doesn't lock up
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
@autoreleasepool {
int fileCount;
LsofTask *task = [LsofTask new];
NSMutableArray *items = [task launch:authRef numFiles:&fileCount];
self.unfilteredContent = items;
self.totalFileCount = fileCount;
// Update UI on main thread once task is done
dispatch_async(dispatch_get_main_queue(), ^{
isRefreshing = NO;
// Re-enable controls
[progressIndicator stopAnimation:self];
[outlineView setEnabled:YES];
[outlineView setAlphaValue:1.0];
[refreshButton setEnabled:YES];
[authenticateButton setEnabled:YES];
// Filter results
[self updateFiltering];
});
}
});
}
- (void)setUpdateTimerFromDefaults {
if (updateTimer) {
[updateTimer invalidate];
updateTimer = nil;
}
NSInteger secInterval = [DEFAULTS integerForKey:@"updateInterval"];
if (secInterval == 0) { // Manual updates only
return;
}
updateTimer = [NSTimer scheduledTimerWithTimeInterval:secInterval
target:self
selector:@selector(refresh:)
userInfo:nil
repeats:YES];
}
#pragma mark - Filtering
- (void)updateProcessCountHeader {
NSString *sortedBy = [DEFAULTS stringForKey:@"sortBy"];
if ([sortedBy hasSuffix:@" id"]) {
sortedBy = [NSString stringWithFormat:@"%@ID", [sortedBy substringToIndex:[sortedBy length]-2]];
}
NSString *headerTitle = [NSString stringWithFormat:@"%d processes - sorted by %@", (int)[self.content count], sortedBy];
[[[outlineView tableColumnWithIdentifier:@"children"] headerCell] setStringValue:headerTitle];
}
- (void)updateFiltering {
if (isRefreshing) {
return;
}
// Filter content
int matchingFilesCount = 0;
self.content = [self filterContent:self.unfilteredContent numberOfMatchingFiles:&matchingFilesCount];
// Update outline view header
[self updateProcessCountHeader];
// Update num items label
NSString *str = [NSString stringWithFormat:@"Showing %d out of %d items", matchingFilesCount, self.totalFileCount];
if (matchingFilesCount == self.totalFileCount) {
str = [NSString stringWithFormat:@"Showing all %d items", self.totalFileCount];
}
[numItemsTextField setStringValue:str];
[outlineView reloadData];
if ([DEFAULTS boolForKey:@"disclosure"]) {
[outlineView expandItem:nil expandChildren:YES];
} else {
[outlineView collapseItem:nil collapseChildren:YES];
}
}
// User typed in search filter
- (void)controlTextDidChange:(NSNotification *)aNotification {
if (filterTimer) {
[filterTimer invalidate];
}
filterTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateFiltering)
userInfo:nil
repeats:NO];
}
// VolumesPopUpDelegate
- (void)volumeSelectionChanged:(NSString *)volumePath {
[self performSelector:@selector(updateFiltering) withObject:nil afterDelay:0.05];
}
// Some default changed
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([VALUES_KEYPATH(@"interfaceSize") isEqualToString:keyPath]) {
[outlineView reloadData];
return;
}
if ([VALUES_KEYPATH(@"updateInterval") isEqualToString:keyPath]) {
[self setUpdateTimerFromDefaults];
return;
}
// The default that changed was one of the filters
[self updateFiltering];
}
// Filter content according to active filters
- (NSMutableArray *)filterContent:(NSMutableArray *)unfilteredContent
numberOfMatchingFiles:(int *)matchingFilesCount {
BOOL showRegularFiles = [DEFAULTS boolForKey:@"showRegularFiles"];
BOOL showDirectories = [DEFAULTS boolForKey:@"showDirectories"];
BOOL showIPSockets = [DEFAULTS boolForKey:@"showIPSockets"];
BOOL showUnixSockets = [DEFAULTS boolForKey:@"showUnixSockets"];
BOOL showCharDevices = [DEFAULTS boolForKey:@"showCharacterDevices"];
BOOL showPipes = [DEFAULTS boolForKey:@"showPipes"];
BOOL showApplicationsOnly = [DEFAULTS boolForKey:@"showApplicationsOnly"];
BOOL showHomeFolderOnly = [DEFAULTS boolForKey:@"showHomeFolderOnly"];
BOOL searchCaseSensitive = [DEFAULTS boolForKey:@"searchFilterCaseSensitive"];
BOOL searchUsesRegex = [DEFAULTS boolForKey:@"searchFilterRegex"];
// Access mode filter
NSString *accessModeFilter = [DEFAULTS stringForKey:@"accessMode"];
BOOL hasAccessModeFilter = ([accessModeFilter isEqualToString:@"Any"] == NO);
// Volumes filter
NSNumber *volumesFilter = nil; // Device ID number
BOOL hasVolumesFilter = ([[[volumesPopupButton selectedItem] title] isEqualToString:@"All"] == NO);
if (hasVolumesFilter) {
volumesFilter = [[volumesPopupButton selectedItem] representedObject][@"devid"];
}
// Path filters such as by volume or home folder should
// exclude everything that isn't a file or directory
if (hasVolumesFilter || showHomeFolderOnly) {
showIPSockets = FALSE;
showUnixSockets = FALSE;
showCharDevices = FALSE;
showPipes = FALSE;
}
// User home dir path prefix
NSString *homeDirPath = NSHomeDirectory();
// Search field filter, precompile regexes
NSMutableArray *searchFilters = [NSMutableArray new];
NSString *fieldString = [[filterTextField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *filterStrings = [fieldString componentsSeparatedByString:@" "];
// Trim and create regex objects from search filter strings
for (NSString *fs in filterStrings) {
NSString *s = [fs stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([s length] == 0) {
continue;
}
if (searchUsesRegex) {
NSError *err;
NSRegularExpressionOptions options = searchCaseSensitive ? 0 : NSRegularExpressionCaseInsensitive;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:s
options:options
error:&err];
if (!regex) {
DLog(@"Error creating search filter regex: %@", [err localizedDescription]);
continue;
}
[searchFilters addObject:regex];
} else {
[searchFilters addObject:s];
}
}
// Filters set in Prefs, precompile regexes
NSMutableArray *prefsFilters = [NSMutableArray new];
NSArray *pfStrings = [DEFAULTS objectForKey:@"filters"];
for (NSArray *ps in pfStrings) {
if ([ps[0] boolValue] == NO) {
continue;
}
NSString *s = [ps[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([s length] == 0) {
continue;
}
NSError *err;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:s
options:0
error:&err];
if (!regex) {
DLog(@"Error creating prefs filter regex: %@", [err localizedDescription]);
continue;
}
[prefsFilters addObject:regex];
DLog(@"Adding regex: %@", ps[1]);
}
BOOL hasSearchFilter = ([searchFilters count] > 0);
BOOL hasPrefsFilter = ([prefsFilters count] > 0);
BOOL showAllProcessTypes = !showApplicationsOnly;
BOOL showAllItemTypes = (showRegularFiles &&
showDirectories &&
showIPSockets &&
showUnixSockets &&
showCharDevices &&
showPipes &&
!showHomeFolderOnly &&
!hasVolumesFilter);
// Minor optimization: If there is no filtering, just return
// unfiltered content instead of iterating over all items
if (showAllItemTypes && showAllProcessTypes && !hasSearchFilter && !hasPrefsFilter && !hasAccessModeFilter) {
*matchingFilesCount = self.totalFileCount;
return unfilteredContent;
}
NSMutableArray *filteredContent = [NSMutableArray array];
// Iterate over each process, filter the children
for (NSMutableDictionary *process in self.unfilteredContent) {
NSMutableArray *matchingFiles = [NSMutableArray array];
for (NSDictionary *file in process[@"children"]) {
// Let's see if child gets filtered by type or path
if (showAllItemTypes == NO) {
if (showHomeFolderOnly && ![file[@"name"] hasPrefix:homeDirPath]) {
continue;
}
if (volumesFilter) {
// DLog(@"%@ cmp %@", file[@"device"][@"devid"], volumesFilter);
if ([file[@"device"][@"devid"] isEqualToNumber:volumesFilter] == NO) {
continue;
}
}
NSString *type = file[@"type"];
if (([type hasPrefix:@"F"] && !showRegularFiles) ||
([type hasPrefix:@"D"] && !showDirectories) ||
([type hasPrefix:@"I"] && !showIPSockets) ||
([type hasPrefix:@"U"] && !showUnixSockets) ||
([type hasPrefix:@"C"] && !showCharDevices) ||
([type hasPrefix:@"P"] && !showPipes)) {
continue;
}
}
// Filter by access mode
if (hasAccessModeFilter) {
NSString *mode = file[@"accessmode"];
if ([accessModeFilter isEqualToString:@"Read"] && ![mode isEqualToString:@"r"]) {
continue;
}
if ([accessModeFilter isEqualToString:@"Write"] && ![mode isEqualToString:@"w"]) {
continue;
}
if ([accessModeFilter isEqualToString:@"Read/Write"] && ![mode isEqualToString:@"u"]) {
continue;
}
}
// See if it matches regexes in search field filter
if (hasSearchFilter) {
int matchCount = 0;
if (searchUsesRegex) {
// Regex search
for (NSRegularExpression *regex in searchFilters) {
if (!([file[@"name"] isMatchedByRegex:regex] ||
[file[@"pname"] isMatchedByRegex:regex] ||
[file[@"pid"] isMatchedByRegex:regex] ||
[file[@"protocol"] isMatchedByRegex:regex] ||
[file[@"ipversion"] isMatchedByRegex:regex] ||
[file[@"socketstate"] isMatchedByRegex:regex])) {
break;
}
matchCount += 1;
}
} else {
// Non-regex search
NSStringCompareOptions options = searchCaseSensitive ? 0 : NSCaseInsensitiveSearch;
for (NSString *searchStr in searchFilters) {
if ([file[@"name"] rangeOfString:searchStr options:options].location == NSNotFound &&
[file[@"pname"] rangeOfString:searchStr options:options].location == NSNotFound &&
[file[@"pid"] rangeOfString:searchStr options:options].location == NSNotFound) {
break;
}
matchCount += 1;
}
}
// Skip if it doesn't match all filter strings
if (matchCount != [searchFilters count]) {
continue;
}
}
// Prefs filters only filter by name
if (hasPrefsFilter) {
// Skip any file w. name matching
BOOL skip = NO;
for (NSRegularExpression *regex in prefsFilters) {
if ([file[@"name"] isMatchedByRegex:regex]) {
skip = YES;
}
}
if (skip) {
continue;
}
}
[matchingFiles addObject:file];
}
// If we have matching files for the process, and it's not being excluded as a non-app
if ([matchingFiles count] && !(showApplicationsOnly && ![process[@"app"] boolValue])) {
NSMutableDictionary *p = [process mutableCopy];
p[@"children"] = matchingFiles;
// Num files shown in brackets after name needs to be updated
[LsofTask updateProcessInfo:p];
[filteredContent addObject:p];
*matchingFilesCount += [matchingFiles count];
}
}
return filteredContent;
}
- (IBAction)showAll:(id)sender {
[DEFAULTS setObject:@YES forKey:@"showRegularFiles"];
[DEFAULTS setObject:@YES forKey:@"showDirectories"];
[DEFAULTS setObject:@YES forKey:@"showCharacterDevices"];
[DEFAULTS setObject:@YES forKey:@"showIPSockets"];
[DEFAULTS setObject:@YES forKey:@"showPipes"];
[DEFAULTS setObject:@YES forKey:@"showUnixSockets"];
[DEFAULTS setObject:@NO forKey:@"showApplicationsOnly"];
[DEFAULTS setObject:@NO forKey:@"showHomeFolderOnly"];
[DEFAULTS setObject:@"Any" forKey:@"accessMode"];
[filterTextField setStringValue:@""];
[volumesPopupButton selectItemAtIndex:0];
[DEFAULTS synchronize];
[self updateFiltering];
}
#pragma mark - Interface actions
- (IBAction)open:(id)sender {
NSInteger selectedRow = ([outlineView clickedRow] == -1) ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
NSString *path = item[@"name"];
if ([WORKSPACE canRevealFileAtPath:path] == NO || [WORKSPACE openFile:path] == NO) {
NSBeep();
}
}
- (IBAction)kill:(id)sender {
NSInteger selectedRow = ([outlineView clickedRow] == -1) ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
if (item[@"pid"] == nil) {
NSBeep();
return;
}
int pid = [item[@"pid"] intValue];
// Confirm
BOOL optionKeyDown = (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) == NSAlternateKeyMask);
if (optionKeyDown == NO) {
if ([Alerts proceedAlert:[NSString stringWithFormat:@"Are you sure you want to kill “%@” (%d)?", item[@"pname"], pid]
subText:@"This will send the process a SIGKILL signal. Hold the option key (⌥) to avoid this prompt."
withActionNamed:@"Kill"] == NO) {
return;
}
}
// Kill it
BOOL ownsProcess = [ProcessUtils isProcessOwnedByCurrentUser:pid];
if ([ProcessUtils killProcess:pid asRoot:!ownsProcess] == NO) {
[Alerts alert:@"Failed to kill process"
subTextFormat:@"Could not kill process %@ (PID: %d)", item[@"pname"], pid];
return;
}
[self refresh:self];
}
- (IBAction)show:(id)sender {
NSInteger selectedRow = [outlineView clickedRow] == -1 ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
[self revealItemInFinder:item];
}
- (IBAction)showInfoInFinder:(id)sender {
NSInteger selectedRow = [outlineView clickedRow] == -1 ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
NSString *path = item[@"path"] ? item[@"path"] : item[@"name"];
[WORKSPACE showFinderGetInfoForFile:path];
}
- (IBAction)showPackageContents:(id)sender {
NSInteger selectedRow = [outlineView clickedRow] == -1 ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
NSString *path = item[@"path"] ? item[@"path"] : item[@"name"];
if (![WORKSPACE showPackageContents:path]) {
NSBeep();
}
}
- (IBAction)moveToTrash:(id)sender {
NSInteger selectedRow = [outlineView clickedRow] == -1 ? [outlineView selectedRow] : [outlineView clickedRow];
NSDictionary *item = [[outlineView itemAtRow:selectedRow] representedObject];
NSString *path = item[@"path"] ? item[@"path"] : item[@"name"];
BOOL optionKeyDown = (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) == NSAlternateKeyMask);
if (!optionKeyDown) {
// Ask user to confirm
NSString *prompt = @"This will tell the Finder to move the specified file into your Trash folder. \
Hold the option key (⌥) to avoid this prompt.";
if ([Alerts proceedAlert:[NSString stringWithFormat:@"Move “%@” to the Trash?", [path lastPathComponent]]
subText:prompt
withActionNamed:@"Move to Trash"] == NO) {
return;
}
}
// Move to trash, refresh in a bit to give Finder time to complete command. Tends to be slow :/
if ([WORKSPACE moveFileToTrash:path]) {
[self performSelector:@selector(outlineViewSelectionDidChange:) withObject:nil afterDelay:0.4];
[outlineView performSelector:@selector(reloadData) withObject:nil afterDelay:0.6];
}
}
- (IBAction)getInfo:(id)sender {
NSInteger selectedRow = [outlineView selectedRow];
if (selectedRow >= 0) {
[self showInfoPanelForItem:[[outlineView itemAtRow:selectedRow] representedObject]];
} else {
NSBeep();
}
}
- (void)showInfoPanelForItem:(Item *)item {
// Create info panel lazily
if (infoPanelController == nil) {
infoPanelController = [[InfoPanelController alloc] initWithWindowNibName:@"InfoPanel"];
}
[infoPanelController loadItem:item];
[infoPanelController showWindow:self];
}
// Called when user selects Copy menu item via Edit or contextual menu
- (void)copy:(id)sender {
NSInteger selectedRow = [outlineView clickedRow] == -1 ? [outlineView selectedRow] : [outlineView clickedRow];
if (selectedRow == -1) {
NSBeep();
return;
}
// Write to pasteboard
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
NSMutableArray *names = [NSMutableArray new];
NSMutableArray *filePaths = [NSMutableArray new];
[[outlineView selectedRowIndexes] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSDictionary *item = [[outlineView itemAtRow:idx] representedObject];
if ([FILEMGR fileExistsAtPath:item[@"name"]]) {
[filePaths addObject:item[@"name"]];
}
if ([item[@"type"] isEqualToString:@"Process"]) {
[names addObject:[NSString stringWithFormat:@"%@ (%@)", item[@"name"], item[@"pid"]]];
} else {
[names addObject:[NSString stringWithFormat:@"\t%@", item[@"name"]]];
}
}];
if ([filePaths count]) {
[pasteBoard clearContents];
[pasteBoard declareTypes:@[NSFilenamesPboardType] owner:nil];
[pasteBoard setPropertyList:filePaths forType:NSFilenamesPboardType];
}
NSString *copyStr = [names componentsJoinedByString:@"\n"];
[pasteBoard setString:copyStr forType:NSStringPboardType];
}
- (void)rowDoubleClicked:(id)object {
NSInteger rowNumber = [outlineView clickedRow];
Item *item = [[outlineView itemAtRow:rowNumber] representedObject];
BOOL cmdKeyDown = (([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask);
if (cmdKeyDown) {
[self revealItemInFinder:item];
} else {
[self showInfoPanelForItem:item];
}
}
- (void)revealItemInFinder:(NSDictionary *)item {
NSString *path = item[@"path"] ? item[@"path"] : item[@"name"];
if ([WORKSPACE canRevealFileAtPath:path]) {
if ([WORKSPACE selectFile:path inFileViewerRootedAtPath:[path stringByDeletingLastPathComponent]]) {
return;
}
}
NSBeep();
}
- (IBAction)showPrefs:(id)sender {
// Create info panel lazily
if (prefsController == nil) {
prefsController = [[PrefsController alloc] initWithWindowNibName:@"Prefs"];
}
[prefsController showWindow:self];
}
- (IBAction)showSelectedItem:(id)sender {
NSInteger selectedRow = [outlineView selectedRow];
if (selectedRow > -1) {
[outlineView scrollRowToVisible:selectedRow];
} else {
NSBeep();
}
}
#pragma mark - Disclosure
- (IBAction)disclosureChanged:(id)sender {
if ([DEFAULTS boolForKey:@"disclosure"]) {
[outlineView expandItem:nil expandChildren:YES];
} else {
[outlineView collapseItem:nil collapseChildren:YES];
}
[self updateDiscloseControl];
}
- (void)updateDiscloseControl {
if ([DEFAULTS boolForKey:@"disclosure"]) {
[disclosureTextField setStringValue:@"Collapse all"];
[disclosureButton setIntValue:1];
} else {
[disclosureTextField setStringValue:@"Expand all"];
[disclosureButton setIntValue:0];
}
}
#pragma mark - Sort
- (IBAction)sortChanged:(id)sender {
NSArray *comp = [[sender title] componentsSeparatedByString:@" by "];
NSString *sortBy = [[comp lastObject] lowercaseString];
[DEFAULTS setObject:sortBy forKey:@"sortBy"];
[self updateProcessCountHeader];
[self updateSorting];
}
- (void)updateSorting {
NSString *sortBy = [DEFAULTS objectForKey:@"sortBy"];
// Default to sorting alphabetically by name
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"pname"
ascending:[DEFAULTS boolForKey:@"ascending"]
selector:@selector(localizedCaseInsensitiveCompare:)];
// Integer comparison for string values block
NSComparator integerComparisonBlock = ^(id first,id second) {
NSUInteger cnt1 = [first intValue];
NSUInteger cnt2 = [second intValue];
if (cnt1 < cnt2) {
return NSOrderedAscending;
} else if (cnt1 > cnt2) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
};
// Number of children (i.e. file count) comparison block
NSComparator numChildrenComparisonBlock = ^(id first,id second) {
NSUInteger cnt1 = [first count];
NSUInteger cnt2 = [second count];
if (cnt1 < cnt2) {
return NSOrderedAscending;
} else if (cnt1 > cnt2) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
};
if ([sortBy isEqualToString:@"process id"]) {
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"pid"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:integerComparisonBlock];
}
else if ([sortBy isEqualToString:@"user id"]) {
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"userid"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:integerComparisonBlock];
}
else if ([sortBy isEqualToString:@"file count"]) {
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"children"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:numChildrenComparisonBlock];
}
else if ([sortBy isEqualToString:@"process type"]) {
// Process type sorting uses the "bundle" and "app" boolean properties
NSMutableArray *sdesc = [NSMutableArray new];
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"bundle"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:integerComparisonBlock];
[sdesc addObject:sortDesc];
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"app"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:integerComparisonBlock];
[sdesc addObject:sortDesc];
self.sortDescriptors = [sdesc copy];
return;
}
else if ([sortBy isEqualToString:@"carbon psn"]) {
sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"psn"
ascending:[DEFAULTS boolForKey:@"ascending"]
comparator:integerComparisonBlock];
}
else if ([sortBy isEqualToString:@"bundle identifier"]) {
sortDesc = [[NSSortDescriptor alloc] initWithKey:@"identifier"
ascending:[DEFAULTS boolForKey:@"ascending"]
selector:@selector(caseInsensitiveCompare:)];
}
self.sortDescriptors = @[sortDesc];
}
#pragma mark - Authentication
- (IBAction)toggleAuthentication:(id)sender {
if (isRefreshing) {
NSBeep();
return;
}
if (!authenticated) {
OSStatus err = [self authenticate];
if (err == errAuthorizationSuccess) {
authenticated = YES;
} else {
if (err != errAuthorizationCanceled) {
NSBeep();
DLog(@"Authentication failed: %d", err);
}
[self deauthenticate];
return;
}
} else {
[self deauthenticate];
}
NSString *iconName = authenticated ? @"Unlocked" : @"Locked";
NSImage *img = [IconUtils imageNamed:iconName];
NSString *actionName = authenticated ? @"Deauthenticate" : @"Authenticate";
NSString *ttip = authenticated ? @"Deauthenticate" : @"Authenticate to view all system processes";
[authenticateButton setImage:img];
[authenticateButton setToolTip:ttip];
[authenticateMenuItem setImage:img];
[authenticateMenuItem setTitle:actionName];
[authenticateMenuItem setToolTip:ttip];
[self refresh:self];
}
- (OSStatus)authenticate {
DLog(@"Authenticating");
const char *toolPath = [LSOF_PATH fileSystemRepresentation];
AuthorizationItem myItems = { kAuthorizationRightExecute, strlen(toolPath), &toolPath, 0 };
AuthorizationRights myRights = { 1, &myItems };
AuthorizationFlags flags = \
kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed \
| kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
// Create authorization reference
OSStatus err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authRef);
if (err != errAuthorizationSuccess) {
return err;
}
// Pre-authorize the privileged operation
err = AuthorizationCopyRights(authRef, &myRights, kAuthorizationEmptyEnvironment, flags, NULL);
if (err != errAuthorizationSuccess) {
return err;
}
return err;
}
- (void)deauthenticate {
DLog(@"Deathenticating");
if (authRef) {
AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
authRef = NULL;
}
authenticated = NO;
}
#pragma mark - NSOutlineViewDelegate
- (void)outlineView:(NSOutlineView *)ov didClickTableColumn:(NSTableColumn *)tableColumn {
[DEFAULTS setBool:![DEFAULTS boolForKey:@"ascending"] forKey:@"ascending"];
[self updateSorting];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
NSInteger selectedRow = [outlineView selectedRow];
if (selectedRow >= 0) {
Item *item = [[outlineView itemAtRow:selectedRow] representedObject];
BOOL canReveal = [WORKSPACE canRevealFileAtPath:item[@"name"]];
BOOL hasBundlePath = [WORKSPACE canRevealFileAtPath:item[@"path"]];
[revealButton setEnabled:(canReveal || hasBundlePath)];
[getInfoButton setEnabled:YES];
[killButton setEnabled:YES];
[infoPanelController loadItem:item];