~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychPriority/KillUpdateProcess.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function wasRunning=KillUpdateProcess
2
 
% wasRunning=KillUpdateProcess
3
 
4
 
% OS X: ___________________________________________________________________
5
 
%
6
 
% Kill the OS X update daemon.  Killing update prevents  interruption of
7
 
% MATLAB at 30-second intervals, enabling  the Psychtoolbox to run smooth,
8
 
% uninterrupted animations.
9
 
%
10
 
% Use Priority or Rush instead of KillUpdateProcess; You need not invoke
11
 
% KillUpdateProcess directly from your scripts because Priority and Rush do
12
 
% that for you.  
13
 
14
 
%
15
 
%                          * * * * * * * * * 
16
 
% IMPORTANT: When first installing the Psychtoolbox on your computer, run
17
 
% the included simplepsychtoolboxsetup.sh script from an OS X terminal
18
 
% window.  By modifying system file "/etc/sudoers"
19
 
% simplepsychtoolboxsetup.sh gives MATLAB permission to shutdown the OS X
20
 
% update process. KillUpdateProcess will detect if MATLAB does not have
21
 
% permission to kill the update process and prompt you to run
22
 
% simplepsychtoolboxsetup.sh. 
23
 
%                          * * * * * * * * * 
24
 
%
25
 
% KillUpdateProcess first checks to see if an update process is running. If
26
 
% no update process is running, then KillUpdateProcess returns value 0 to
27
 
% indicate that no update process was running.  If an update process is
28
 
% running, then KillUpdateProcess kills the update process, reads the jobs
29
 
% list to verify that update was indeed killed, and returns value 1 to
30
 
% indicate that update was running.  If KillUpdateProcess can not kill
31
 
% update because simplepsychtoolboxsetup.sh has not been run, then it
32
 
% prompts within the MATLAB command window  to run
33
 
% simplepsychtoolboxsetup.sh, opens a terminal window, and issues
34
 
% instructions.    
35
 
%
36
 
% To insure that MATLAB does not exit without restarting the update
37
 
% process,  The Psychtoolbox script finish.m, which MATLAB automatically
38
 
% calls when exiting MATLAB, invokes StartUpdateProcess. KillUpdateProcess
39
 
% will fail to kill update if it does not detect a finish.m file on the
40
 
% MATLAB path which contains a call to StartUpdateProcess. 
41
 
42
 
% KillUpdateProcess will fail to kill update under any of these conditions:
43
 
%   - There is no finish.m file on the MATLAB path 
44
 
%   - There is more than one finish.m file on the MATLAB path  
45
 
%   - There is a single finish.m file on the MATLAB path but it does not
46
 
%     contain a  StartUpdateProcess call.
47
 
%   - The system "/etc/sudoers" file does not give MATLAB permission to 
48
 
%     shutdown update.  If KillUpdateProcess detects that 
49
 
%     simplepsychtoolboxsetup.sh has not been run, then it reports this, prompts
50
 
%     to run simplepsychtoolboxsetup.sh, and returns the value Nan.   
51
 
52
 
% The OS X update process is part of its BSD Unix foundation. For more
53
 
% information on update, see the Unix manual page for update. (Enter "man
54
 
% update" in the OS X Terminal window.)
55
 
56
 
% OS 9: ___________________________________________________________________
57
 
%
58
 
% KillUpdateProcess does not exist in OS 9. 
59
 
%
60
 
% WINDOWS: ________________________________________________________________
61
 
62
 
% KillUpdateProcess does not exist in Windows.
63
 
64
 
% _________________________________________________________________________
65
 
%
66
 
% See also: StartUpdateProcess, KillUpdateProcess, IsUpdateRunning
67
 
 
68
 
% HISTORY
69
 
% 1/8/04    awi     Wrote it.
70
 
% 1/16/04   awi     Improved comments.  Added AssertOSX.  
71
 
%                   Consider changing this to AssertUnix. 
72
 
% 1/16/04   awi     Replaced kill line.
73
 
% 8/21/05   awi     Return Nan in case of no permission to shutdown update.  
74
 
% 8/22/05   awi     Prepended explanation to notice that "Priority" evoked it.
75
 
% 8/24/05   awi     Deleted from help text paragraph stating that KillUpdateProcess
76
 
%                   requests su password. Mario's script which modifies the
77
 
%                   sudoers file obviates that.
78
 
%                   Explained simplepsychtoolboxsetup.sh in help.
79
 
%                   Cosmetic
80
 
% 10/10/05 awi          Merged with dgp version: changed "strcmp" to "streq"
81
 
 
82
 
% TO DO: 
83
 
% A better solution to the problem of insurring that the update process is
84
 
% eventually restored would be to provide a Cocoa application, launched by
85
 
% KillUpdateDameon, which checked for update and nagged if it went
86
 
% undetected for a time. If MATLAB were found to be not running it would
87
 
% make sure that update was running.  The same application could handle
88
 
% starting and stopping update.   Even if the app did not
89
 
% handle starting and stopping, this scheme could be made foolproof (for
90
 
% reasonable levels of foolishness) if the app would not exit unless update
91
 
% was running and matlab would not kill update unless the app were running.
92
 
% That way alwyas either update is running or the update restoration app is
93
 
% running. This update gurantor app could also have time limit after which
94
 
% it will restart update even if MATLAB were running.  Perhaps it could
95
 
% decide this on the basis of MATLAB idle time?  
96
 
97
 
 
98
 
AssertOSX;
99
 
 
100
 
% check that the shell script to give everyone permission to shut down and
101
 
% restart the update process has been run.  It leaves its mark by backing
102
 
% up the /etc/sudoers file into  /etc/sudoers_pre_psychtoolbox.  This is
103
 
% not a perfect test, because someone could modify /etc/sudoers without
104
 
% deleting the /etc/sudoers_pre_psychtoolbox file. However, we can not
105
 
% examine the contents of sudoers without root.
106
 
if exist('/etc/sudoers_pre_psychtoolbox')~=2
107
 
    Screen('CloseAll');
108
 
    fprintf(' This message has been triggered by an attempt to raise priority of the MATLAB \n');
109
 
    fprintf(' process above level 0.  If you did not call "Priority" or "Rush" directly from the\n');
110
 
    fprintf(' MATLAB command line, then it may have been called within another script or function\n');
111
 
    fprintf(' which you did call\n');
112
 
    fprintf('\n');
113
 
 
114
 
        fprintf(' In order to use raise priority on OS X you first need to run the script\n');
115
 
        fprintf(' simplepsychtoolboxsetup.sh from the OS X Terminal command line. In OS X,\n');
116
 
        fprintf(' Priority and Rush commands require shutting down the system "update" process\n');
117
 
        fprintf(' for as long as priority remains above 0. Shutting down update ordinarily\n');
118
 
        fprintf(' requires entering an administrator password.  However, the Psychtoolbox\n');
119
 
        fprintf(' includes a shell script which will reconfigure your computer to allow MATLAB\n');
120
 
        fprintf(' to shutdown update without having to enter a password. Examining your /etc\n');
121
 
        fprintf(' directory, the absence of file "sudoers_pre_psychtoolbox " seems to indicate that\n');
122
 
        fprintf(' you have not run the simplepsychtoolboxsetup.sh script since installing\n');
123
 
        fprintf(' the Psychtoolbox.\n\n');
124
 
        fprintf(' Should I open a terminal window and give instructions ');
125
 
    yn=input(' [Y/N]? ','s');
126
 
    keepAsking=1;
127
 
    while(keepAsking)
128
 
        goAhead=streq(upper(yn), 'Y') || streq(upper(yn), 'YES');
129
 
        exitOut=streq(upper(yn), 'N') || streq(upper(yn), 'NO');
130
 
        keepAsking= ~(goAhead || exitOut);
131
 
    end %while 
132
 
    if exitOut
133
 
        wasRunning=nan;
134
 
        return;
135
 
    end
136
 
    unix('open -a Terminal');  
137
 
    fprintf('\n\n\nInstructions for calling the simplepsychtoolboxsetup.sh script within the\n');
138
 
    fprintf('terminal window:\n\n');
139
 
    fprintf('1. Find the full path to the simplepsychtoolboxsetup.sh script on your\n');
140
 
    fprintf('   hard drive. For example, if you keep your Psychtoolbox folder inside\n');
141
 
    fprintf('   your applications folder, then the path would be:\n');
142
 
    fprintf('      /Applications/Psychtoolbox/PsychContributed\n\n');
143
 
    fprintf('2. Use the unix cd command to move to the PsychContributed directory. For example:\n');
144
 
    fprintf('      cd /Applications/Psychtoolbox/PsychContributed\n\n');
145
 
    fprintf('3. Invoke the simplepsychtoolboxsetup.sh script using Sudo.  You will then be\n');
146
 
    fprintf('   prompted to enter an administrator password.  For example:\n');
147
 
    fprintf('      sudo ./simplepsychtoolboxsetup.sh\n\n\n');
148
 
    fprintf(' If you successfully run the simplepsychtoolboxsetup.sh script after installing\n');
149
 
    fprintf(' the Psychtoolbox, then you should thereafter be able to raise priority\n');
150
 
    wasRunning=nan;
151
 
    return;
152
 
end
153
 
% check that the command to restart update is implanted in the finish file
154
 
if ~IsStartUpdateImplantedInFinish
155
 
    error('Killing the update process first requires that StartUpdateProcess be called from the finish.m file.  See "help KillUpdateProcess".');
156
 
end
157
 
p=GetProcessDescriptorFromCommandName('update');
158
 
if isempty(p)
159
 
    wasRunning=0;
160
 
    return;
161
 
elseif length(p) > 1
162
 
    error('Multiple processes with command name "update" detected.  This is unexpected.');
163
 
else  %there is only one update process
164
 
%       unix([ 'sudo kill ' int2str(p.pid) ]);
165
 
    unix('sudo /usr/bin/killall update');
166
 
        pause(1);   %allow OS X lots of time to shut down update before checking. 
167
 
        p=GetProcessDescriptorFromCommandName('update');
168
 
        if ~isempty(p)
169
 
        %allow OS X lots more time to shut down update before checking. 
170
 
        pause(5);   
171
 
        p=GetProcessDescriptorFromCommandName('update');
172
 
        end
173
 
        if ~isempty(p)
174
 
        error('Failed to kill the update process');
175
 
        end
176
 
        wasRunning=1;
177
 
end %else
178
 
 
179
 
 
180
 
 
181